Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

file reading and writing

how can i use a simple while loop to read from a buffer to a  file &&
from a file to a file in C
0
achille67
Asked:
achille67
1 Solution
 
manish_regmiCommented:
hi,
 If you opened the file using open, you can use read() and write().
ssize_t read(int fd, void *buf, size_t count);

ssize_t write(int fd, const void *buf, size_t count);
write to a file.

regards manish
0
 
achille67Author Commented:
thanks, i know i should use
ssize_t read(int fd, void *buf, size_t count);

ssize_t write(int fd, const void *buf, size_t count);
but i was sure how i should call them inside another function
0
 
achille67Author Commented:
thanks, i know i should use
ssize_t read(int fd, void *buf, size_t count);

ssize_t write(int fd, const void *buf, size_t count);
but i wasn't sure how i should call them inside another function
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
manish_regmiCommented:
hi,
 read() function reads the data from file to memory.
fd is the descriptor of the file you get by open.
buf is the buffer used to store the data.
count is the no of bytes to read.

eg

char buf[512];
int fd;
fd=open("/xtz", O_RDWR);
read(fd, &buf, 512);


this will copy 512 bytes of data from the file /xtz to buf.

similarly.

write(fd, &buf, 512) will write the data stored in buf to the file.

regards manish


0
 
manish_regmiCommented:
hi,
 read() function reads the data from file to memory.
fd is the descriptor of the file you get by open.
buf is the buffer used to store the data.
count is the no of bytes to read.

eg

char buf[512];
int fd;
fd=open("/xtz", O_RDWR);
read(fd, &buf, 512);


this will copy 512 bytes of data from the file /xtz to buf.

similarly.

write(fd, &buf, 512) will write the data stored in buf to the file.

regards manish


0
 
stefan73Commented:
Hi achille67,
> but i was sure how i should call them inside another function
declare the file descriptor in as global or pass it as argument to a function, like:

void write_single_block(int fd){
    write(fd,your_buffer,your_len);
    [...]
}

Cheers,
Stefan
0
 
da99rmdCommented:
>how can i use a simple while loop to read from a buffer to a  file
What kind of buffer ?

here is a loop that does what i think you want.
# Reads one file and write the contents to anothe.
void read_write(char *infile, char *outfile)
{
char buf[1024];
int infd,outfd;
infd=open(infile, O_RDONLY);
outfd=open(outfile.O_CREAT);
while(read(infd, &buf, 1024)) {
    write(outfd,&buff,1024);
    }
close(outfd);
close(infd);
}

/Rob
0
 
achille67Author Commented:
Thanks Rob, you can read people's mind. That is the dam loop!!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now