Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

string - C file handling query

So I have a program that takes user input and compares it to a specific line in a file, however the final line will always be credited as incorrect, so can someone solve this for me?, thanks.

File content (just a list of random words)

Baby
Milk
Car
Face
Library
Disc
Lollipop
Suck
Food
Pig

(libraries are stdio,conio and string)

char text[100], blank[100];
int c = 0, d = 0;

void space(void);

int main()
{
    int loop = 0;
    char str[512];
    char string[512];
    int line = 1;
    int dis = 1;
    int score = 0;
    char text[64];

    FILE *fd;

    fd = fopen("Student Usernames.txt", "r");   // Should be test

    if (fd == NULL)
    {
        printf("Failed to open file
");
        exit(1);
    }

    do
    {
        printf("Enter the string: ");
        gets(text);

        while (text[c] != '')
        {
            if (!(text[c] == ' ' && text[c] == ' '))
            {
                string[d] = text[c];
                d++;
            }
            c++;
        }

 string[d] = '';
 printf("Text after removing blanks
%s
", string);

 getch();

for(loop = 0;loop<line;++loop)
{
    fgets(str, sizeof(str), fd);
}
printf("
Line %d: %s
", dis, str);
dis=dis+1;
str[strlen(str)-1] = '';
if(strcmp(string,str) == 0 )
 {
 printf("Match
");
 score=score+2;
 }
     else
     {
     printf("Nope
");
     score=score+1;
     }
 getch();
 c=0;
 d=0;
}
while(!feof(fd));
printf("Score: %d",score);
getch();
}

For any input on the last line, the output will always be incorrect, I believe this is something to do with the for loop not turning it into the next variable, but seeing as the <= notation makes this program worse, I really just need a simple fix for the program thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Some observations:

You must never use gets (it is not even in the C11 standard anymore). Instead of gets(text) use fgets(text, sizeof(text), stdin) – this way a long input will not overflow the text array.

There will be stuff printed at the end because you don't check the return value of either the gets or the fgets, so when end of file occurs for either the file or for user input the rest of that iteration still runs. fgets returns NULL if it didn't read anything – check for that instead of using feof.

You remove newlines from the file input but not from the user input, so the comparison will always fail when you switch from gets to fgets (which doesn't strip linefeeds). The second (otherwise pointless) comparison of text[c] against ' ' should be against ' '.

edit: Also, in case the last line of your file does not end in a linefeed, the comparison will fail on the last line because you don't check if the last character is a linefeed before you remove it.

The for (loop = 0; loop < line; ++loop) -loop is pointless because line is always 1, so the body is only executed once.

You have unnecessarily global variables which the program hard to follow. And, for instance, your local text[64] overshadows the global text[100], so if you think you are modifying the global buffer, you are not. If your code is complete, none of the variables should be global.

The function getch() is non-standard. There is no easy direct replacement, so you may just accept that you are not writing portable code, but it's something to be aware of.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...