Thread: Weird Ouput

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    25

    Weird Ouput

    Okay. I'm currently designing a small little "maze" game. Heading towards an ascii dungeon game, but keeping it simple for now. Simply, the program so far is meant to read in a text file containing the maze, copy to a terrain class which draws it to the screen class. The screen class then outputs the map. It reads it all in okay, but every time I output, the first line is all messed up. For example, a text file that contained:
    Here is some text.
    Here is some more.
    And here's a line.
    Outputs:
    O?¶a♦ is some more.
    And here's a line.
    I'm using notepad2 to edit the text and Cygwin to compile. I have tried both windows and unix format. I have tried all the different encodings. Has anyone else come across this? There's quite a bit of code so I won't post it until you ask for it. (Just in case it's not a programming issue.)

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd say the most likely culprit is that you're going past the end of some other data that in turn clobbers your string data. Try adding some debug prints to verify your string data at various places in the code, and narrow down the scope until you have one function (or a few lines), then analyze that bit of code.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    25
    Quote Originally Posted by matsp View Post
    I'd say the most likely culprit is that you're going past the end of some other data that in turn clobbers your string data. Try adding some debug prints to verify your string data at various places in the code, and narrow down the scope until you have one function (or a few lines), then analyze that bit of code.

    --
    Mats
    I was able to add debugging code to verify that the file is being read correctly. I tried adding a sort of std::cout << charBeingProcessed << std::endl; kind ok stuff. Nothing was output.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you are saying that the strings are read OK, but later on they are not OK, which is exactly the point I was trying to make - some other bit of code is overwriting your string.

    Alternatively, you are not storing the text correctly -keeping a pointer to a string on the stack of a function that is no longer active is a classic example of this - been done by almost every programmer ever to write string handling code, I would expect.

    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    25
    Well, the function which draws the terrain onto the screen takes a pointer to the screen class. I removed the pointer, and the output is different but still messed up. The problem is that I can think of heaps of places where this map data is being transfered and copied. I'm just having a hard time figuring out where it's wrong.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whenever you copy or otherwise process the data, add a printout to make sure it's OK. That's the best way to debug this sort of problem - or in a debugger, put a break on every function that processes the data and then check it at the end of the function - but that can be tedious if there's a lot of functions and a lot of data.

    --
    Mats

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    25
    I have tried to output data in some of the copying processes. It's not actually outputing anything.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by addle_brains View Post
    I have tried to output data in some of the copying processes. It's not actually outputing anything.
    Then your output isn't working right, so try fixing that first... ;-)

    --
    Mats

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    25
    I may have figured it out. Is there a way to reset the "read position" in a file so to the start?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In C- style file handling (fopen, fread, fgets, and friends): fseek(stream, pos, SEEK_SET)
    In C++ style file handling (fstream class): seekg(pos) member function.

    --
    Mats

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    25
    Ok. I managed to narrow it down where it's happening to a constructor:

    Code:
    #include <fstream>
    #include <string>
    #include <vector>
    #include <iostream>
    #include "terrain.h"
    
    
    using namespace Game;
    
    terrain::terrain (std::string fileName) {
    	
    	std::ifstream in(fileName.c_str());
    	
    	if (!in) {
    	
    		throw GameException("Cannot open terrain file.");
    	}
    	
    	else {
    	
    		std::string entry;
    		std::string longFile;
    		std::vector<string> stringVect;
    		
    		while (getline(in, entry)) {
    			
    			if (entry > longFile) {
    				longFile = entry;
    			}
    			
    			stringVect.push_back(entry);
    		}
    		
    		width = longFile.size();
    		height = stringVect.size();
    	}
    	
    	in.seekg (0);
    	terrainArray = new Array2D<char>(width, height);
    
    	char passTo;
    	while (in.get(passTo)) {
    		
    		for(
    			unsigned y = 0;
    			y < height;
    			y++
    		) {
    			for(
    				unsigned x = 0;
    				x < width;
    				x++
    			) {
    				terrainArray->Put(x, y, passTo);
    				
    			}
    		}
    	}
    	in.close();
    }
    The Array2D template was supplied by a lecturer, so it works. I've asked him about this issue, but all he said was to check my file format.
    Last edited by addle_brains; 08-18-2007 at 09:43 AM.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    			if (entry > longFile) {
    				longFile = entry;
    			}
    You are using "longFile" to determine the max size of the array, but you are comparing strings in alphabetica order, so, rather than comparing the length of the strings. So a shorter string will overwrite a longer one with alphabetically higher value, e.g. "Aardvark" < "Elk"

    --
    Mats

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    25
    Okay, i've changed that to
    Code:
    if (entry.size() > longFile.size()) {
    	longFile = entry;
    }
    Regardless, that didn't fix the problem.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Don't know for sure, but I think you may want to add one to the size to allow for the string "abc" being 'a', 'b', 'c', '\0' : 4 chars.

    And watch for newlines that may not be taken in the "getline", but are taken in "in.get" perhaps?

    I don't know what else may be the problem.

    --
    Mats

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> while (in.get(passTo))
    That code should never run. The previous loop with getline goes until the stream reaches a fail state (which will happen after you pass the end of the file). You never clear the fail state, so get won't get anything and that loop will never be entered. Call in.clear() before that loop.

    Then, once you actually get into the loop, you do have to account for the fact that getline discards newlines but get reads them in.

    >> I think you may want to add one to the size to allow for the string "abc" being 'a', 'b', 'c', '\0' : 4 chars.
    There is no null terminator in C++ strings, its not relevant (technically there might be one in a given implementation, but again, it is not relevant).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 11:23 PM
  2. weird
    By kiz in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:16 AM
  3. Weird Characters With GetDlgItemText
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2006, 04:53 PM
  4. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 07:32 AM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM