charlie10719
charlie10719

Reputation: 33

How to write() to a file byte by byte or in chunks in C

I'm trying to write byte by byte, 2 bytes, 4 bytes etc in chunks to a file. I currently have this code however am stuck.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>


int main(int argc, char* argv[]){

    char buf[1];

    //creating output file
    int outfile = open(argv[1], O_CREAT | O_APPEND | O_RDWR, 0666);

    int infile = open(argv[2], O_RDONLY);

    fread = read(infile, buf, 1);
    printf("%s\n", buf);
    write(outfile);
    close(infile);
    close(outfile)

}

Upvotes: 0

Views: 1747

Answers (2)

Jarvis__-_-__
Jarvis__-_-__

Reputation: 242

First of all here I can't see where you had declared fread variable,

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>


int main(int argc, char* argv[]){

    char buf[1];

    //creating output file
    int outfile = open(argv[1], O_CREAT | O_APPEND | O_RDWR, 0666);

    int infile = open(argv[2], O_RDONLY);

    fread = read(infile, buf, 1); // here fread var !!?
    printf("%s\n", buf);
    write(outfile);
    close(infile);
    close(outfile)

}

and this is not the way to write in a file, if you succeeded in compilation so according to this you will be able to write only one byte. You have to implement a loop either for loop or while loop in order to write all the byte from one file to another, if I am getting you correctly.

I am assuming here that you are trying to write data byte by byte from one file to another, So with that assumption here is the working code with some changes in your program..

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>


int main(int argc, char* argv[]){
    int fread =0;
    char buf[1]; 

    //creating output file
    int outfile = open(argv[1], O_CREAT | O_APPEND | O_RDWR, 0666);

    int infile = open(argv[2], O_RDONLY);
    
    while(fread = read(infile, buf, 1)){ //change here number of bytes you want to read at a time and don't forget to change the buffer size too :)
    printf("%s\n", buf);
    write(outfile, buf, fread);
   }
    close(infile);
    close(outfile)

}

Upvotes: 0

4386427
4386427

Reputation: 44274

In your current code you read 1 byte and then print it using %s. That won't work. %s is for printing strings and strings must be zero-terminated but you won't get a zero-terminated string when using read (further the array buf can only hold an empty string). I assume you want to print the data that you read so do like:

printf("%c\n", buf[0]);

For reading the whole file, you need a loop:

while((fread = read(infile, buf, 1)) > 0)
{
    printf("%c\n", buf[0]);
}

Regarding block size you can only set a maximum but read may return fewer byte. So do like:

#define MAX_BYTES_IN_A_SINGLE_READ 4

to set the max block size and use it like:

char buf[MAX_BYTES_IN_A_SINGLE_READ];
ssize_t fread;
while((fread = read(infile, buf, MAX_BYTES_IN_A_SINGLE_READ)) > 0)
{
    for (ssize_t i = 0; i < fread; ++i)
    {
        printf("%c\n", buf[i]);
    }
}

Upvotes: 2

Related Questions