Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Linux中用C编写文件?_C_Linux - Fatal编程技术网

如何在Linux中用C编写文件?

如何在Linux中用C编写文件?,c,linux,C,Linux,我想重写Linux的“cp”命令。因此,该程序的工作方式类似于#/a.out originalfile copiedfile。我可以打开文件,创建新文件,但不能写入新文件。什么都没写。原因可能是什么 当前的C代码是: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int mai

我想重写Linux的“cp”命令。因此,该程序的工作方式类似于
#/a.out originalfile copiedfile
。我可以打开文件,创建新文件,但不能写入新文件。什么都没写。原因可能是什么

当前的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);
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*aa[]{
int-fd,fd1;
字符缓冲区[100];
如果(argc!=3){
printf(“用法:./a.out\n”);
返回-1;
}
fd=打开(aa[1],仅限O_rds,S_IRUSR);
如果(fd==-1){
printf(“未找到文件。\n”);
返回-1;
}
fd1=开放(aa[2],O|u CREAT | O|u WRONLY,S|u IRUSR);
如果(fd1!=-1){
printf(“文件已创建。\n”);
}
(三);
while(n=读取(fd,缓冲器,50)){
写入(fd1,缓冲区,n);
printf(“…写入…”\n”);
}
关闭(fd);
关闭(fd1);
}

您必须在与
读取
相同的循环中执行
写入

您需要将读取的()数据写入新文件:

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);
更新:添加了正确的打开


顺便说一句,O_CREAT可以是OR'd(O_CREAT | O_WRONLY)。实际上,您打开的文件句柄太多。只需打开一次。

首先,您编写的代码是不可移植的,即使您让它工作。既然有一种完全独立于平台的方式,为什么还要使用操作系统特有的功能呢?这是一个只使用单个头文件的版本,可以移植到实现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;
}
#包括
int main(int argc,字符**argv)
{
文件*源文件;
文件*destFile;
char-buf[50];
整数单位;
如果(argc!=3)
{
printf(“用法:fcopy source destination\n”);
返回1;
}
sourceFile=fopen(argv[1],“rb”);
destFile=fopen(argv[2],“wb”);
if(sourceFile==NULL)
{
printf(“无法打开源文件\n”);
返回2;
}
如果(destFile==NULL)
{
printf(“无法打开目标文件\n”);
返回3;
}
而(numBytes=fread(buf,1,50,sourceFile))
{
fwrite(buf,1,numBytes,destFile);
}
fclose(源文件);
fclose(destFile);
返回0;
}
编辑:编辑有这样一句话:

一般来说,你应该坚持 使用流而不是文件 描述符,除非有 你想做的具体操作是什么 只能在文件描述符上执行。 如果你是一名初级程序员 我们不确定要使用什么函数 建议你把注意力集中在 格式化输入函数(参见 格式化输入)和格式化输出 函数(请参见格式化输出)

如果您关心可移植性 将您的程序的 GNU,你也应该知道 文件描述符的可移植性较差 就像溪流一样。你可以期待任何系统 运行isoc以支持流,但是 非GNU系统可能不支持文件 描述符,或仅限于 实现GNU的一个子集 对文件进行操作的函数 描述符。文件的大部分 GNU中的描述符函数 库包含在POSIX.1中 然而,标准


您必须使用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;
}
#包括
#包括
#包括
#包括
#包括
int main(){
ssize_t nrd;
int-fd;
int-fd1;
char*buffer=malloc(100*sizeof(char));
fd=打开(“bli.txt”,仅限ordu);
fd1=打开(“bla.txt”,O|u CREAT | O|u WRONLY,S|u IRUSR | S|u IWUSR);
而(nrd=read(fd,buffer,sizeof(buffer))){
写入(fd1、缓冲区、nrd);
}   
关闭(fd);
关闭(fd1);
自由(缓冲);
返回0;
}
确保rad文件存在并包含某些内容。
虽然不完美,但很有效。

您好,谢谢您的回复。我用新代码编辑了我的帖子。我照你说的做了,但还是不起作用。未将任何内容写入新文件。请帮帮我!对不起,我还是不工作。我在while循环中放入了一些字符串,它表明while循环只工作了一次(是的,我的代码并不完美,但它朝着正确的方向前进了一大步。继续玩下去。就像Jonathan在你文章的评论中说的,你对代码有很多问题。你需要检查read()返回值是否为==0。你可能还需要使用sizeof(缓冲区)而不是50。但我只是想给你一个有用的提示,而不是带走自己找到答案的乐趣/挫折。我同意你的观点,但有时你知道,我们没有足够的时间自己找到答案。事实上,我总是自己找到解决方案,但对于这个问题,我明天要考试。我已经花了h我们解决这个问题。无论如何谢谢你的帮助!@Devyn通常当有人没有时间完成一项任务时,他们会付钱给其他人来完成。我希望当我有太多的工作要做时,我能得到免费编程。:)未使用的变量fd2;别忘了检查一下,写下你所期望的一切;不使用指定作为条件(使用GCC-墙);从main()返回一个值更好,尽管C99(错误地)允许您离开main()返回。您的fd1错误消息不正确;当它在fd1上失败时,您不会退出。您可以在读取中使用sizeof(buffer),而不是50,50是缓冲区大小的一半。错误通常写在stderr中,而不是stdout中。如果读取失败(负值),而不是什么都不返回,那么您的循环就会出现问题。只需将您的代码复制并粘贴到我的机器上,用gcc编译,就可以正常工作。哦,奇迹发生了。由于您的评论,我尝试将文件移动到另一个位置并再次测试。它工作!!!非常感谢。如果您在目标文件打开后更改了条件检查,以便在打印(当然是打印到stderr)errno和错误消息失败时,您可以更好地了解不写入任何内容的确切原因。良好的C编程实践是始终检查