|
-
February 10th, 2004, 04:15 PM
#1
Writes OK but doesn't Read very well?
Hi Boys 'n Girls-
Kin someone point out the problem with this code?
#include "stdafx.h"
#include <stdio.h>
int main(int argc, char* argv[])
{
FILE *Afile;
int xx=123456,yy;
Afile=fopen("d:Test.txt","wb");
fwrite(&xx,4,1,Afile);
rewind (Afile);
fread(&yy,4,1,Afile);
printf("\n Wrote: %d\n Read: %d\n",xx,yy);
_fcloseall();
return 0;
}
Thanks,
--------------------------------------------
http://www.volfirst.net/~sirjorj/index.html
---------------------------------------------
-
February 10th, 2004, 04:44 PM
#2
Well...what is the problem? I simply assume - based on your subject - that the write command has not been flushed to the file at the time you are reading again...so you might want to add a 'fflush(Afile)' after your write statement...
-
February 10th, 2004, 04:46 PM
#3
you open with the mode : "wb"
I'm not really that familiar with the C open modes, but I think
that it should be : "r+b" or "w+b"
Last edited by Philip Nicoletti; February 10th, 2004 at 04:49 PM.
-
February 10th, 2004, 04:56 PM
#4
Originally posted by Philip Nicoletti
you open with the mode : "wb"
I'm not really that familiar with the C open modes, but I think
that it should be : "r+b" or "w+b"
No..."wb" is actually fine...you are referring to a different mode...there exist two modes starting with a 'r':
- "r" -> opens file for reading
- "r+" -> opens file for reading and changing
Thus, both "rb" and "r+b" are valid - though different - operations...same applies to writing...
-
February 10th, 2004, 04:59 PM
#5
To read and write, you must open the file as either "r+" or "w+". The binary mode (no translation of carriage return/line feed) is probably irrelavent in this case (though "r+b" or "w+b" would be correct).
In your case, the "wb" only opened the file for writing and not reading.
Further, when opened this way, writes/reads are not neccessarily committed when you do a rewind(), so you need to do a flush. Here is some info from MSDN:
>>
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.
>>
b
Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.
Link:
http://msdn.microsoft.com/library/de...c_._wfopen.asp
Andrew Ferlitsch
-
February 10th, 2004, 05:05 PM
#6
Originally posted by aferlitsch
In your case, the "wb" only opened the file for writing and not reading.
Overlooked the obvious... 
Well, I guess it is too late for me...
-
February 10th, 2004, 05:26 PM
#7
No- its not too late!
Sorry folks I made a mistake in setting up an example situation of the real problem. The integer is actually being read in as an element of a larger char block.
A more correct example program would go like this:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
FILE *Afile;
int xx=123456,yy;
char aa[4];
Afile=fopen("d:Test.txt","w+b");
fwrite(&xx,4,1,Afile);
rewind (Afile);
fread(aa,4,1,Afile);
yy=(int)aa;
printf("\n Wrote: %d\n Read: %d\n",xx,yy);
_fcloseall();
return 0;
}
..and thanx for all the replies.
--------------------------------------------
http://www.volfirst.net/~sirjorj/index.html
---------------------------------------------
-
February 10th, 2004, 05:55 PM
#8
You are setting yy to be equal to a pointer to aa, not the bytes that are in aa. You would need to do something like
Code:
yy = (int) (*(int *)aa);
I can't remember off the top of my head the byte-ordering that Intel processors use, but you might run into byte ordering problems doing this. Why not just do:
Code:
fread(&yy,4,1,Afile);
-
February 10th, 2004, 07:24 PM
#9
you can try memcpy(&yy, a, 4)
not sure the bytes will still mean what you want them to mean
awni
-
February 10th, 2004, 09:13 PM
#10
Originally posted by wayside
Why not just do:
Code:
fread(&yy,4,1,Afile);
'Casue I don't know the file address for yy but I do know its location in a buffer that had been read.
So I must handle it from that location.
--------------------------------------------
http://www.volfirst.net/~sirjorj/index.html
---------------------------------------------
-
February 10th, 2004, 09:38 PM
#11
[QUOTE]Originally posted by wayside
You are setting yy to be equal to a pointer to aa, not the bytes that are in aa. You would need to do something like
Code:
yy = (int) (*(int *)aa);
Bravo wayside!
Your code did the trick.
{Haven't tested awni's memcpy)
Thanks for the help.
--------------------------------------------
http://www.volfirst.net/~sirjorj/index.html
---------------------------------------------
-
February 11th, 2004, 09:20 AM
#12
Originally posted by Sirjorj
'Casue I don't know the file address for yy but I do know its location in a buffer that had been read.
So I must handle it from that location.
I don't understand what you mean by the "file address", this makes no sense to me.
By doing
Code:
fread(&yy,4,1,Afile);
you are using the yy variable, which you have declared as an int, a a 4 byte buffer, the same as you are using with your char aa[4] variable. They both take the same amount of space, by using &yy you are getting a pointer to it and can treat it like a 4 byte buffer, and read directly into it. You don't need to go through a char buffer first.
-
February 11th, 2004, 04:35 PM
#13
[QUOTE]Originally posted by wayside
I don't understand what you mean by the "file address", this makes no sense to me.
Yes you are right with regard to the SAMPLE code I submitted, I could read the value directly.
BUT in my REAL program I would not be able to set fseek()...
so I must take the value from an available buffer as it is being processed.
Guess I should have explained the difference between my sample and the real-life problem.
But- tnx again.
P.S. the memcpy() also works OK.
--------------------------------------------
http://www.volfirst.net/~sirjorj/index.html
---------------------------------------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|