473,769 Members | 7,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with fread()

Helas,

I got something that should be a minor problem, but anyhow it isn't to me
right now.

A little code fragment:
fread(&file_qn, x, 1, fp_q);

The corresponding text file looks like this:
456 5 1.txt%&'
I've already skipped the '456' part with fseek, read the '5' with scanf,
file_qn should now get the '1.txt' part and x is the length of that string,
which is 5.
Thanks for any help in advance !

Alain.
Nov 14 '05 #1
10 4192
On Thu, 29 Jan 2004 05:35:36 +0100, "Alain Lafon" <pr***@gmx.at > wrote
in comp.lang.c:
Helas,

I got something that should be a minor problem, but anyhow it isn't to me
right now.

A little code fragment:
fread(&file_qn, x, 1, fp_q);

The corresponding text file looks like this:
456 5 1.txt%&'
I've already skipped the '456' part with fseek, read the '5' with scanf,
file_qn should now get the '1.txt' part and x is the length of that string,
which is 5.
Thanks for any help in advance !

Alain.


Here's my advice: Post again and explain what your "minor problem"
is. Post a complete compilable code snippet, how the heck are we
supposed to know the type of file_qn or x? If there are compiler
error messages, copy the text and paste into the body of your message.

You haven't provided enough information on your problem for anyone to
offer any other advice.

--
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 14 '05 #2
I'm sorry.

Here's my further information:
The code compiles fine. The problem is that the program simply closes when
it should do the fread() part.
Here's the code fragment:

void abhaengen(FILE *fp_q, int key)
{
int i;
const char* file_qn;
-some other variables-
-snip-
fscanf(fp_q, "%i", &x); //gets the size of the string which is to be
read by fread()
fread(&file_qn, x, 1, fp_q); //this is exactly where the program simply
closes with no further messages.
-snip-
}

The file that is to be read looks like this:
456 5 1.txt%&'
I'm sorry to have bothered you with an incomplete posting. But it's already
5:52am and I'm getting a little confused *g*

Thanks again,
Alain.
Nov 14 '05 #3
nrk
Alain Lafon wrote:
I'm sorry.

Here's my further information:
The code compiles fine. The problem is that the program simply closes when
it should do the fread() part.
Here's the code fragment:

void abhaengen(FILE *fp_q, int key)
{
int i;
const char* file_qn;
Why const? This declaration says file_qn is a pointer to const char, which
means you cannot change what file_qn points to. Since this is precisely
what you want to do, lose the const:
char *file_qn;

However, you still haven't made file_qn point to anything useful. One way
of doing this is by using malloc to allocate some memory and make file_qn
point to that.
-some other variables-
-snip-
fscanf(fp_q, "%i", &x); //gets the size of the string which is to be
read by fread()
Now you even know how much memory you need (check the return value of fscanf
to make sure you got a valid integer):
file_qn = malloc(sizeof *file_qn * x); /* check the result for NULL */
fread(&file_qn, x, 1, fp_q); //this is exactly where the program That's totally wrong. fread will read nmemb items each size bytes big into
the area pointed to by ptr:

fread(file_qn, 1, x, fp_q);

Which says, read x items of size 1 from fp_q into the area pointed to by
file_qn. However, note that the fscanf will still leave any spaces between
the integer and the filename in the input stream, which means that simple
fread will get leading blank(s) and part of the filename. You're probably
better off doing this:

fscanf(fp_q, "%*[\n]"); /* throw away any blanks */
fread(&file_qn, x, 1, fp_q); /* now read the filename */
simply
closes with no further messages.
Probably a result of accessing memory you don't own through that fread call.
One would hope a decent OS will give you some indication as to why the
program was killed.

HTH,
-nrk.
-snip-
}

The file that is to be read looks like this:
456 5 1.txt%&'
I'm sorry to have bothered you with an incomplete posting. But it's
already 5:52am and I'm getting a little confused *g*

Thanks again,
Alain.


--
Remove devnull for email
Nov 14 '05 #4
Woo - some magic happened (;
I just made this little steganographic and cryptographic program - it worked
all fine, but i wouldn't be able to handle large filenames. This is why I
started implementing the pointers. I worked like the last 14 hours to get
this thing to work - and you did the very last part of it !

Thank you very, very much !

Btw: I learned that syntax for the fread() function from the book of Herbert
Schidt - somebody who was in the ANSI/ISO comittee - i thought he should
know such things...
Whatever..

Thanks just again !
Nov 14 '05 #5
[..]
void abhaengen(FILE *fp_q, int key)
{
int i;
int x;
const char* file_qn;
-some other variables-
-snip-
fscanf(fp_q, "%i", &x); //gets the size of the string which is to be
read by fread()
"file_qn", when not initialized, is a pointer to an arbitrary location.
If sizeof ( file_qn ) < x, then obviously the behaviour is undefined.

Use:

file_qn = malloc ( (size_t) x );
fread(&file_qn, x, 1, fp_q); //this is exactly where the program simply
closes with no further messages.
-snip-


And, do not forget to free the allocated memory.
[..]
--
Vijay Kumar R Zanvar
Nov 14 '05 #6
Mac
On Thu, 29 Jan 2004 06:33:06 +0100, Alain Lafon wrote:
Woo - some magic happened (;
I just made this little steganographic and cryptographic program - it worked
all fine, but i wouldn't be able to handle large filenames. This is why I
started implementing the pointers. I worked like the last 14 hours to get
this thing to work - and you did the very last part of it !

Thank you very, very much !

Btw: I learned that syntax for the fread() function from the book of Herbert
Schidt - somebody who was in the ANSI/ISO comittee - i thought he should
know such things...
Whatever..

Thanks just again !


Herbert Schildt's name is mud around this newsgroup. I haven't read any of
his stuff myself. But his poor reputation seems to be unanimous.

If you don't have anything else, get _The C Programming Language_, Second
Edition, by Kernighan and Ritchie.

You may also want to read the faq for this newsgroup.

http://www.eskimo.com/~scs/C-faq/top.html

Good luck

Mac
--

Nov 14 '05 #7
Alain Lafon wrote:
.... snip ...
Btw: I learned that syntax for the fread() function from the book
of Herbert Schidt - somebody who was in the ANSI/ISO comittee - i
thought he should know such things...


I believe he paid a fee to be on the committee, and never showed
up. He is a general laughing stock in the C world - his books,
while readable, are invariably loaded with mistakes. They are
generally referred to as BullSchildt, and recommended for lighting
fires.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #8
"Mac" <fo*@bar.net> writes:
[...]
Herbert Schildt's name is mud around this newsgroup. I haven't read any of
his stuff myself. But his poor reputation seems to be unanimous.


Indeed. He's the author of _The Annotated ANSI C Standard_, a book
containing the ISO C90 standard on the left-hand pages and his
commentary on the right-hand pages. The commentary is largely
nonsense. It's ably reviewed by Clive Feather at
<http://www.lysator.liu .se/c/schildt.html>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #9
Too bad i bought his book for just too much money then...

Thanks anyone for help !

Maybe if you're interested in what you helped building, take a look at
https://sourceforge.net/projects/pcif/

Thanks a lot again !

cya ^^
Nov 14 '05 #10

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

Similar topics

8
15360
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same location in the file. What is happening, is I can read the file, either in entirety or only part of it. And no matter what I try setting the position to, using fsetpos, it always gets set back to the very beginning of the file. I
22
1871
by: Rajshekhar | last post by:
Hi , i am writing a simple prgm to read a .txt file then store the contents into the array... program as follows: -------------------------- #include<stdio.h> int main() { FILE *fp1;
4
2639
by: rendl42 | last post by:
Hey all, I'm a relative newbie to PHP, and am getting some strange results with the fread() function with PHP (win32) 5.1.2. Here is some code: <?php echo $_SERVER."<br>"; echo $_SERVER; $myFile = $_SERVER."/Dave/blah.txt"; echo $myfile."<br>";
13
3551
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could discern, fread all the sudden kept returning the same byte over and over as if it were no longer advancing in the file. I used a hex editor to determine the address of the last byte read in the file. CF was the last address, D0 was not ever read...
6
5273
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
6354
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a time */ I would assume the latter form would be faster, or at least
3
2078
by: Harry | last post by:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> void scramble(void); struct bmp_header { short int sig; int size_bmp;
24
6019
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h> #include<string.h> void main(){ FILE *fp,*ftemp;
9
1934
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is like this : (very simple..........) 010001010110001101010101010101010101010101 #include<stdio.h>
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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
10049
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
9997
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
9865
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
7413
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
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.