0

完整代码:https ://pastebin.com/NpspcJB8

我不确定我是否使用正确fgets()fputs()但我需要这个或类似的东西来编辑我的程序文件。在我的程序中,我将创建一个文件,但是当我在程序中生成文件内容时,我得到了这个:

文件输出

Space: D44 Customer Name: empty Registration number: empty

Space: D45 Customer Name: empty Registration number: empty

Space: D46 Customer Name: empty Registration number: empty

Space: D47 Customer Name: empty Registration number: empty

Space: D48 Customer Name: empty Registration number: empty

Space: D49 Customer Name: empty Registration number: empty

Space: D50 Customer Name: empty Registration number: empty
 empty
 empty
 empty
 empty
 empty
 empty
 empty

当我在程序中添加停车位时会发生这种情况,我相信这只会改变数组值。我需要empty在文件输出中删除,我尝试在代码中使用我首先提到的函数,在我创建的函数中:

int CheckExtraInput() {

    fptr=fopen("parking_spaces.txt", "r+");

    if (fptr==NULL) {
        printf("Error! File does not exist");
        return 1;
    }   

    char extraInput[] = " empty\n";

// http://www.cplusplus.com/reference/cstdio/fgets/ 

// https://www.tutorialspoint.com/c_standard_library/c_function_strchr.htm  

//  char *s = strchr(extraInput, '\n');
//  if(s) *s = '\0';

//  fseek(fptr, 7, SEEK_SET);
    while (fgets(extraInput, 8, fptr) != NULL) {

        fputs(" aaa ", fptr);

    }

}

我曾经" aaa "测试过这段代码,但它没有。我相信我可以使用更多来让它工作,但我不确定是什么;我之前使用fseek()过类似的结果。

我只需要找到一种方法让程序走到文件末尾,如果它开始检测到empty,它需要将其删除以节省存储空间。

4

0 回答 0