C文件处理查询 [英] C file handling query

查看:123
本文介绍了C文件处理查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个程序,需要用户输入,并在一个文件中进行比较,以特定的线,但是最后一行将始终记不正确的,所以有人可以解决这个问题对我?谢谢。

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

(库是标准输入输出,conio和字符串)

(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\n");
        exit(1);
    }

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

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

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

 getch();

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

有关最后一行的输入,输出将始终是不正确的,我相信这是值得做的for循环不把它变成下一个变量,但看到的&lt; =符号,使这个方案更糟糕,我真的只需要一个简单的解决方案感谢。

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.

推荐答案

有些意见:

您必须从不使用获得(它甚至不是在C11标准了)。取而代之的获取(文本)使用与fgets(文字,sizeof的(文本),标准输入) - 这样一个长的输入不会溢出的文本阵列。

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.

将有东西在最后打印,因为你不检查无论是获得与fgets ,因此当文件结束时发生的任何文件或迭代的其余部分仍然运行用户输入。 与fgets 收益 NULL ,如果它没有读什么 - 检查,而不是使用的feof

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.

您从文件输入用户输入删除换行符,但没有,所以当您从切换比较将始终失败可获得与fgets (不换行剥离)。第二个(否则无意义)正文[C] 对比较应当针对的'\\ n'

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 '\n'.

修改:另外,如果您的文件中换行符并没有结束的最后一行,比较会失败在最后一行,因为你不检查,如果最后一个字符是换行符之前你删除它。

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.

为(循环= 0;环路LT;线; ++环) -loop是没有意义的,因为始终为1,所以身体只执行一次。

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

您有不必要的全局变量的程序难以遵循的。而且,例如,你的本地文[64] 掩盖了全球正文[100] ,所以如果你认为你正在修改全局缓冲区,你是不是。如果你的code是完整的,没有一个变量应该是全球性的。

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.

功能残培()是非标准的。有没有简单的直接替代品,所以你可能只是接受你不编写便携式code,但它的东西要注意的。

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.

这篇关于C文件处理查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆