Question

I'm trying to use low level functions in C and wanting to read from the STDIN and store that information in a file.

int dash, c;
char buffer[1024];
if((dash = creat("file.txt", S_IRWXU)) < 0)
    perror("creat error");
while ((c = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) {
    if (write(dash, buffer, c) != c)
       perror("write error");

I having a problem understanding how I can access 'file.txt' to read it to either print to the screen or store to another file. Would I just use 'read("file.txt", buffer, sizeof[buffer])'?

EDIT Now after creating "file.txt" I want to open another file, lets say file1 (argv[3]) and dump "file.txt" into file1 (agrv[3]). Would this work?

fd = open(argv[3], O_RDWR); //open 3rd arg for writing
fd_2 = open("file.txt", O_RDWR); //open created file
do {
     n = read(fd_2, buffer, sizeof(buffer));
     if (n < 0)
        perror("read error argv[2]"); //greater 0=succesful
     write(STDOUT_FILENO, buffer, n); // this is where I'm stuck
    } while (n == sizeof(buffer));
close(fd);

I have both files open now but can't figure out how to write "file.txt" into argv[3].

Was it helpful?

Solution 2

Try the code. give the input filename as second argument.

#include<stdio.h>
#include<fcntl.h>

#define MAX_BUF_SIZE 512

int main(int argc, char **argv)
{
        int fd;
        int count,value;
        char buf[MAX_BUF_SIZE];

        if(argc < 2)
                fprintf(stderr, "Usage: a.out filename\n");
         else
        {
                 fd = open(argv[1], O_RDONLY);
                 if(fd < 0 )
                        fprintf(stderr, "Error In Opening File\n");
                else
                {
                        fprintf(stdout, "Enter No of bytes to read from file %s\n",argv[1]);
                        fscanf(stdin,"%d",&value);
                        printf("value %d\n",value);

                        count = read(fd,buf,value);
                        buf[count]='\0';

                        if(count <=0)
                                fprintf(stderr, "Error In Reading from file\n");
                        else if(count < value)
                                fprintf(stdout, "Partial read data is %s\n",buf);
                        else if(count == value)
                                fprintf(stdout, "Data is: \n%s\n",buf);
                        else
                                fprintf(stderr,"Error in read.");

                close(fd);
                }
        }
        return 0;
}

The '\0' is added to buffer to avoid any garbage value with the output.

Note: fopen,fread are library fn where open, read are system call

OTHER TIPS

Use open() to get the file descriptor:

int fd = open("file.txt", O_RDWR);

Then you can use read() to read from this file descriptor just like STDIN_FILENO.

c = read(fd, buffer, sizeof(buffer));

Make sure to check for the return value of both functions in real code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top