473,666 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi all,

I'm trying to read from the txt file 'ip.packets.2.t xt' 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_s ec + (double)t.tv_us ec/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.tx t",
O_RDWR);
t = now();
bytesr = read(fdo1, bufread, 2);
t = now() - t;
printf("time %.3f\n", t);
exit(0);
}

Nov 15 '05 #1
9 1970
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.t xt' 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_s ec + (double)t.tv_us ec/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.tx t",
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.l earn.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.t xt' 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.t xt' 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.t xt' 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.progr ammer. (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_s ec + (double)t.tv_us ec/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.tx t",
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_Keit h) 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*******@spam cop.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_Keit h) 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.progr ammer 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.progr ammer 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.c om, 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_Keit h) 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.or g> wrote
in comp.lang.c:
Jack Klein <ja*******@spam cop.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #9
Jack Klein <ja*******@spam cop.net> writes:
On Sat, 24 Sep 2005 21:24:52 GMT, Keith Thompson <ks***@mib.or g> wrote
in comp.lang.c:
Jack Klein <ja*******@spam cop.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_Keit h) 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
4650
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 not obvious how the code works if you don't know the intricacies of the Property Let/Get syntax. Likewise, I dislike (and code to minimize the use of) the VB/VBA syntax of returning a value by referring to the function name as if it were a...
115
14087
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 prefer a solution that (also) supports Windows. On the net I found a number of products that i looked at, but none of them gave me the impression of a serious candidate at this moment (KNoda, Gnome DB Manager, InterBase...). 2 additional...
6
8452
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(), read(), _itelli64(), _lseeki64() with type __int64 Linux/Cygwin: #define _FILE_OFFSET_BITS 64 Use open(), read(), lseek() with type off_t
8
9163
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 C can be used to perform this task. Thanks you so much
13
22618
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" /"fread" / "fwrite" are normal library functions. May be internally must be calling "fopen" /"fread" / "fwrite" functions.
5
2161
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 be able to save my work in a file with custom file extention (similar to *.PSD in photoshop) 2) After I save my work, if I double click on that file, my work should be opened with my application automatically and I need to be able to work on
22
4973
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 connection with no problem. What is the correct way to open a .CSV file ? If I can not open the CSV file, how can I programmatically save the CSV file to an XLS file ? Thanks a lot. dim myCon OleDb.OleDbConnection
25
2876
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 a utility developed by Tony Toews to handle the distribution and subsequent updates of the software. I'm having some trouble with the overall upgrade process I've implemented, and I'm hoping one of you may have an idea how to go about fixing...
3
1427
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 on bitmap file.(this bitmap file is loaded using windows handler functions). Hope will give reply.
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.