Thread: Reading a file.

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    Reading a file.

    Hey ! I have this simple code to read a file and retrieve its content but it's not working properly (it ouputs default values instead of in-file values) = /

    Here's the content of the test file:

    Code:
        r    
    yyyyyyyyyyyyyyyy
    ggggggggggggg
    rrrrrrrrrrrrrrrrrrrrrr
    While stepping through my code with the debugger, it seems that it jumps over the while() condition loop but I don't get why. peek() isn't supposed to return EOF since I used seekg().

    Here's the function:
    Code:
    #include "game_load.h"
    
    const unsigned int* Board::OpenFileContent(const char* file_path) {
    	int cur_pos = 0;
    	std::string line;
    	std::ifstream file(file_path);
    	while(file.peek() != EOF) {
    		std::getline(file, line);
    		if(line.size() > LineLength)
    			LineLength = line.size();
    		LineNb++;
    	}
    
    	Array = new unsigned int[LineLength * LineNb];
    
    	file.seekg(0, std::ios::beg);
    	while(file.peek() != EOF) {
    		std::getline(file, line);
    		for(int i = 0; i < line.size(); i++) {
    			switch(line[ i ]) {
    				case 'g':
    					Array[cur_pos] = B_GREEN;
    					break;
    				case 'y':
    					Array[cur_pos] = B_YELLOW;
    					break;
    				case 'r':
    					Array[cur_pos] = B_RED;
    					break;
    				case ' ':
    				default:
    					Array[cur_pos] = B_EMPTY;
    			}
    			cur_pos++;
    		}
    
    		while(cur_pos < (cur_pos / LineLength + 1)) {
    			Array[cur_pos] = B_EMPTY;
    			cur_pos++;
    		}
    	}
    
    	return Array;
    }
    And here's how I use it:
    Code:
    #include "game_load.h"
    #include <iostream>
    
    int main() {
    	Board b;
    	const unsigned int* dat = b.OpenFileContent("test.dat");
    	for(int y = 0; y < b.GetLineNb(); y++) {
    		for(int x = 0; x < b.GetLineLength(); x++)
    			std::cout << dat[y*x + x];
    		std::cout << std::endl;
    	}
    }
    All of this is compiled using Microsoft's Visual C++ v7.1. Is there any obvious detail I didn't think of or is there any trick I am not aware of ?

    Edit: Closing the file and reopening doesn't change the results. The while() condition loop is still skipped. =/
    I also called fail() and it returns true which means a 'failure extracting from (the) stream'(C++ reference) occured.

    Also, calling exceptions() and catch()'ing exceptions gives me a segmentation fault which is weird...

    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    clear() reset flag bits

    check out vector no need peek()

    Kuphryn

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    I already tried clear() as well. No change at all. Forgot to say that.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Considering what you are doing with the data later on, you might just want to read the file character by character and make your life a bit easier, instead of peeking and grabbing whole lines.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    @Citizen: I find it easier to grab whole lines as I'm used to it. Thanks, though.

    It worked when I put clear() before seekg() which is logical when I think about it. However, now it outputs a lot of junk. Stepping through the debugger tells me that line contains the right string on the getline() line... but on the for() line (which is right after), it contains a lot of junk. What the hell does that mean ?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't understand what all this peek should be good for. Just use the normal way to read from a stream line by line

    Code:
    	while( std::getline(file, line) ) {
    		if(line.size() > LineLength)
    			LineLength = line.size();
    		LineNb++;
    	}
    Kurt

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    I didn't know about that trick, thanks.

    Apparently, what's messed up is how I show the content of the array. When I displayed it on a single line it displayed all the correct values. I'll have to check back my algorithm = /

    Thanks all !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM