Solved

file reading and writing

Posted on 2004-04-04
8
181 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to pass an array to a shell script? 5 169
making an rpm that just copies the entire project 2 48
how does yum update work 2 48
Error while starting init script 2 93
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

16 Experts available now in Live!

Get 1:1 Help Now