做与fgets /时的fputs最后一行重复 [英] Last line repeats when doing fgets/fputs

查看:256
本文介绍了做与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

 line1
 line2
 line3
 line4
 line5 

将成为,用字符串换行插入:

will become, with the insertion of the string "newline":

data_temp.txt - >更名后的的data.txt

data_temp.txt --> renamed later data.txt

 line1
 line2
 line3
 line4
 line5 
 newline

通过这个目的,我有以下的code:

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");   

我的问题基本上是在写作到原始文件,新文件。 原始文件的最后一行显示复制到新文件

在给定的例子:

的data.txt

 line1
 line2
 line3
 line4
 line5 

LINE5 (原始文件的最后一行)的两次表现的新文件,然后是要插入的字符串。

The line5 (last line of the original file) is showed twice on the new file, and then is the string to be inserted.

data_temp.txt - >更名后的的data.txt

data_temp.txt --> renamed later data.txt

 line1
 line2
 line3
 line4
 line5 
 line5
 newline

我坚信这个问题是在原文件(又名而(!的feof(数据))循环)的读取,对检查EOF时,与fgets或的fputs。任何想法解决这个?

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. Any idea to solve this?

推荐答案

正确的。问题是在循环状态。

Correct. The problem is in your loop condition.

的feof()是邪恶的,因为它经常被误解。 的feof()并不表示你的的最终的文件。它只是表明还没有遇到过(你没有获取过去文件的末尾一个字节)。

feof() is evil because it's often misunderstood. feof() doesn't indicate that you are at the end-of-file. It only indicates that it has not yet encountered it (that you haven't fetched a byte past the end of the file).

您必须改为内循环检测,当你遇到EOF(当与fgets()收益 NULL ),并突破圈外则。

You must instead detect within the loop when you encounter EOF(when fgets() returns NULL) and break out of the loop then.

这篇关于做与fgets /时的fputs最后一行重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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