连接字符串并将其复制到数组时获得奇怪的输出

时间:2015-02-24 02:27:47

标签: c arrays string-concatenation

我正在开发一个函数,该函数从文件中获取用户指定数量的字符,并将它们连接成一个将存储到数组中的字符串。用户将提示连接目录和要连接的字符数量。然而,当我尝试这样做并打印出结果时,我得到了奇怪的符号和数字;

  

@ؙ> \ 212 \ 377` \ 366 \ 277_ \377克

任何人都可以解释为什么会发生这种情况。谢谢。信息:现在在main()中,fileNames是目录中的文件名列表。

/*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               
        }
    }    
}

0 个答案:

没有答案