在c中复制文本文件的内容 [英] copying contents of a text file in c

查看:80
本文介绍了在c中复制文本文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读一个文本文件并将其内容传输到c中的另一个文本文件,这是我的代码:



I want to read a text file and transfer it''s contents to another text file in c, Here is my code:

   char buffer[100];

   FILE*  rfile=fopen ("myfile.txt","r+");
   if(rfile==NULL)
  {
    printf("couldn''t open File...\n");
  }


  fseek(rfile, 0, SEEK_END);
  size_t file_size = ftell(rfile);
  printf("%d\n",file_size);
  fseek(rfile,0,SEEK_SET);
  fread(buffer,file_size,1,rfile);


  FILE* pFile = fopen ( "newfile.txt" , "w+" );
  fwrite (buffer , 1 ,sizeof(buffer) , pFile );
  fclose(rfile);
  fclose (pFile);
  return 0;
}





我面临的问题是接收文件中不必要数据的出现,我尝试使用sizeof(buffer)和file_size的fwrite函数,在第一种情况下它显示更多无用字符,而在第二种情况下,无用字符的数量只有3(我检查了fread的值和它是零),如果有人指出我的错误告诉我如何摆脱这些无用的角色,我真的很感激...



the problem that I am facing is the appearence of unnecessary data in the receiving file, I tried the fwrite function with both "sizeof(buffer)" and "file_size",In the first case it is displaying greater number of useless characters while in the second case the number of useless characters is only 3(I checked the value of fread and it is "zero"),I would really appreciate if someone pointed out my mistake and told me how to get rid of these useless characters...

推荐答案

第一个您需要做的是阅读有关fread的文档: http://www.cplusplus.com/reference/cstdio/fread / [ ^ ]非常清楚你需要做什么,甚至包括一个例子。

因为你只分配一个100字节的缓冲区(请不要直接使用数字,#define它们为常量,所以你只在一个地方设置/更改它们)你不能在不使用循环的情况下读取大于100字节的文件。但是在这个阶段,这是一个读者练习。

fread的参数很简单:

1)指向要接收的缓冲区的第一个元素的指针数据 - 你有这个。 :thumbsup:

2)要读取的元素的大小 - 您可能没有这个,因为您将其设置为刚刚打开的流的当前位置,然后移动可能是文件的结尾...

3)要读取的此类元素的数量 - 您将其设置为1.

4)文件流:thumbsup:



当然,它不会读取任何字节 - 因为你在开始阅读它之前将它定位在teh文件的末尾...



相反,根本不寻求,并使用fread来读取缓冲区的大小:

The first thing you need to do is read the documentation on fread: http://www.cplusplus.com/reference/cstdio/fread/[^] which is pretty clear on what you have to do and even includes an example.
Since you are only allocating a buffer of 100 bytes (and please don''t use numbers directly, #define them to a constant so you only set / change them in one place) you cannot read files larger than 100 bytes without using a loop as well. But that is an "exercise for the reader" at this stage.
The parameters to fread are simple:
1) A pointer to the first element of a buffer to receive the data - you have this. :thumbsup:
2) The size of the element to be read - you probably don''t have this, as you are setting it to the current position of the stream you just opened, and then moved to probably the end of the file...
3) The number of such elements to read - you set this to one.
4) The file stream :thumbsup:

But of course, it will read no bytes - because you positioned it at the end of teh file before you started reading it...

Instead, don''t seek at all, and use fread to read the size of the buffer:
size_t bytesRead = fread(buffer, 1, 100, rfile);
...
fwrite(buffer, 1, bytesRead, pFile);

仅使用我建议的常量数字100.

并检查bytesRead - 如果它是缓冲区的大小,那么文件还没有完成,所以你需要阅读更多。

Only use the constant I suggested instead of the number 100.
And check bytesRead - if it is the size of your buffer, then the file isn''t finished, so you need to read some more.


基本上,您只是在部分填充时写入完整缓冲区。但是你的代码有几个缺点,因此我正在尝试重写它



Basically you are writing the full buffer while it is only partially filled. However your code has several flaws, hence I''m trying to rewrite it

char * buffer;
FILE*  rfile=fopen ("myfile.txt","r");
if(rfile == NULL)
{
  printf("couldn't open File...\n");
  // here you HAVE TO exit, return or doing whatever appropriate
}
size_t file_size, read_size;
fseek(rfile, 0, SEEK_END);
size_t file_size = ftell(rfile);
fseek(rfile, 0, SEEK_SET);
file_size -= ftell(rfile);
printf("%d\n",file_size);
buffer = (char*) malloc(file_size);
if ( !buffer)
{
  // close the file and exit (or whatever appropriate)
}
read_size = fread(buffer,file_size,1,rfile);

FILE* wfile = fopen ( "newfile.txt" , "w" );
fwrite (buffer, 1, read_size , wfile );
fclose(wfile);
free(buffer);
fclose (rfile);
return 0;





注意,为了简洁起见,我仍然缺少一些错误检查。



note, for the sake of brevity, I''m still missing some error checking.


您分配了一个100字节的固定大小的缓冲区。如果你的文件较长,你将超出缓冲区结束,这将产生不确定的结果,甚至可能导致你的程序崩溃。



另外:你应该只写尽可能多的数据你已阅读而不是缓冲区大小。
You allocated a fixed size buffer of 100 bytes. If your file is longer you will overrun the buffer end and that will produce undefined results and may even crash your program.

Also: You should only write as much data as you have read and not the buffer size.


这篇关于在c中复制文本文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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