• Advertisement

Archived

This topic is now archived and is closed to further replies.

File I/O in VC++

This topic is 5149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What should i use to write things in/out of a file? (something like fread/fwrite, but something that works) thanx in advance "Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin

Share this post


Link to post
Share on other sites
Advertisement
File streams is probably what you want - specifically the ifstream and ofstream classes (fstream.h). I suggest either you snag a decent intro to C++ book, or look up some tutorials on the web. If you''re familiar with istream/ostream (think cin and cout) then you won''t have much trouble.

-Lutrosis

Share this post


Link to post
Share on other sites
Like the above poster said, if you are familiar with the c++ library functions for cin and cout, you should have no problem with using fstream.

Here is a good article that shows you how to input/output to text or binary.

Share this post


Link to post
Share on other sites
quote:
Original post by Lutrosis
...(fstream.h)...
-Lutrosis


These days, #include <fstream> (minus .h) is the standard.


[edited by - nervo on January 13, 2004 5:21:49 PM]

Share this post


Link to post
Share on other sites
plese! im not that lame! cout is not so good becouse it writes everything in ascii format. fread starts to read trash from file after a few dozen bytes. havent read about it, but one of my class leaders said, that there is something wrong with it (he said windows blocks reading if the file is accessed too frequently). he also told me something about a cfile class, which is the correct implementation of the standard fread/fwrite functions. can somebody confirm this?

oh and this code ends up with access violation when i call fwrite:

printf("File I/O Tester\n\n");
FILE *fp;
fp = fopen("test.bdf","w");
if(fp = NULL)
{
printf("Error opening File!");
return 1;
}
int size=5000;
float *obuffer=new float[size];
int i;
for(i=0; i=(float)i;
i--;
fwrite(obuffer,4,size,fp);
fclose(fp);

"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin

[edited by - orbano on January 13, 2004 6:25:01 PM]

Share this post


Link to post
Share on other sites
fread and fwrite work perfectly well.

The C++ IO streams are not very good at binary IO IMNSHO - I wrote my own class wrapper over the C functions to use for binary IO. For text input & output, fstream works pretty good.

...
Your code needs a little help:

FILE* fp = fopen("filename", "w");
if(fp==NULL)
//error!

return -1;

float* data = new float[5000];
for(int i=0; i<5000; ++i)
data[i] = i;

if(5000 != fwrite(data, sizeof(float), 5000, fp))
{
//error!

delete[] data;
return -2;
}
delete[] data;

fclose(fp);



[edited by - Magmai Kai Holmlor on January 13, 2004 6:41:27 PM]

Share this post


Link to post
Share on other sites
thanx. what was the problem with my code? i dont see anything important change, except a better error handling, and replacing my size variable with a constant. size_t type is not compatible with int?
and delete should be used instead of delete. use delete[] when deleting arrays of arrays.

"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin

[edited by - orbano on January 13, 2004 6:59:37 PM]

Share this post


Link to post
Share on other sites
and now i got read error. this code can read only 154bytes from the file created.

FILE *fp;
fp = fopen("test.bdf","w");
if(fp==NULL)
{
cout << "cannot open for write";
return -1;
}
unsigned int size = 100000;
float* data = new float[size];
unsigned int i;
for(i=0; i = (float) i;
if(size != fwrite(data, sizeof(float), size, fp))
{
cout << "write error";
delete data;
return -2;
}
fclose(fp);
FILE *fp2;
fp2= fopen("test.bdf","r");
if(fp==NULL)
{
cout << "cannot open for reading";
return -1;
}
float* indata = new float[size];
unsigned int bytesread = fread(indata, sizeof(float), size, fp2);
if(size != bytesread)
{
cout << "read error: " << bytesread;
delete indata;
return -2;
}
for(i=0; i < size; ++i)cout << indata[i] << " - " << data[i];
delete indata;
delete data;
fclose(fp2);
return 0;

[edited by - orbano on January 13, 2004 7:33:44 PM]

[edit: fixed <'s]

[edited by - Magmai Kai Holmlor on January 13, 2004 12:10:55 AM]

Share this post


Link to post
Share on other sites
Depends. For DOS work I suggest fopen,fwrite,fread,etc.
But for Windows programs I use CreateFile,WriteFile,ReadFile,etc. Saves me an include.

Share this post


Link to post
Share on other sites
quote:
Original post by orbano
thanx. what was the problem with my code? i dont see anything important change, except a better error handling, and replacing my size variable with a constant. size_t type is not compatible with int?


The for loop just wasn''t right - it didn''t do what you wanted.

quote:

and delete should be used instead of delete. use delete[] when deleting arrays of arrays.



You use delete[] when you use new[] - for any array.

Share this post


Link to post
Share on other sites
Change your fopen to "wb" and "rb" to put it in binary mode.
(Defaults to text mode, which will mess with the end-of-line characters)

Use delete[] instead of delete.

fread doesn't return the bytes read, it returns elements read - you tell it the size of the elements with the 'sizeof(float)' code.

int count = fread(indata, sizeof(float), size, fp2);
if(count != size)
{//error
}


Doh, the board garbles the for loop code because the [ i ] confuses it (it means italics to the board) and it thinks the < is the start of an html tag. Let's use j (and you use & l t ; for a <
Something like...
for(int j=0; j<size; ++j)
data[j] = (float)j;
...is what you want.

You have a memory leak where you
delete[] indata;
return -2;
data is never delete'd.

You should use unique return values for each failure point.

After you delete something, it's not a bad idea to set the pointer to 0. You are allowed to do delete 0, and nothing bad will happen.

[edited by - Magmai Kai Holmlor on January 13, 2004 12:09:50 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Nervo
These days, #include <fstream> (minus .h) is the standard.
These days, &lt; and &gt; (with the semicolons) are the standard.

Share this post


Link to post
Share on other sites
quote:
Original post by Beer Hunter
quote:
Original post by Nervo
These days, #include <fstream> (minus .h) is the standard.
These days, &lt; and &gt; (with the semicolons) are the standard.


I can always count on Beer Hunter to keep me on my toes. Though you haven''t posted much these days!

Share this post


Link to post
Share on other sites
quote:
Original post by Beer Hunter
quote:
Original post by Nervo
These days, #include <fstream> (minus .h) is the standard.
These days, &lt; and &gt; (with the semicolons) are the standard.


Actually, source and code tags are. They let you forget about all the mess, and instead let you show nice code.




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

Share this post


Link to post
Share on other sites
Thank you all your help!
The peroblem was with the file input mode. With bynary mode it works perfectly. And. Lets make things celar: delete is the opposite of new. Doesnt matter if the type of the pointer is a single element, or an array (like myfloat = new float[...]). Delete will delete exactly the same as new has created. You need the [] after delete, if you want to tell the compiler, that it should call another delete to the elements of the array, you wish to delete.
And my code was doing exactly what i wanted. It was just a 30-raw test propgram to test if fwrite/read works fine, so thats why i didnt care about correct return values or such.


"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin

Share this post


Link to post
Share on other sites
quote:
The C++ IO streams are not very good at binary IO IMNSHO

I would love to hear why you think this is the case. Assuming that by "binary" you actually mean "unformatted", of course (for binary I/O they are orders of magnitude better than C''s offering), their interface is damn near identical to that of C.

Share this post


Link to post
Share on other sites

  • Advertisement