简体   繁体   English

C:如何从一个文件中读取一行并将其追加到另一个文件?

[英]C: How to read a line from one file, and append it to another?

Suppose I have a file called greeting.txt that has the following contents: 假设我有一个名为greeting.txt的文件,其内容如下:

Hello
World
How
Are
You

How do I read each line and then append it to another file in C? 如何读取每一行,然后将其附加到C中的另一个文件中? So far, I have this: 到目前为止,我有这个:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE *infile;
    FILE *outfile;

    infile = fopen("greeting.txt", "r");
    outfile = fopen("greeting2.txt", "w");

    //Trying to figure out how to do the rest of the code

    return 0;
}

The expected result is that there will be a file named greeting2.txt with the exact same contents as greeting.txt. 预期的结果是,将有一个名为greeting2.txt的文件,其内容与greeting.txt完全相同。

My plan is to use a WHILE loop to cycle through each line of greeting.txt and appending each line to greeting2.txt, but I'm not quite sure how to read the line, then write. 我的计划是使用WHILE循环遍历greeting.txt的每一行并将每一行追加到greeting2.txt,但是我不太确定如何读取该行然后编写。

I'm new to C, and I'm having some trouble figuring this out. 我是C语言的新手,在弄清楚这一点时遇到了一些麻烦。 Any help is very much appreciated. 很感谢任何形式的帮助。

Here's an example: 这是一个例子:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 512

int main(int argc, char *argv[])
{
  FILE *file, *file2;
  char line[MAX];

  if (argc != 4)
  {
    printf("You must enter: ./replace old-string new-string file-name\n")
    exit(1);
  }

  //Here's what you're interested in starts....
  file = fopen(argv[3], "r");
  file2 = fopen("temp", "w");
  while (fgets(line,sizeof(line),file) != NULL);
  {
    /*Write the line */
    fputs(line, file2);
    printf(line);

  }
  fclose (file);
  fclose (file2);
  //Here is where it ends....

  return 0;
}

Source: 资源:

http://cboard.cprogramming.com/c-programming/82955-c-reading-one-file-write-another-problem.html http://cboard.cprogramming.com/c-programming/82955-c-reading-one-file-write-another-problem.html

Note: The source has one small error which I fixed here. 注意:来源有一个小错误,我在这里已修复。

If you want to copy the entire content from one file to other then you can read file byte by byte and write to other. 如果要将整个内容从一个文件复制到另一个文件,则可以逐字节读取文件并写入其他文件。 This could be done with getc() and putc(). 这可以用getc()和putc()完成。 If you want to do it by copying entire line you should make a char buffer[] with some length and then use gets() to read char from file and store it to buffer. 如果要通过复制整行来做到这一点,则应制作一个具有一定长度的char buffer [],然后使用gets()从文件中读取char并将其存储到缓冲区。 All functions have versions which works with files. 所有功能都有与文件一起使用的版本。 I mean that where is fgetc(), fgetc() fgets(). 我的意思是fgetc(),fgetc()fgets()在哪里。 For futher details you can search full description in google. 有关更多详细信息,您可以在Google中搜索完整的说明。

Helpful calls: fread , fseek , fwrite 有用的电话: freadfseekfwrite

//adjust buffer as appropriate
#define BUFFER_SIZE 1024
char* buffer = malloc(BUFFER_SIZE);//allocate the temp space between reading and writing
fseek(outfile, 0, SEEK_END);//move the write head to the end of the file
size_t bytesRead = 0;
while(bytesRead = fread((void*)buffer, 1, BUFFER_SIZE, infile))//read in as long as there's data
{
    fwrite(buffer, 1, BUFFER_SIZE, outfile);
}

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

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