简体   繁体   English

C中的fgets函数和文件处理

[英]fgets function and file handling in C

I am trying to make a program which will store the data entered by the user in a text file whose name is provided by the user. 我正在尝试制作一个程序,该程序将用户输入的数据存储在名称由用户提供的文本文件中。 Program will terminate when the user enters exit. 当用户进入出口时程序将终止。 strcmp function of string.h is used for string comparison and fgets() is used to read data from stdin. string.h的strcmp函数用于字符串比较,而fgets()用于从stdin读取数据。

Here is my code. 这是我的代码。

#include<stdio.h>
#include<string.h>

void main()
{
    char file[60];          // will store file name
    printf("Enter file name: ");
    fgets(file, 59, stdin);

    FILE *fp = fopen(file, "a+");   // open file in append mode

    if(fp == NULL){
        printf("File not found !");
        return;
    }

    char data[100];
    printf("Enter some data to add to file(exit to terminate): ");
    fgets(data, 99, stdin);

    int flag = strcmp(data, "exit");

    while(flag != 0){
        fputs(data, fp);

        fgets(data, 59, stdin);
        flag = strcmp(data, "exit");
        printf("%d\n", flag);       // for checking whether string are correctly comapred or not
    }

    printf("Bye");

}

Program does not terminate even if i enter exit. 即使我进入出口,程序也不会终止。 I have also tried concatenating "\\n" at the end of string input by user but that also doesn't help. 我也尝试在用户输入的字符串末尾连接“ \\ n”,但这也无济于事。 Although, gets() function works fine, but i know it is not preferred to use to I shifted to fgets() but it doesn't work for me. 虽然gets()函数可以正常工作,但我知道使用fgets()并不是首选,但是它对我不起作用。

Check the man page for fgets() , it reads and stores the newline (caused by pressing ENTER ) after the entered input. 检查手册页中的fgets() ,它在输入后读取并存储换行符(按ENTER引起)。 Thus, the strcmp() fails. 因此, strcmp()失败。

You have to manually strip the input buffer off the newline, before you can compare the input. 在比较输入之前,必须手动从换行符中除去输入缓冲区。 A simple yet elegant way of doing that would be 一个简单而优雅的方法是

 data[strcspn(data, "\n")] = 0;

fgets reads in a complete "line", ie a sequence of characters until (and including!) a new line character. fgets读取完整的“行”,即一系列字符,直到(包括!)换行符为止。 Hence, when a user presses "Enter", the new line will be part of the string read in and a strcmp(data,"exit") will evaluate to "not equal". 因此,当用户按下“ Enter”键时,新行将成为读入字符串的一部分,并且strcmp(data,"exit")评估结果为“不等于”。

So either strip off the new line before comparison, or compare with a string literal including a new line. 因此,要么在比较之前删除新行,要么与包含新行的字符串文字进行比较。 Since you write the data as is(ie including the new lines) to a file, it seems cumbersome to first strip the new line off and add it then in the output manually. 由于您将数据原样(即包括新行)写入文件,因此先剥离新行并手动添加然后将其添加到输出中似乎很麻烦。 So I'd actually suggest the second approach: 所以我实际上建议第二种方法:

fgets(data, 100, stdin);
flag = strcmp(data, "exit\n");

An alternative would be to use strstr if excess characters do not matter (ie your program would exit if the user types "exit" or "asdfexitasdf". - both of which contain "exit".) 如果多余的字符无关紧要,则可以使用strstr (即,如果用户键入“ exit”或“ asdfexitasdf”,则程序将退出-两者都包含“ exit”。)

So 所以

int flag = strstr(data, "exit");
if(flag != NULL)
    //exit the program
else
    //stay in the program

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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