[SOLVED]using fstream and char arrays, reading junk from file
I have done some searching but because of the broad searching terms I have come accross countless websites that don't really deal with what it is I wish to know.
here is my code:
Code:
char currString[25];
ifstream levelFile("./Media/levels/level1.txt",std::ios::in);
while(!levelFile.eof()){
levelFile >> currString;
//levelFile.getline(currString,25);
if (currString=="##"){
//Ommited
} else if (currString=="Room_01"){
//Ommited
}
}
levelFile.close();
What I am trying to do is basically read some parameters from a .txt file to instantiate some objects later on.
I have multiple objects I want to instantiate from this one file, and I was planning on using "##" as a break between the objects. When I am debugging I also see garbage often before my actual text, such as instead of "Room_01" I might see something like "€@Room_01....".
Another problem I am having is that I have some parameters such as "-5" and then I may have paramater that is "matBoxMaterial", so if I load matBoxmaterial into currString first, when it comes to -5 I get
"-5xMatMaterial..."
and then all the other junk from the rest of the array. I guess my general question is, how can I read in just what is on that line? Or perhaps how can I clear the char array of garbage. I have contemplated using a terminator (like -5/0) but it just seems like I am missing something really obvious!
Any advice is always muchly appreciated.
nixius~
Re: using fstream and char arrays, reading junk from file
Without seeing a sample input file, it is difficult to see why you are
getting garbage, but the following is not correct:
Code:
if (currString=="##")
{
//Ommited
}
else if (currString=="Room_01")
{
//Ommited
}
That would be OK if currString was a std::string, but for char arrays
you should use strcmp()
1 Attachment(s)
Re: using fstream and char arrays, reading junk from file
The .txt is as follows:
Room_01
#
-250,45,0
8,100,500
box.mesh
Examples/HeatNoise
mat_boxMaterial
Box
#
-150,45,0
8,100,500
box.mesh
Examples/HeatNoise
mat_boxMaterial
Box
#
-50,45,250
408,100,8
box.mesh
Examples/HeatNoise
mat_boxMaterial
Box
#
-50,45,-250
408,100,8
box.mesh
Examples/HeatNoise
mat_boxMaterial
Box
#
-100,20,100
50,50,50
box.mesh
Examples/HeatNoise
mat_boxMaterial
Box
##
I am now using this:
if (strcmp(currString,"##")){
I attached a small screenshot, something doesn't look right at all!
Any more details you need let me know!
Re: using fstream and char arrays, reading junk from file
Are you sure that it is not the debugger that is giving you problems ?
What happens if you just output what you read in:
Code:
while(levelFile >> currString)
{
cout << currString << "\n";
}
levelFile.close();
Also, it would be safer to use a std::string instead of a char array.
That way you do not need to worry about going past the bounds
of the array.
Re: using fstream and char arrays, reading junk from file
yeah that seems to work!
I finished up with this code:
Code:
string currString;
currString="";
while(levelFile >> currString){
cout << currString << "\n";
if (currString=="Room_01"){
i=50;
} else if (currString=="box.mesh"){
i=66;
} else if (currString=="#"){
i = 90;
}
}
in debug it adds some junk when I mouse over, but the if comparissons work so I will just ignore it.
Thanks alot for your help!