Solved

file reading and writing

Posted on 2004-04-04
8
180 Views
Last Modified: 2010-04-22
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
Comment
Question by:achille67
8 Comments
 
LVL 8

Expert Comment

by:manish_regmi
ID: 10754684
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
 

Author Comment

by:achille67
ID: 10754716
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
 

Author Comment

by:achille67
ID: 10754722
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
 
LVL 8

Expert Comment

by:manish_regmi
ID: 10755304
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 8

Expert Comment

by:manish_regmi
ID: 10755307
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10755417
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
 
LVL 8

Accepted Solution

by:
da99rmd earned 50 total points
ID: 10755574
>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
 

Author Comment

by:achille67
ID: 10756377
Thanks Rob, you can read people's mind. That is the dam loop!!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now