简体   繁体   中英

C: Handling fgetc input from file streams

OK, I'm working on a spellcheck program in C. The bit I'm having trouble with is reading a word from the system dictionary into the program. I've searched for an answer but I couldn't find anything quite right, probably because this is a pretty rudimentary thing. Here's the code I've got so far.

void readWord(FILE * stream, int buffer[]){
15   
16   while(fgetc(stream) != 10){
17     buffer[i] = fgetc(stream);              
18     printf("copying %d to buffer\n", buffer[i]);    //added for debugging
19   }
20   return;
21 }

Here are the variables used in the function call along with the call

int buffer[WIZE];
readWord(dictionary, buffer, WSIZE);

dictionary is pointing to /usr/share/dict/words, and there's code to make sure it opens correctly but I didn't put it in this question; maybe that's the problem after all. This should output Aarhus, but instead it outputs:

Dictionary opened successfully
copying 97 to buffer
copying 104 to buffer
copying 115 to buffer

I'm wondering if each time the program executes line 16 the file position gets updated to the next character? The output is every other letter of Aarhus, if the pointer is starting at the second character.

The readWord function is also used to read individual words from the text files to check, which is the reason I didn't use getline. There are other functions to flush the buffer to a string along with the terminal null character, and display. This is just the bit that deals with getting one word at a time from files.

您应该使用getline从文件流中读取输入。

97 is the character code for a , 104 for h and so on. Try printing them as characters using %c .

I'm wondering if each time the program executes line 16 the file position gets updated to the next character?

In your readWord you will need to update the index ( i ) of buffer too every time you copy a character. You will also need to null-terminate buffer once you've hit a newline. 10 is not portable; use \\n when checking for newlines.

while(fgetc(stream) != 10){

In the while condition you are reading a character that is not saved to anywhere. Then, you read another one in the body, so you are reading 2 characters per iteration and saving only one.

Also, you are storing the character in buffer[i], but i is not being increased. Try this:

while((buffer[i] = fgetc(stream)) != 10){
     printf("copying %c to buffer\n", buffer[i]);    //added for debugging
     i++;
 }

Got it working thanks to the answers; Here's the fix

void readWord(FILE * stream, int buffer[]){
    unsigned short int i = 0;
    while((buffer[i] = fgetc(stream)) != '\n'){
            printf("copying %d to buffer\n", buffer[i]);
            i++;
    }
    return;
}

-and the output

Dictionary opened successfully
copying 65 to buffer
copying 97 to buffer
...
Dictionary word is Aarhus

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM