Jump to content
  • Advertisement

Archived

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

t2sherm

File input output

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

I am trying to output to a file and read in from that file in VC5. I looked in the help and found many different types of functions with types like FILE and other functions like cin and cout and i guess they can be used to write to a file. I am trying to write out a struct to a file. I know C++, but I''m not familiar will all of these functions, and I don''t know which one(s) to use and how to use them. I am used to VB where it is just a few lines of code and it will write out or read in files. t2sherm ô¿ô

Share this post


Link to post
Share on other sites
Advertisement
try this...you''re prolly doing a windows-ish program, but you should be able to adapt this....


#include // I/O stream
#include // filestream header

void main()
{

ostream Output; // Variable...
istream Input;

input.open("C:\file.in"); // open the input file
output.open("C:\file.out"); // open the output file

//then read from the input like this
input >> variable;

//then write to output like this...
output << variable;

//then you MUST close the files
input.close();
output.close();


return;
}



Ok, real simple and crude code, but thats the bare bones of it....

Share this post


Link to post
Share on other sites
Try this (C):
-------------------
#include <stdio.h>

FILE *outFile;

outFile=fopen("c:\\autoexec.bat", "r");
/* the second has to be mode; r for read, b for binary... it can be a combo like "w" */

fprintf(outFile, "Hi, your autoexec.bat is dead.\n");

fclose(outFile);

-----------------
or this (C++) :
-----------------

#include <fstream.h>

fstream outFile;

outFile.open("c:\\autoexec.bat", ios::out);
outFile << Your autoexec.bat is dead. In C++, even...";
outFile.close;
==============

Share this post


Link to post
Share on other sites
If I read abe_bcs''s post right, shouldn''t the file opening mode on the first example be "w"? Right now it is set up to read... Anyway, if you want a nice wrapper class for the C style FILE functions, email me. I wrote it myself (and everyone snickers: THAT''S a major accomplishment!)

--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

Share this post


Link to post
Share on other sites
quote:
Original post by RoosterJM

#include // I/O stream
#include // filestream header

void main()
{

ostream Output; // Variable...
istream Input;

input.open("C:\file.in"); // open the input file
output.open("C:\file.out"); // open the output file

//then read from the input like this
input >> variable;

//then write to output like this...
output << variable;

//then you MUST close the files
input.close();
output.close();


return;
}




Actually, remember that you don''t always have to explicitly close() streams, as they close when they go out of scope

Share this post


Link to post
Share on other sites
Depending on compilier...in VC++ they close, but in borland (yech!) they dont...

anywho...its a good habit..

Share this post


Link to post
Share on other sites
quote:
Original post by RoosterJM

Depending on compilier...in VC++ they close, but in borland (yech!) they dont...

anywho...its a good habit..


Then I think that means Borland''s streams are broken I guess you could consider it good practice to close them yourself, but then that is the whole idea of objects... you can open a file with the constructor and the destructor should close it. *sigh*

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!