如何在 Linux 中用 C 编写文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2008267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a file with C in Linux?
提问by Devyn
I want to rewrite the "cp" command of Linux. So this program will work like #./a.out originalfile copiedfile
. I can open the file, create new file but can't write the new file. Nothing is written. What could be the reason?
我想重写Linux的“cp”命令。所以这个程序会像#./a.out originalfile copiedfile
. 我可以打开文件,创建新文件,但不能写入新文件。什么都没写。可能是什么原因?
The current C code is:
当前的 C 代码是:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char *aa[]){
int fd,fd1;
char buffer[100];
if(argc!=3){
printf("Usage : ./a.out <original> <copy> \n");
return -1;
}
fd=open(aa[1],O_RDONLY,S_IRUSR);
if(fd==-1){
printf("file not found.\n");
return -1;
}
fd1=open(aa[2],O_CREAT | O_WRONLY,S_IRUSR);
if(fd1!=-1){
printf("file is created.\n");
}
ssize_t n;
while(n=read(fd,buffer,50)){
write(fd1,buffer,n);
printf("..writing..\n");
}
close(fd);
close(fd1);
}
采纳答案by dlamotte
You need to write() the read() data into the new file:
您需要将 read() 数据写入()到新文件中:
ssize_t nrd;
int fd;
int fd1;
fd = open(aa[1], O_RDONLY);
fd1 = open(aa[2], O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
while (nrd = read(fd,buffer,50)) {
write(fd1,buffer,nrd);
}
close(fd);
close(fd1);
Update: added the proper opens...
更新:添加了正确的打开...
Btw, the O_CREAT can be OR'd (O_CREAT | O_WRONLY). You are actually opening too many file handles. Just do the open once.
顺便说一句,O_CREAT 可以被 OR'd (O_CREAT | O_WRONLY)。您实际上打开了太多文件句柄。只需打开一次。
回答by Nikolai Fetissov
You have to do write
in the same loop as read
.
您必须write
在与read
.
回答by Chinmay Kanchi
First of all, the code you wrote isn't portable, even if you get it to work. Why use OS-specific functions when there is a perfectly platform-independent way of doing it? Here's a version that uses just a single header file and is portable to any platform that implements the C standard library.
首先,您编写的代码不可移植,即使您可以使用它。当有一种完全独立于平台的方法时,为什么要使用特定于操作系统的函数?这是一个仅使用单个头文件并且可移植到任何实现 C 标准库的平台的版本。
#include <stdio.h>
int main(int argc, char **argv)
{
FILE* sourceFile;
FILE* destFile;
char buf[50];
int numBytes;
if(argc!=3)
{
printf("Usage: fcopy source destination\n");
return 1;
}
sourceFile = fopen(argv[1], "rb");
destFile = fopen(argv[2], "wb");
if(sourceFile==NULL)
{
printf("Could not open source file\n");
return 2;
}
if(destFile==NULL)
{
printf("Could not open destination file\n");
return 3;
}
while(numBytes=fread(buf, 1, 50, sourceFile))
{
fwrite(buf, 1, numBytes, destFile);
}
fclose(sourceFile);
fclose(destFile);
return 0;
}
EDIT: The glibc referencehas this to say:
编辑:glibc 参考有这样的说法:
In general, you should stick with using streams rather than file descriptors, unless there is some specific operation you want to do that can only be done on a file descriptor. If you are a beginning programmer and aren't sure what functions to use, we suggest that you concentrate on the formatted input functions (see Formatted Input) and formatted output functions (see Formatted Output).
If you are concerned about portability of your programs to systems other than GNU, you should also be aware that file descriptors are not as portable as streams. You can expect any system running ISO C to support streams, but non-GNU systems may not support file descriptors at all, or may only implement a subset of the GNU functions that operate on file descriptors. Most of the file descriptor functions in the GNU library are included in the POSIX.1 standard, however.
一般来说,你应该坚持使用流而不是文件描述符,除非你想要做一些只能在文件描述符上完成的特定操作。如果您是初学者并且不确定要使用哪些函数,我们建议您专注于格式化输入函数(参见格式化输入)和格式化输出函数(参见格式化输出)。
如果您担心程序在 GNU 以外的系统上的可移植性,您还应该意识到文件描述符不像流那样可移植。您可以期望任何运行 ISO C 的系统都支持流,但非 GNU 系统可能根本不支持文件描述符,或者可能仅实现对文件描述符进行操作的 GNU 函数的子集。然而,GNU 库中的大多数文件描述符函数都包含在 POSIX.1 标准中。
回答by Patryk Krzyzanski
You have to allocate the buffer with mallock, and give the read write the pointer to it.
您必须使用 mallock 分配缓冲区,并为读写提供指向它的指针。
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
ssize_t nrd;
int fd;
int fd1;
char* buffer = malloc(100*sizeof(char));
fd = open("bli.txt", O_RDONLY);
fd1 = open("bla.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
while (nrd = read(fd,buffer,sizeof(buffer))) {
write(fd1,buffer,nrd);
}
close(fd);
close(fd1);
free(buffer);
return 0;
}
Make sure that the rad file exists and contains something. It's not perfect but it works.
确保 rad 文件存在并包含某些内容。它并不完美,但它有效。