如何在没有平台依赖性的情况下复制C中的文件?

时间:2023-01-13 21:00:13

It looks like this question is pretty simple but I can't find the clear solution for copying files in C without platform dependency.

看起来这个问题非常简单,但我无法找到在没有平台依赖性的情况下在C中复制文件的明确解决方案。

I used a system() call in my open source project for creating a directory, copying files and run external programs. It works very well in Mac OS X and other Unix-ish systems, but it fails on Windows. The problem was:

我在我的开源项目中使用了一个system()调用来创建目录,复制文件和运行外部程序。它在Mac OS X和其他Unix-ish系统中运行良好,但在Windows上失败。问题是:

system( "cp a.txt destination/b.txt" );
  • Windows uses backslashes for path separator. (vs slashes in Unix-ish)
  • Windows使用反斜杠作为路径分隔符。 (vs Unix中的斜线)

  • Windows uses 'copy' for the internal copy command. (vs cp in Unix-ish)
  • Windows使用'copy'作为内部复制命令。 (vs Unix中的cp)

How can I write a copying code without dependency?

如何编写没有依赖的复制代码?

( Actually, I wrote macros to solve this problems, but it's not cool. http://code.google.com/p/npk/source/browse/trunk/npk/cli/tests/testutil.h, L22-56 )

(实际上,我编写了宏来解决这个问题,但这并不酷.http://code.google.com/p/npk/source/browse/trunk/npk/cli/tests/testutil.h,L22-56)

3 个解决方案

#1


6  

You need to use the C standard library functions in stdio.h.

您需要在stdio.h中使用C标准库函数。

In particular, fopen, fread, fwrite, and fclose will be sufficient.

特别是,fopen,fread,fwrite和fclose就足够了。

Be sure to include the b ("binary") option in the flags to fopen.

请务必在标志中包含b(“binary”)选项以进行fopen。

[edit]

Unfortunately, the file names themselves (forward-slashes vs. back-slashes) are still platform dependent. So you will need some sort of #ifdef or similar to deal with that.

不幸的是,文件名本身(正斜杠与反斜杠)仍然依赖于平台。所以你需要某种#ifdef或类似的来处理它。

Or you can use a cross-platform toolkit.

或者您可以使用跨平台工具包。

#2


9  

The system() function is a lot more trouble than it's worth; it invokes the shell in a seperate proccess, and should usually be avoided.

system()函数比它的价值更麻烦;它在一个单独的进程中调用shell,通常应该避免。

Instead fopen() a.txt and dest/b.text, and use getc()/putc() to do the copying (because the standard library is more likely to do page-aligned buffering than you)

而不是fopen()a.txt和dest / b.text,并使用getc()/ putc()进行复制(因为标准库更可能进行页面对齐缓冲而不是你)

FILE *src = fopen("a.txt", "rb");
FILE *dst = fopen("dest/b.txt", "wb");
int i;
for (i = getc(src); i != EOF; i = getc(src))
{
    putc(i, dst);
}
fclose(dst);
fclose(src);

#3


0  

Use the standard C library stdio.h. First open input file for reading using fopen(inputFilename, "rb") and open output file for writing using fopen(outputFilename, "wb"), copy the content using fread and fwrire. Then close both files using fclose.

使用标准C库stdio.h。首先打开输入文件,使用fopen(inputFilename,“rb”)进行读取,打开输出文件,使用fopen(outputFilename,“wb”)进行写入,使用fread和fwrire复制内容。然后使用fclose关闭这两个文件。

#1


6  

You need to use the C standard library functions in stdio.h.

您需要在stdio.h中使用C标准库函数。

In particular, fopen, fread, fwrite, and fclose will be sufficient.

特别是,fopen,fread,fwrite和fclose就足够了。

Be sure to include the b ("binary") option in the flags to fopen.

请务必在标志中包含b(“binary”)选项以进行fopen。

[edit]

Unfortunately, the file names themselves (forward-slashes vs. back-slashes) are still platform dependent. So you will need some sort of #ifdef or similar to deal with that.

不幸的是,文件名本身(正斜杠与反斜杠)仍然依赖于平台。所以你需要某种#ifdef或类似的来处理它。

Or you can use a cross-platform toolkit.

或者您可以使用跨平台工具包。

#2


9  

The system() function is a lot more trouble than it's worth; it invokes the shell in a seperate proccess, and should usually be avoided.

system()函数比它的价值更麻烦;它在一个单独的进程中调用shell,通常应该避免。

Instead fopen() a.txt and dest/b.text, and use getc()/putc() to do the copying (because the standard library is more likely to do page-aligned buffering than you)

而不是fopen()a.txt和dest / b.text,并使用getc()/ putc()进行复制(因为标准库更可能进行页面对齐缓冲而不是你)

FILE *src = fopen("a.txt", "rb");
FILE *dst = fopen("dest/b.txt", "wb");
int i;
for (i = getc(src); i != EOF; i = getc(src))
{
    putc(i, dst);
}
fclose(dst);
fclose(src);

#3


0  

Use the standard C library stdio.h. First open input file for reading using fopen(inputFilename, "rb") and open output file for writing using fopen(outputFilename, "wb"), copy the content using fread and fwrire. Then close both files using fclose.

使用标准C库stdio.h。首先打开输入文件,使用fopen(inputFilename,“rb”)进行读取,打开输出文件,使用fopen(outputFilename,“wb”)进行写入,使用fread和fwrire复制内容。然后使用fclose关闭这两个文件。