Skip to main content
GameDev.net gamedev.net
🔒 Locked

ofstream // ifstream ?

Started by bilsa Mar 10, 2003 at 12:55 PM 3 replies 300+ views
Original Post
bilsa
bilsa
Hello! I''m currently using those two calls to work with files. I haven''t been dooing much reading about those... just satisfied with reading and writing from/to files simply and with no pain. Currently the separator for this is the space " ", which separates when to start and stop reading/writing from/to a file. But in many cases I want to read a whole sentence from my files without having to put a number of how many words I want to read. I have the same problem with writing to a file... it all just writes in a row... but what if I would like to start on a new row or something? (to have it easier to read) So does anyone know how to make my own separator and read/write from/to a file untile it gets to my separator, like ";" or "%"... Or is there any other way of achieving what I want? Thx!
Sand_Hawk
Sand_Hawk
Usually, when I need to write a textfile I use the \n as line seperator(\n is newline). To write the stuff, I usually do this:


char *szVar = "Text I liek to write";
fstream Fout;
Fout.open(...);
Fout << szVar << endl;


To read, you just use getline() to read:


char szVar[512];
fstream Fin;
Fin.open(...);
Fin.getline(szVar, 512, ''\n''); // ''\n'' is the delimiter, or the line seperator


I hope this is what you wanted to know!

Sand Hawk

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

(Inspired by Pouya)
----------------(Inspired by Pouya)
Sand_Hawk
Sand_Hawk
Usually, when I need to write a textfile I use the \n as line seperator(\n is newline). To write the stuff, I usually do this:


char *szVar = "Text I liek to write";
fstream Fout;
Fout.open(...);
Fout << szVar << endl;


To read, you just use getline() to read:


char szVar[512];
fstream Fin;
Fin.open(...);
Fin.getline(szVar, 512, ''\n''); // ''\n'' is the delimiter, or the line seperator


I hope this is what you wanted to know!

Sand Hawk

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

(Inspired by Pouya)
----------------(Inspired by Pouya)
bilsa
bilsa
Thank you!

That should work good. But what I was actually hoping for was some way of creating your own separator

But your way will work good for what I''m doing as well
civguy
civguy

  
char my_very_own_separator = '';'';

std::ifstream fin("jee.txt");
std::string text;
std::getline(fin, text, my_very_own_separator);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.