当前位置: 动力学知识库 > 问答 > 编程问答 >

Unexpected characters at the end of char array - C

问题描述:

I know similar questions have been posted before but I haven't been able to solve the issue for my case.

I have the following C code:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int main()

{

char textChars[4] = { 'A', 'B', 'C', 'D' };

char noMatchChars[4] = { '1', '2', '3', '4' };

int tLengths[5] = { 14, 142, 1414, 14142, 141420 };

int i,j;

for (i = 0; i < 1; i++)

{

char textString1[tLengths[i]+1];

char textString2[tLengths[i]+1];

char textString3[tLengths[i]+1];

char textString4[tLengths[i]+1];

for (j = 0; j < tLengths[i]; j++)

{

textString1[j] = textChars[0];

textString2[j] = textChars[1];

textString3[j] = textChars[2];

textString4[j] = textChars[3];

}

textString1[tLengths[i]] = '\0';

textString2[tLengths[i]] = '\0';

textString3[tLengths[i]] = '\0';

textString4[tLengths[i]] = '\0';

FILE *fp;

char filepathPattern[14];

char filepathText[11];

char iChar[1];

sprintf(iChar, "%d", i);

strcpy(filepathText, iChar);

strcat(filepathText, "_text1.txt");

fp = fopen(filepathText, "w");

fprintf(fp, textString1);

fclose(fp);

memset(filepathText,0,strlen(filepathText));

strcpy(filepathText, iChar);

strcat(filepathText, "_text2.txt");

fp = fopen(filepathText, "w");

fprintf(fp, textString2);

fclose(fp);

memset(filepathText,0,strlen(filepathText));

strcpy(filepathText, iChar);

strcat(filepathText, "_text3.txt");

fp = fopen(filepathText, "w");

fprintf(fp, textString3);

fclose(fp);

memset(filepathText,0,strlen(filepathText));

strcpy(filepathText, iChar);

strcat(filepathText, "_text4.txt");

fp = fopen(filepathText, "w");

fprintf(fp, textString4);

fclose(fp);

}

}

It works as expected for every string expect for textString4 which outputs as 14 'D's as expected followed by a random character and then 14 'C's (the previous string) for some reason but the other strings don't have this issue.

I assumed it was a memory issue but when I replaced


char textStringX[tLengths[i]+1];


with char *textStringX = malloc( sizeof(char) * ( tLengths[i] + 1 ) );


the result was identical.

I'm new to C so apologies if the solution to this is trivial.

网友答案:

The first issues are string related. The following lines:

char iChar[1];
sprintf(iChar, "%d", i);

Are a problem, in that you created a char array, iChar, with room for only one char, then in the very next line try to use a string function sprintf to place two chars into iChar: the value of i (0 at this point) and the NULL char. You need to create iChar with more space: i.e.

char iChar[3]; // will allow printing up to any two digit value + NULL.  

Eg. zero would look like this: |0|\0|\0|
99 like this: |9|9|\0|

In C, without a NULL termination, you do not have a C string. The string functions will not work properly without a C string.

Because the write to iChar fails in the call above, the next lines in your code, also string functions always expect NULL terminated char arrays. anything else will cause them to fail:

strcpy(filepathText, iChar);
strcat(filepathText, "_text1.txt");

Because I do not know the contents of your text file, I cannot take your code beyond this. But address these string issues, then step through your code line by line. I believe most of the issues you cite will be addressed.

分享给朋友:
您可能感兴趣的文章:
随机阅读: