CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2007
    Posts
    19

    [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~
    Last edited by nixius; February 23rd, 2008 at 02:03 PM.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,526

    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()

  3. #3
    Join Date
    Nov 2007
    Posts
    19

    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!
    Attached Images Attached Images  

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,526

    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.

  5. #5
    Join Date
    Nov 2007
    Posts
    19

    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



HTML5 Development Center

Click Here to Expand Forum to Full Width