C中的文件操作 [英] File operation in c

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

问题描述

我想创建一个二进制文件的副本.以下代码显示了我的操作方法:

I want to create a copy of a binary file. The following code shows how I do this:

#include "stdafx.h"
 
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
 
int main(int argc, char *argv[])
{
    FILE *f1,*f2;
    f1=fopen("D:\\Users\\Dharmaraj\\RESUME.doc","rb");
    f2=fopen("1.doc","wb");
    char temp;
    while(1)
    {
        temp=fgetc(f1);
        if(temp==EOF)
            break;
        fputc(temp,f2);
    }
    fclose(f1);
    fclose(f2);
    return 0;
}

不适用于.doc和.exe文件.

It does not work for .doc and .exe files.

推荐答案

而不是一次使用fgetc/fputc来读写字符,我会考虑使用fread/fwrite读取和写入整个文件.
Rather than using fgetc/fputc to read and write a character at a time, I would look at using fread/fwrite to read and write the whole file.


如果不向我们显示代码,我们可能将无法为您提供很多帮助. ..

我想问题是您没有以二进制方式打开文件...

无论如何,请查看此链接 [
Without showing us code... we probably won''t be able to give you a lot of help...

I guess that the problem is that you''ve not opened the file in a binary way...

anyway, take a look at this link[^]... a simple goolge search has thrown this as the first result for me...

Let''s see if this works for you...


由于您包括windows.h,我相信Windows API copyfile函数在这里就足够了.

http://msdn.microsoft.com/en-us/library/aa363851%28v = vs.85%29.aspx [ ^ ]
Since you are including windows.h I believe the windows API copyfile function would suffice here.

http://msdn.microsoft.com/en-us/library/aa363851%28v=vs.85%29.aspx[^]


这篇关于C中的文件操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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