简体   繁体   English

执行fgets / fputs时,最后一行重复

[英]Last line repeats when doing fgets/fputs

I'm doing an insertion, that imply a file , a string , and a new file who will receive all the data of the original file plus the string to be inserted , and it will replace the original file. 我正在执行插入操作,这意味着一个文件一个字符串一个新文件该文件将接收原始文件的所有数据以及要插入的字符串 ,它将替换原始文件。

So, for example, my original file: 因此,例如,我的原始文件:

data.txt data.txt

 line1
 line2
 line3
 line4
 line5 

will become, with the insertion of the string "newline": 通过插入字符串“ newline”将变为:

data_temp.txt --> renamed later data.txt data_temp.txt- >重命名为更高的data.txt

 line1
 line2
 line3
 line4
 line5 
 newline

With that purpose, I have the following code: 为此,我有以下代码:

/* FILE variables of the original file and the new file */
FILE *data, *data_temp;
data = fopen( "data.txt", "r" ); 
data_temp = fopen( "data_temp.txt", "w" ); 

/* String buffer */
char buf[256];                      
int i_buf;

/* The string to be inserted in the new file */
char newline[10] = "newline";

/* For each line of the original file, the content of each line 
is written to the new file */
while(!feof(data))              
{
            /* Removing the \n from the string of the line read */
    fgets(buf, MAX_INSERT, data);                   
    for(i_buf = strlen(buf)-1; i_buf && buf[i_buf] < ' '; i_buf--)  
    buf[i_buf] = 0;

            /* Writing the string obtained to the new file */
    fputs(buf, data_temp);
    fputs("\n", data_temp);
}

    /* The string will be inserted at the final of the new file */
if(feof(datos))
{
    fputs(newline, datos_temp);
}

    /* Closing both files */
fclose(data);
fclose(data_temp);

    /* The original file is deleted and replaced with the new file */
remove ("data.txt");
rename ("data_temp.txt", "data.txt");   

My problem is basically in the writing to the original file, to the new file. 我的问题基本上是在将原始文件写入新文件中。 The last line of the original file shows duplicated in the new file . 原始文件的最后一行显示在新文件中重复的文件

In the example given: 在给出的示例中:

data.txt data.txt

 line1
 line2
 line3
 line4
 line5 

The line5 (last line of the original file) is showed twice on the new file , and then is the string to be inserted. 5行(原始文件的最后一行) 在新文件上显示两次 ,然后是要插入的字符串。

data_temp.txt --> renamed later data.txt data_temp.txt- >重命名为更高的data.txt

 line1
 line2
 line3
 line4
 line5 
 line5
 newline

I strongly believe the problem is in the reading of the original file (AKA the while(!feof(data)) loop), on the checking the EOF, the fgets, or the fputs. 我坚信问题出在检查EOF,fgets或fputs时读取原始文件(又称为while(!feof(data))循环)。 Any idea to solve this? 有解决的办法吗?

Correct. 正确。 The problem is in your loop condition. 问题出在您的循环条件中。

feof() is evil because it's often misunderstood. feof()是邪恶的,因为它常常被误解。 feof() doesn't indicate that you are at the end-of-file. feof()并不表示您位于文件末尾。 It only indicates that it has not yet encountered it (that you haven't fetched a byte past the end of the file). 它仅指示它尚未遇到(您尚未从文件末尾获取一个字节)。

You must instead detect within the loop when you encounter EOF(when fgets() returns NULL ) and break out of the loop then. 相反,当遇到EOF时(当fgets()返回NULL ),您必须在循环内进行检测,然后退出循环。

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

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