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!

Reading from a file

Status
Not open for further replies.

FrankyTheFish

Programmer
Sep 7, 2002
14
0
0
CA
Hey guys quick question. I'm trying to read information from a file. This is the error I'm getting and below the code I'm using.

I'm trying to read any file name. The first file is for the song, the second file is for the sound effects. So the names have to be anything that the user wishes them to be.

Any ideas what I'm doing wrong?

Thanks for taking the time to look at this.

Frank

c:\Documents and Settings\FrancoisMessier\My Documents\Drive_F\Drive_F\SDL\sdlBitmaps.cpp(150): error C2440: '=' : cannot convert from 'std::basic_istream<_Elem,_Traits>::_Myt' to 'char [11]'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

char stringRead[255];
attackString = inFile.getline(stringRead, 255);
musicString = inFile.getline(stringRead, 255);
 
You need to separate the things you are doing. First, call getline with the stringRead parameter to get the string from the file. Then copy the string in stringRead into attackString. If attackString is a character array, you should use strcpy() or strncpy() in <cstring>.

The getline function doesn't return a string, which is why you can't use the code you have. You can check the return value of getline against a bool to see if the call succeeded or failed, or you can just ignore it and assume that it succeeded.

Also, remember that you might have problems when you try to copy the data into the attackString array if you haven't changed your code from your last post. My advice is to declare attackString as a 255 character array, and then copy the default value into it later if there is no file.
 
I've been trying to find some information on stringRead do you know of any sites or book would have info as to how to use it?

Thanks again,
Frank
 
stringRead is your variable name. You invent this name. What's info do you want to search in Internet about your artifact?..
 
char stringRead[255];
inFile.getline(stringRead, 255);
strcpy(attackString,stringRead);
inFile.getline(stringRead, 255);
strcpy(musicString,stringRead);

When I try this I get the same thing as before.
The first time I run the program it will display the music tring when I toggle the music on and off. When I turn my program off. Edit the file which contains the default file name and then turn it back on. It doesn't display anything meaning the string is empty.

This is what I've done:

//all includes are there

ifstream inFile;
ofstream outFile;

char inputFilename[] = "CTF.txt";
char attackString[] = "phaser.wav";
char musicString[] = "music.ogg";

void fileCheck();
void fileCreate();
void fileLoad();


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

if (! inFile)
{
cout << "Config File non-existant. Now Creating config file... Please wait..." << endl;
fileCreate();

}
else
{
cout << "Loading Config File... Please wait..." << endl;
fileLoad();

}
}

void fileLoad() {

char stringRead[255];
inFile.getline(stringRead, 255);
strcpy(attackString,stringRead);
inFile.getline(stringRead, 255);
strcpy(musicString,stringRead);
inFile.close();
}

void fileCreate() {


outFile << attackString << endl;
outFile << musicString << endl;
outFile.close();

}

I'm assuming it's the strcpy that's causing me a problem. But I'm really not sure. Any ideas on what I'm missing?
Again thanks for taking the time to look at this, it's greatly appreciated.
 
> inFile.open(inputFilename, ios::in);
> outFile.open(inputFilename, ios::eek:ut);
Opening the same file for input and output is probably not a good idea (like maybe you lose the contents?)

Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
I mentioned this twice already, but it looks like you didn't catch it.
Code:
char attackString[] = "phaser.wav";
...
strcpy(attackString,stringRead);
That code will lead to undefined behavior and very possibly a crash if the filename in stringRead is longer than "phaser.wav". The same is true for musicString and "music.ogg". The reason is that the first line of code only allocates space for a ten character string, but the second line will copy as many characters as there are in stringRead (which might be up to 254). The copied characters will overrun the buffer if there are too many of them.

Everything is working now because you're lucky, but I'd suggest fixing that problem sooner rather than later.
 
ooo yes, I had fixed that :) I was looking over all your posts and found and understood what you guys were saying :)

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top