Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get getline functions - quick question

Status
Not open for further replies.

FrankyTheFish

Programmer
Sep 7, 2002
14
0
0
CA
What I'm trying to do is to create a class that will ultimatly be used to have file from which the game will get all the eidtalbe variables, such as much, sounds effects and game parameters.

Now I haven't used ifstream in a very long time, I've tryed a few ways and can't seem to get it to work.

There would be 1 parameter per line. If anyone can explain to me how getting information from a file works I'd appreciate it greatly.

Thanks for taking the time to read and reply.

Frank


=-=-==--=

ifstream inFile;
ofstream outFile;

char inputFilename[] = "CTF.txt";
char attackString[] = "phaser.wav"; //default values if there's no file
char musicString[] = "music.ogg"; //default

inFile.open(inputFilename, ios::in);
outFile.open(inputFilename, ios::eek:ut);

//char stringRead[50];
attackString = getline(inFile,256,'\n');
getline(inFile, musicString);


=-==-=-==
 
There are two different forms of getline, one for C style strings like inputFilename, attackString, and musicString in your code above, and the other for the C++ string class. The code above attempts to use the string version.

Here is an example using the C style string version:
Code:
char input[256];
inFile.getline(input, 256);
Also be careful about what number you put in as the second parameter to getline. It should be the length of the array. In your code, musicString is only 10 characters long ("music.ogg" plus null terminator), and so you would have to use 10 for the call to getline. This might cut off your input if it is longer, so I would suggest using a longer input string and then copying the default value if there is no file.
 
I am not exactly sure what it is, but I heard somewhere that there is a slight bug within the Microsoft version of the getline function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top