C 连接字符串并将其复制到数组时获得奇怪的输出
我正在开发一个函数,该函数从文件中提取用户指定数量的字符,并将它们连接成一个字符串,该字符串将存储到数组中。将提示用户输入目录和要连接的字符数。然而,当我尝试这样做并打印出结果时,我得到了奇怪的符号和数字,就像这样 @ؙ>\212\377`\366\277\u377g 有人能解释一下为什么会发生这种情况吗。非常感谢。信息:这是现在的主要内容,fileNames是目录中的文件名列表C 连接字符串并将其复制到数组时获得奇怪的输出,c,arrays,string-concatenation,C,Arrays,String Concatenation,我正在开发一个函数,该函数从文件中提取用户指定数量的字符,并将它们连接成一个字符串,该字符串将存储到数组中。将提示用户输入目录和要连接的字符数。然而,当我尝试这样做并打印出结果时,我得到了奇怪的符号和数字,就像这样 @ؙ>\212\377`\366\277\u377g 有人能解释一下为什么会发生这种情况吗。非常感谢。信息:这是现在的主要内容,fileNames是目录中的文件名列表 /*struct def*/ typedef struct arr{ char *wordd; int indx;
/*struct def*/
typedef struct arr{
char *wordd;
int indx;
}ARR;
/* Dynamic array of structs*/
ARR arr[COUNTER];
char wordConct[81];
for (i = 0 ; i < count; i++) {
sprintf( fullName, "%s/%s", directoryName, fileNames[i]); //DIRECTORY/FILENAME FORMAT
/*Read in from file*/
inFile = fopen( fullName, "r");
assert( inFile);
/* This loop concatenates input then stores in array */
while( fscanf( inFile, "%c", word) != EOF) {
if (isalnum(*word) != 0) { // Only goes through if alphanumeric
//nChar is user specified number of characters to be concatenated
if (j <= nChar) {
strcat(wordConct, word); //concatenates
j++; //add 1 to j to count until nChar is reached
}
else {
//copy concatenated word into the array once nChar is reached
strcpy(arr[j].wordd, wordConct);
}
j = 0; //reset back to zero for next iteration
}
}
}
好的,你有没有在调试器中检查过代码,检查过变量,等等?是的,在每个if,else if中,看看它是否通过了这些点,它似乎工作正常。我还没有尝试过调试器。wordConct必须是有效的C样式字符串。现在只是垃圾。初始化为零。你能解释一下ARR是什么吗?我有点困惑,因为您使用的是j,此时它将是用户指定的长度限制,来引用ARR类型数组并执行strcpy…我觉得有些可疑。并在while循环的每次迭代中重新初始化它。