473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

open and read functions (can't make 'read' work..)

Hi all,

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?

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("C:/cygwin/home/Jeannie/workspace/hw2/ip.packets.2.txt",
O_RDWR);
t = now();
bytesr = read(fdo1, bufread, 2);
t = now() - t;
printf("time %.3f\n", t);
exit(0);
}

Nov 15 '05 #1
9 1952
On 24 Sep 2005 12:59:19 -0700, "ferbar" <fb******@gmail.com> wrote in
comp.lang.c:
Hi all,

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
Unfortunately there is no 'read' function in C. It is, as far as the
C language and this newsgroup are concerned, a non-standard platform
specific extension.
bytesr = read(fdo1, bufread, 2);


The 'open' function returns the file dsc 3. So this seems ok..


No 'open' function in C either.
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>
The headers <stdio.h> and <stdlib.h> are standard C, the other three
are non-standard extensions and not discussed here.

I won't go through your code and point out the rest of your functions
that are non-standard and off-topic here. Since you posted through
Google, there is no header information to provide insight on your
system.
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;
The declaration above looks like it could very well cause a real
problem. You have defined a pointer to void, but it is uninitialized.
You have not defined any data for it to point to, nor have you set it
to point to anything useful. Any use of this pointer's value produces
undefined behavior.
pathname = argv[1];
fdo1 = open("C:/cygwin/home/Jeannie/workspace/hw2/ip.packets.2.txt",
O_RDWR);
t = now();
bytesr = read(fdo1, bufread, 2);
Even without knowing what the 'read' function is supposed to do, I can
tell you that passing the uninitialized pointer value 'bufread' to a
function is undefined behavior all by itself. Most likely, 'read' is
trying to store data into the memory pointed to by 'bufread', but
'bufread' does not point to any valid memory.

You probably need to change the definition of 'bufread' to something
like:

unsigned char bufread [SOME_NUMBER];

....where SOME_NUMBER is a value that is large enough to hold the
maximum amount of data that will be stored there.
t = now() - t;
printf("time %.3f\n", t);
exit(0);
}


With almost no exceptions, standard and even non-standard functions in
C that take a pointer variable require that the pointer actually
points to valid existing memory. This is almost certainly the case
with your 'read' function.

If you have more questions about the extensions like 'read', 'open',
and 'gettimeofday', which are not standard C, the best group to ask
would be news:comp.unix.programmer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #2
ferbar wrote:
Hi all,

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?

<snip>
open() and read() are Unix functions, they're not standard C. Any reason
you can't use fopen() and fread()? Especially since (judging from the
pathname) you're running on Windows anyway. You wouldn't need Cygwin if
you used the standard I/O functions.

Check the value of errno to find out what went wrong. For example:

#include <string.h>
#include <errno.h>

....

bytesr = read(fdo1, bufread, 2);
if (bytesr < 0) {
printf("Can't read from file: %s", strerror(errno));
}

Note that "bufread" must point to initialized memory. You can't just
pass in a void*. You probably meant to declare a buffer like

unsigned char bufread[BUFSIZE];

S.
Nov 15 '05 #3
ferbar wrote on 24/09/05 :
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);


Not standard C.

Please use standard C (fopen(), fread() etc.) on c.l.c, or go to a unix
group.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
Nov 15 '05 #4
"ferbar" <fb******@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
bytesr = read(fdo1, bufread, 2);

The 'open' function returns the file dsc 3. So this seems ok..

Any idea what might be wrong?


The open() and read() functions are not defined in standard C. The
standard C equivalents are fopen() and fread(). There are valid
reasons for using the non-portable versions (in non-portable code),
but this isn't the place to ask about them.

If you want to try using fopen() and fread(), this is the place to
ask; for open() and read(), try comp.unix.programmer. (It looks like
you're using Cygwin, but I don't think you're doing anything
Cygwin-specific.)

Having said that, I have a couple of comments.
#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("C:/cygwin/home/Jeannie/workspace/hw2/ip.packets.2.txt",
O_RDWR);
t = now();
bytesr = read(fdo1, bufread, 2);
t = now() - t;
printf("time %.3f\n", t);
exit(0);
}


You say that the open() function returns 3, and read() returns -1, but
there's no indication in your code that you actually checked the
values. Apparently what you posted isn't the actual code you're
talking about.

For fopen(), you *always* need to check whether the file was opened
successfully; the same applies to open(). And the code you posted
assigns the result of read() to bytesr, but doesn't use it; presumably
there's something useful you could do with that result.

You set pathname to argv[1] (without checking whether there actually
is an argv[1]) -- but then you don't use it.

Finally (and I think this is your real problem), the variable bufread
is never initialized before being passed to read(). I'm not going to
comment here on what read() does with its second argument; it's
sufficient to say that any reference to an uninitialized variable
invokes undefined behavior. The name "bufread", and the fact that
it's of type void*, tend to imply that it should be a pointer to a
buffer. You need to make it point to a buffer that's large enough for
whatever you're going to use it for.

That's about all I can say without going beyond standard C.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 15 '05 #5
Jack Klein <ja*******@spamcop.net> writes:
[...]
With almost no exceptions, standard and even non-standard functions in
C that take a pointer variable require that the pointer actually
points to valid existing memory. This is almost certainly the case
with your 'read' function.


With no exceptions at all, any function that takes a pointer argument
requires that the pointer either points to valid existing memory, or
has the value NULL. Even if the function itself allowed an
uninitialized value (e.g., if another argument tells the function to
ignore the value), evaluating the uninitialized variable would invoke
undefined behavior before the function is called.

(When I say "no exceptions at all", I'm ignoring non-portable hacks
like using (void*)-1 as a special value. Even if you assume that's
valid, an uninitialized value is not.)

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 15 '05 #6
Yes, I have to use non-standard. You are right, this is not the forum.
I posted in comp.unix.programmer too.

But thanks all for being kind enough to respond anyways... About the
pointer, true. No way it could work with uniniatialized pointers.

Thanks!

FBM

Nov 15 '05 #7
"ferbar" <fb******@gmail.com> writes:
Yes, I have to use non-standard. You are right, this is not the forum.
I posted in comp.unix.programmer too.

But thanks all for being kind enough to respond anyways... About the
pointer, true. No way it could work with uniniatialized pointers.


Once again:

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) ks***@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.
Nov 15 '05 #8
On Sat, 24 Sep 2005 21:24:52 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c:
Jack Klein <ja*******@spamcop.net> writes:
[...]
With almost no exceptions, standard and even non-standard functions in
C that take a pointer variable require that the pointer actually
points to valid existing memory. This is almost certainly the case
with your 'read' function.


With no exceptions at all, any function that takes a pointer argument
requires that the pointer either points to valid existing memory, or
has the value NULL. Even if the function itself allowed an
uninitialized value (e.g., if another argument tells the function to
ignore the value), evaluating the uninitialized variable would invoke
undefined behavior before the function is called.

(When I say "no exceptions at all", I'm ignoring non-portable hacks
like using (void*)-1 as a special value. Even if you assume that's
valid, an uninitialized value is not.)


This time I think you misinterpreted me.

The wording "With almost no exceptions, standard and even non-standard
functions in C that take a pointer variable require that the pointer
actually points to valid existing memory" is worded the way it is
because some functions, such as free() and strtol(), are defined to
accept null pointer arguments, which most certainly do not point "to
valid existing memory."

So my wording as written is correct, and at the time it seemed
unimportant to distinguish between null and uninitialized pointers. It
seems perhaps a little less unimportant now.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #9
Jack Klein <ja*******@spamcop.net> writes:
On Sat, 24 Sep 2005 21:24:52 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c:
Jack Klein <ja*******@spamcop.net> writes:
[...]
> With almost no exceptions, standard and even non-standard functions in
> C that take a pointer variable require that the pointer actually
> points to valid existing memory. This is almost certainly the case
> with your 'read' function.


With no exceptions at all, any function that takes a pointer argument
requires that the pointer either points to valid existing memory, or
has the value NULL. Even if the function itself allowed an
uninitialized value (e.g., if another argument tells the function to
ignore the value), evaluating the uninitialized variable would invoke
undefined behavior before the function is called.

(When I say "no exceptions at all", I'm ignoring non-portable hacks
like using (void*)-1 as a special value. Even if you assume that's
valid, an uninitialized value is not.)


This time I think you misinterpreted me.

The wording "With almost no exceptions, standard and even non-standard
functions in C that take a pointer variable require that the pointer
actually points to valid existing memory" is worded the way it is
because some functions, such as free() and strtol(), are defined to
accept null pointer arguments, which most certainly do not point "to
valid existing memory."

So my wording as written is correct, and at the time it seemed
unimportant to distinguish between null and uninitialized pointers. It
seems perhaps a little less unimportant now.


Sure. I was expanding on what you wrote, not disagreeing.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

55
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's...
115
by: TheAd | last post by:
At this moment I use MsAccess and i can build about every databound application i want. Who knows about a serious open source alternative? Because Windows will be a client platform for some time, i...
6
by: Rolf Schroedter | last post by:
(Sorry for cross-posting). I need to access large files > 2GByte (Linux, WinXP/NTFS) using the standard C-library calls. Till today I thought I know how to do it, namely for Win32: Use open(),...
8
by: Asma | last post by:
Dear Sir, I am trying to find a way to open a Word document using C language and read the text of word doc into a variable. (Turbo C on Dos 6.0). Can anyone please tell me which libraries in...
13
by: Blue | last post by:
Hi , Can any one please let me explain me the diffrences between "open"/ "fopen" or "read"/"fread" or "write/fwrite". I know that "open" /"read" / "write" are system calls and "fopen"...
5
by: Yoshitha | last post by:
Hi I am developing a C#.Net windows application for my project. In that project I have an IDE to work on. The application is similar to Adobe Photoshop. My requirement is as follows. 1) I must...
22
by: fniles | last post by:
I have a .CSV file (comma delimited) that I want to open using OLEDB, but I get the error "External table is not in the expected format." If I save the .CSV file to an .XLS file, I can open the...
25
by: Andy_Khosravi | last post by:
I just recently changed my database that I'm running from a monolithic DB to a split FE/BE. The front end resides on the client machine and the BE resides on a network drive. I'm experimenting with...
3
by: swetha | last post by:
Hi, I want to load one bitmap file with out using handler functions or win32 applications in C++(not in MFC). I have one more doubt that is it possible to draw lines or any thing using OPENGL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.