Jump to content
  • Advertisement

Archived

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

orbano

File I/O in VC++

This topic is 5334 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!