简体   繁体   English

试图用C语言在文件的每一行末尾写?

[英]Trying to write at end of each line in a File in C Language?

I have been trying from past 3 hours and stuck at one point. 我过去3个小时一直在尝试,一度陷入困境。 I want to add some content after each line in a File using C. 我想使用C在文件的每一行之后添加一些内容。

Here's My Code 这是我的代码

int main(){

FILE *fp;
char c;
int p;

fp = fopen("example1.txt","r+");

if (fp == 0) {
    perror("No File");
    exit(-1);
}

while ( (c = fgetc(fp)) != EOF) {
    if (c != '\n')
        printf("%c",c);
    else {
        //Going back two points is for '\n'
        fseek(fp,-2,SEEK_CUR);
        fprintf(fp,"=2\n");
    }
}


fclose(fp);
return 0;
}

The Input File: 输入文件:

1+1
1+1
1+1

The Desired Output: 所需的输出:

1+1=2
1+1=2
1+1=2

The Real Output: 实际输出:

1+1=2
=2
=2
=2
=2
=2

The Execution gets into Infinite Loop.. 执行进入无限循环。

Let's say you have one line of text in the file. 假设您在文件中有一行文本。

1+1

If you look at the contents byte wise as characters, you'll see: 如果将内容按字节看成字符,则会看到:

+---+---+---+----+
| 1 | + | 1 | \n |
+---+---+---+----+

When you encounter the '\\n' , you go back two characters and print "=2\\n" to the file. 当遇到'\\n' ,您返回两个字符并在文件中打印"=2\\n" After, the contents of the file will be: 之后,文件的内容将为:

+---+---+---+----+
| 1 | = | 2 | \n |
+---+---+---+----+

The call to fprintf does not change the position from where you read the data. fprintf的调用不会更改读取数据的位置。 Hence, the read position is at the second character, '=' , of the file. 因此,读取位置在文件的第二个字符'='处。

The entire process repeats. 重复整个过程。 The contents of the file remain 文件内容保留

+---+---+---+----+
| 1 | = | 2 | \n |
+---+---+---+----+

The output continues to be 输出继续为

=2

after the first line. 第一行之后。

Reading from and writing to the same file is fraught with too many pitfalls. 读取和写入同一文件充满了很多陷阱。 It will be better if you separate them. 如果将它们分开,会更好。

I had faced the same problem. 我曾经遇到过同样的问题。 I got it to work partially.At the desired position, I used ftell to get the current position. 我得到了它的部分工作。在所需的位置,我用ftell获取当前位置。 I subtracted this from the initial file size and stored in remainingSize variable. 我从初始文件大小中减去了这一点,并将其存储在剩余大小变量中。 Using fread I got the chars after this position into a buffer. 使用fread,我将该位置后的字符放入缓冲区。 Then I wrote the desired output at current position.Then I wrote the data from buffer back into the file. 然后在当前位置写入所需的输出,然后将缓冲区中的数据写回到文件中。 This worked for small data, but there was a bug in my code which I couldn't resolve and it led to ftell returning an out of range value. 这适用于小数据,但是我的代码中有一个我无法解决的错误,导致ftell返回超出范围的值。

It could be explained as 可以解释为

if the data is 如果数据是

+---+---+---+----+
| 1 | + | 1 | \n |
+---+---+---+----+
+---+---+---+----+
| 3 | * | 5 | \n |
+---+---+---+----+

let's say size of file is 'x' You go to the point after 1. By ftell, u can get the current pos 'y' You have to read 'x'-'y' chars and bring them into buffer You can write the desired output at current position and then write the contents of buffer back to the file 假设文件的大小为'x'。1到1之后。ftell表示,您可以获得当前位置'y'。您必须读取'x'-'y'字符并将其放入缓冲区中。在当前位置所需的输出,然后将缓冲区的内容写回到文件

+---+---+---+----+
| 1 | + | 1 | = | 2
+---+---+---+----+

Buf =
+---+---+---+----+
| \n | 3 | * | 5 |\n
+---+---+---+----+

I think this solution is inefficient as it involves many reads and writes and may not work on big files. 我认为该解决方案效率低下,因为它涉及许多读取和写入操作,可能不适用于大文件。

You have to rewrite the entire file, other than the first line. 除了第一行,您必须重写整个文件。 Trying to do that in-place to the existing file is actually pretty difficult given arbitrarily long lines in an arbitrarily-large file. 考虑到任意大文件中的任意长行,尝试就地对现有文件执行此操作实际上非常困难。

So it's much, much easier to just write a new file. 因此,编写一个新文件要容易得多。

Something like this (completely lacks any error checking): 这样的事情(完全没有任何错误检查):

// open files
FILE *in = fopen( "infile.txt", "r" );
FILE *out = fopen( "infile.txt.tmp", "w+" );
// getline() variables
char *line = NULL;
size_t len = 0UL;
ssize_t result;

// buffer for output line
// (hope it's long enough...)
char buffer[ 1024 ];
for ( ;; )
{
    // getline() makes this so much easier...
    result = getline( &line, &len, in );
    if ( result == -1 )
    {
        break;
    }
    // strip newline
    char *p = strchr( line, '\n' );
    if ( p != NULL )
    {
        *p = '\0';
    }
    // append "=2" to each line
    snprintf( buffer, sizeof( buffer), "%s%s\n", line, "=2" );
    fputs( buffer, out );
}
fclose( in );
fclose( out );
// assume POSIX rename is available
rename( "infile.txt.tmp", "infile.txt" );

Read the file line-by-line, append the new data to the end of each line, write the new line to a new file , then when done rename() the new file to the old file name. 逐行读取文件,将新数据追加到每行的末尾,将新行写入新文件 ,然后完成后, rename()新文件rename()更改为旧文件名。

Normally, I'd add whitespace between lines to aid in readability, but the extra lines cause a scrollbar to appear on the code, making it less readable. 通常,我会在行之间添加空格以提高可读性,但是多余的行会导致滚动条出现在代码上,从而降低了可读性。

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

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