|
Home > Archive > Unix Programming > September 2005 > open and read functions (can't make 'read' work..)
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 |
open and read functions (can't make 'read' work..)
|
|
| ferbar 2005-09-24, 6:02 pm |
| Hi,
I'm trying to read from the txt file 'ip.packets.2.txt' using the read
function. It seems everything ok, but I get a -1 when executing
[vbcol=seagreen]
The 'open' function returns the file dsc 3. So this seems ok..
Any idea what might be wrong?
Thanks!
FBM
#define BUFSIZE 1024
#include <fcntl.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
double now() {
struct timeval t;
gettimeofday(&t,NULL);
return ((double)t.tv_sec + (double)t.tv_usec/1000000.0);
}
int main(int argc, char** argv) {
int fdo1;
double t;
char * pathname;
ssize_t bytesr;
void * bufread;
pathname = argv[1];
fdo1 = open("ip.packets.2.txt", O_RDWR);
t = now();
bytesr = read(fdo1, bufread, 2);
t = now() - t;
printf("time %.3f\n", t);
exit(0);
}
| |
| Paul Pluzhnikov 2005-09-24, 6:02 pm |
| "ferbar" <fbarsoba@gmail.com> writes:
> I'm trying to read from the txt file 'ip.packets.2.txt' using the read
> function. It seems everything ok, but I get a -1 when executing
>
Do "man perror" -- the system will tell you *why* it refuses
to read(2) if you ask it to.
[vbcol=seagreen]
> Any idea what might be wrong?
Ask yourself: where are you asking read(2) to deposit the data,
or where does "bufread" point to?
Hint: using uninitialized pointers is a bad idea(TM).
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| ferbar 2005-09-24, 6:02 pm |
| It is indeed.. :-(
Thanks for your explanation. It was very clear.
I did the following:
void * bufread;
bufread = (void*)malloc(BUFSIZE*sizeof(void));
And it worked.
There is a way to initialize the pointer w/o using the heap?
Thanks!
FBM
| |
| Keith Thompson 2005-09-24, 6:02 pm |
| "ferbar" <fbarsoba@gmail.com> writes:
> I'm trying to read from the txt file 'ip.packets.2.txt' using the read
> function. It seems everything ok, but I get a -1 when executing
>
>
> The 'open' function returns the file dsc 3. So this seems ok..
>
> Any idea what might be wrong?
>
> #define BUFSIZE 1024
>
> #include <fcntl.h>
> #include <sys/time.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
>
> double now() {
> struct timeval t;
> gettimeofday(&t,NULL);
> return ((double)t.tv_sec + (double)t.tv_usec/1000000.0);
> }
>
> int main(int argc, char** argv) {
> int fdo1;
> double t;
> char * pathname;
> ssize_t bytesr;
> void * bufread;
>
> pathname = argv[1];
> fdo1 = open("ip.packets.2.txt", O_RDWR);
> t = now();
> bytesr = read(fdo1, bufread, 2);
> t = now() - t;
> printf("time %.3f\n", t);
> exit(0);
> }
How do you know you get 3 from open() and -1 from bytesr()? If you
have a version of the program that displays these values, you should
show us that version rather than some similar code that may or may not
behave the same way. If you knew enough to be certain that your
changes don't affect the problem, you wouldn't be asking for help. If
you know what the functions return because you're running under a
debugger, you should say so.
The problems I see (some of which have already been pointed out here
or in comp.lang.c) are:
"double now()" is acceptable, but "double now(void)" is more explicit.
(I'm assuming this is C rather than C++.)
You use argv[1] without checking argc to see whether argv[1] even exists.
You assign the value of argv[1] to pathname, but you never use it.
You don't check the value returned by open(). You should *always*
check the value returned, and take appropriate action if it indicates
that it failed. If the file doesn't exist, you'll quietly try to read
from it.
You open the file in read/write mode, but you never write to it.
Using O_RDONLY makes more sense; it also allows you to open the file
even if you don't have write access to it.
bufread is uninitialized; this is probably your real problem.
You assign the result of read() to bytesr, but you never use it. You
should *always* check the value returned, and take appropriate action
if it indicates that it failed.
You never close the file.
You're using open() and read() (and you *should* be using close()),
which are POSIX-specific. I don't see that you're doing anything that
you couldn't do equally well with fopen(), fread(), and fclose(),
which are portable to any system that supports C. Since you're also
using the POSIX-specific gettimeofday(), this isn't a big deal, but
it's usually a good idea to code as portably as possible (but no more
so).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
| |
| ferbar 2005-09-24, 6:02 pm |
| I'm using Eclipse+CDT. But when debugging the read() function, I
couldn't get any feedback from it (got stuck in that line), and then a
changed to Linux envirnment this time w/o IDE.
The argv[1] was a last minute change trying to see if there was an
error with open()
The function now() for the time.. I'm getting it from another source.
About the returned values, right..
About the O_RDONLY, I have to write on the file further on..
It seems that the problem was with the pointer though.
Thanks a lot for your comments. I will learn a lot from them.
FBM
| |
| Keith Thompson 2005-09-24, 6:02 pm |
| "ferbar" <fbarsoba@gmail.com> writes:
> It is indeed.. :-(
What is indeed what?
Don't assume that your readers can easily see the article to which
you're replying. Provide some context.
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
| |
| Paul Pluzhnikov 2005-09-24, 6:02 pm |
| "ferbar" <fbarsoba@gmail.com> writes:
> There is a way to initialize the pointer w/o using the heap?
There are many:
--- cut --- using local array 1 ---
char buf[1024];
void *bufread = buf;
--- cut --- using local array 2 (better) ---
char buf[1024]; ...
bytesr = read(fdo1, buf, 2);
--- cut --- using local array 3 ---
bufread = alloca(1024);
--- cut --- using global array (don't do this unless there is a good reason) ---
char buf[1024];
int main(int argc, char** argv) { ...
void * bufread = buf;
...
--- cut --- using global array 2 (better; still don't do this unless there is a good reason) ---
char buf[1024];
int main(int argc, char** argv) { ...
bytesr = read(fdo1, buf, 2);
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| SM Ryan 2005-09-24, 6:02 pm |
| "ferbar" <fbarsoba@gmail.com> wrote:
# Hi,
#
# I'm trying to read from the txt file 'ip.packets.2.txt' using the read
# function. It seems everything ok, but I get a -1 when executing
#
# >>bytesr = read(fdo1, bufread, 2);
#
# The 'open' function returns the file dsc 3. So this seems ok..
#
# Any idea what might be wrong?
open, read, close, etc set errno on error. If you don't want to look
up the errno, use perror or strerror to print the error message.
Chances are it will tell you exactly what is wrong. Knowing is better
than guessing.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
If you plan to shoplift, let us know.
Thanks
|
|
|
|
|