Solved

file reading and writing

Posted on 2004-04-04
8
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

761 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