Home > Archive > Unix Programming > September 2005 > reading from a file with read(... void * buffer...)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
reading from a file with read(... void * buffer...)
|
|
| ferbar 2005-09-25, 6:59 pm |
| Hi,
I'm writing a program comparing file access patterns. I'm starting with
'read' on a 3MB file.
The function that reads from the file is the following:
void read1test(int fd, void * buffer) {
ssize_t bytesr;
bytesr = read(fd, buffer, BUFFSIZE1);
if (bytesr < 0) {
perror("read:");
exit(bytesr);
}
}
Using Eclipse+CDT, I'm debugging the code, and when the execution is at
the bytesr = read() line, I am not able to see what is stored in the
'buffer' variable.
Below you can see part of the code.
Thanks in advance,
FBM
________________________________________
__________________________
#define BUFFSIZE1 1024
#define FOR_READING 0 //(of no use)
#include <fcntl.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
double now(void) {
struct timeval t;
gettimeofday(&t,NULL);
return ((double)t.tv_sec + (double)t.tv_usec/1000000.0);
}
int openRtest(char *pathnameX) {
int fd;
fd = open(pathnameX, O_RDONLY);
if (fd < 0) {
perror("desc:");
exit(fd);
}
return fd;
}
int openWtest(char *pathnameX) {
int fd;
fd = open(pathnameX, O_RDWR|O_CREAT|S_IRUSR|S_IWUSR);
if (fd < 0) {
perror("desc:");
exit(fd);
}
return fd;
}
void read1test(int fd, void * buffer) {
ssize_t bytesr;
bytesr = read(fd, buffer, BUFFSIZE1);
if (bytesr < 0) {
perror("read:");
exit(bytesr);
}
}
void ls 1test(int fd, off_t offset, int ls action, void * buffer,
int s curr) {
int currpos, nextposition;
ssize_t bytesr;
currpos = ls (fd, 0, s curr);
if (currpos == -1) {
perror("ls :");
exit(currpos);
}
switch (ls action) {
case 0:
while (bytesr != 0) {
bytesr = read(fd, buffer, BUFFSIZE1);
if (bytesr < 0) {
perror("read:");
exit(bytesr);
}
nextposition = ls (fd, offset, SEEK_CUR);
}
}
}
void write1test(int fd, void *buffer) {
ssize_t bytesw;
size_t nbytes;
bytesw = write(fd, buffer, nbytes);
if (bytesw < 0) {
perror("write:");
exit(bytesw);
}
}
int main(int argc, char** argv) {
int fdo1, fdo2, fdo3, initposition;
double t;
char * pathname1, * pathname2;
void * bufread , * bufread2;
char outputpathname[] = "/tmp/output1.txt";
initposition = 0;
bufread = (void*)malloc(BUFFSIZE1*sizeof(void));
bufread2 = (void*)malloc(BUFFSIZE1*sizeof(void));
pathname1 = argv[1];
pathname2 = argv[2];
/* opening file 1 test */
t = now();
fdo1 = openRtest(pathname1);
t = now() - t;
printf("time to open file %s: %.3f\n", pathname1, t);
/* opening file 2 test */
t = now();
fdo2 = openRtest(pathname2);
t = now() - t;
printf("time to open file %s: %.3f\n", pathname2, t);
t = now();
read1test(fdo1, bufread);
t = now() - t;
printf("time to read blocksize %d from file %s: %.3f\n", BUFFSIZE1,
pathname1, t);
t = now();
read1test(fdo2, bufread);
t = now() - t;
printf("time to read blocksize %d from file %s: %.3f\n", BUFFSIZE1,
pathname2, t);
(........)
| |
| ferbar 2005-09-25, 6:59 pm |
| About post:
I can see the output with printf(buffer)... so, I guess it's not a
problem.. though I don't understand why it doesn't appear in the
debugger..
Anyway, thanks
FBM
ferbar wrote:
> Hi,
>
> I'm writing a program comparing file access patterns. I'm starting with
> 'read' on a 3MB file.
>
> The function that reads from the file is the following:
>
> void read1test(int fd, void * buffer) {
> ssize_t bytesr;
>
> bytesr = read(fd, buffer, BUFFSIZE1);
> if (bytesr < 0) {
> perror("read:");
> exit(bytesr);
> }
> }
>
> Using Eclipse+CDT, I'm debugging the code, and when the execution is at
> the bytesr = read() line, I am not able to see what is stored in the
> 'buffer' variable.
>
> Below you can see part of the code.
>
> Thanks in advance,
>
> FBM
> ________________________________________
__________________________
> #define BUFFSIZE1 1024
> #define FOR_READING 0 //(of no use)
>
> #include <fcntl.h>
> #include <sys/time.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
>
> double now(void) {
> struct timeval t;
> gettimeofday(&t,NULL);
> return ((double)t.tv_sec + (double)t.tv_usec/1000000.0);
> }
>
> int openRtest(char *pathnameX) {
> int fd;
> fd = open(pathnameX, O_RDONLY);
> if (fd < 0) {
> perror("desc:");
> exit(fd);
> }
> return fd;
> }
>
> int openWtest(char *pathnameX) {
> int fd;
> fd = open(pathnameX, O_RDWR|O_CREAT|S_IRUSR|S_IWUSR);
> if (fd < 0) {
> perror("desc:");
> exit(fd);
> }
> return fd;
> }
>
> void read1test(int fd, void * buffer) {
> ssize_t bytesr;
>
> bytesr = read(fd, buffer, BUFFSIZE1);
> if (bytesr < 0) {
> perror("read:");
> exit(bytesr);
> }
> }
>
> void ls 1test(int fd, off_t offset, int ls action, void * buffer,
> int s curr) {
> int currpos, nextposition;
> ssize_t bytesr;
>
>
> currpos = ls (fd, 0, s curr);
> if (currpos == -1) {
> perror("ls :");
> exit(currpos);
> }
> switch (ls action) {
> case 0:
> while (bytesr != 0) {
> bytesr = read(fd, buffer, BUFFSIZE1);
> if (bytesr < 0) {
> perror("read:");
> exit(bytesr);
> }
> nextposition = ls (fd, offset, SEEK_CUR);
> }
> }
> }
>
> void write1test(int fd, void *buffer) {
>
> ssize_t bytesw;
> size_t nbytes;
> bytesw = write(fd, buffer, nbytes);
> if (bytesw < 0) {
> perror("write:");
> exit(bytesw);
> }
>
> }
> int main(int argc, char** argv) {
> int fdo1, fdo2, fdo3, initposition;
> double t;
> char * pathname1, * pathname2;
> void * bufread , * bufread2;
> char outputpathname[] = "/tmp/output1.txt";
>
> initposition = 0;
>
> bufread = (void*)malloc(BUFFSIZE1*sizeof(void));
> bufread2 = (void*)malloc(BUFFSIZE1*sizeof(void));
> pathname1 = argv[1];
> pathname2 = argv[2];
>
> /* opening file 1 test */
> t = now();
> fdo1 = openRtest(pathname1);
> t = now() - t;
> printf("time to open file %s: %.3f\n", pathname1, t);
>
> /* opening file 2 test */
> t = now();
> fdo2 = openRtest(pathname2);
> t = now() - t;
> printf("time to open file %s: %.3f\n", pathname2, t);
>
>
> t = now();
> read1test(fdo1, bufread);
> t = now() - t;
> printf("time to read blocksize %d from file %s: %.3f\n", BUFFSIZE1,
> pathname1, t);
>
> t = now();
> read1test(fdo2, bufread);
> t = now() - t;
> printf("time to read blocksize %d from file %s: %.3f\n", BUFFSIZE1,
> pathname2, t);
>
> (........)
|
|
|
|
|