简体   繁体   English

ÿ 在 txt 文件中打印为 EOF

[英]ÿ printed as EOF in txt file

I have a program designed to manage student records in a txt file.我有一个程序旨在管理 txt 文件中的学生记录。 I am attempting to delete a student record.我正在尝试删除学生记录。 The program opens the file, reads the contents and prints them for the user to see.该程序打开文件,读取内容并打印出来供用户查看。 The user then enters an integer for which line they wish to remove.然后用户输入一个整数,他们希望删除哪一行。 It then returns the new content for the user to see.然后它返回新内容供用户查看。

int deleteStudent()
{
    FILE *dOldFile, *dNewFile;//Original file and New temp file
    char ch;
    int deleteLine, temp = 1;

    dOldFile = fopen(fName, "r");//fName is name of file user enters in previous function
    ch = getc(dOldFile);
while (ch != EOF)
{
    printf("%c", ch);//Prints content of Original file for user to see
    ch = getc(dOldFile);
}
rewind(dOldFile);
printf(" \n Enter line number of the line to be deleted:\n>");
scanf("%d", &deleteLine);//user selects which line of file to be removed


dNewFile = fopen("copy.c", "w");
ch = getc(dOldFile);
while (ch != EOF)
{
    ch = getc(dOldFile);
    if (ch == '\n')
        temp++;
        if (temp != deleteLine) //except the line to be deleted
        {
            putc(ch, dNewFile); //copy all lines in file copy.c
        }
}
fclose(dOldFile);
fclose(dNewFile);
remove(fName);
rename("copy.c", fName);
printf("\n Student Records Remaining:\n");
dOldFile = fopen(fName, "r");
ch = getc(dOldFile);
while (ch != EOF)
{
    printf("%c", ch);
    ch = getc(dOldFile);
}
fclose(dOldFile);
printf("\n");
return 0;
}

The issue is, and I believe it may be an issue with my while loops, is the contents printed to the new and renamed text file from fprintf has the first character removed, and from what I understand, a symbol for EOF printed at the end of the edit.问题是,我相信这可能是我的 while 循环的问题,从fprintf打印到新的和重命名的文本文件的内容是否删除了第一个字符,据我所知,最后打印的 EOF 符号的编辑。 For example the original file had例如原始文件有

23 John Smith 18 5555555 23 约翰·史密斯 18 5555555
24 Tom Costa 15 5555555 24 汤姆科斯塔 15 5555555
25 Barry West 35 5555555 25 巴里·韦斯特 35 5555555
26 Daren carr 22 5555555 26 达人卡尔 22 5555555

If the user decides to delete line 3 for example.例如,如果用户决定删除第 3 行。 This is the result in the txt file这是txt文件中的结果

3 John Smith 18 5555555 3 约翰·史密斯 18 5555555
24 Tom Costa 15 5555555 24 汤姆科斯塔 15 5555555
26 Daren carr 22 5555555ÿ 26 达伦卡尔 22 5555555ÿ

Every thing is fine.Just replace the while loop with the while loop and delete the一切都很好。只需将 while 循环替换为 while 循环并删除

 ch = getc(dOldFile);

before the while as shown :如图所示:

dNewFile = fopen("copy.c", "w");
//ch = getc(dOldFile);    comment this out
while((ch = getc(dOldFile))!=EOF)
  {       
   if (ch == '\n')
    temp++;
    if (temp != deleteLine) //except the line to be deleted
    {
        putc(ch, dNewFile); //copy all lines in file copy.c
    }
   }   //modify while to make it better
  fclose(dOldFile);
  fclose(dNewFile);

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

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