Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a file with C in Linux?

Tags:

c

linux

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?

The current C code is:

#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);
}
like image 326
Devyn Avatar asked Jan 05 '10 18:01

Devyn


People also ask

Can I write C in Linux?

You can already run C code because Linux systems include the GNU C library ( glibc ). To write and build it, all you need to do is install a compiler, open a text editor, and start coding.

How do you write to a file in Linux?

Write data to a file using the “cat command.” Write data to a file using the “echo command.” Write data to a file using the “printf command.” Write data to file using any ” nano text editor.”

Which command is used for write in file in C?

Writing to a file (fprintf or fputs) Moving to a specific location in a file (fseek, rewind) Closing a file (fclose)


1 Answers

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.

#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 reference has this to say:

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.

like image 193
Chinmay Kanchi Avatar answered Sep 28 '22 09:09

Chinmay Kanchi