Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::ifstream not working?

Started by tendifo Jul 9, 2009 at 1:03 PM 5 replies 1.5k views
Original Post
tendifo
tendifo
Some code of mine doesn't seem to be working (it compiles and runs, but doesn't output the right thing). I recreated the problem with the following code (I stripped out all the irrelevant code).
#include <fstream>
#include <vector>

int main()
{
	std::vector < std::vector < bool > > h;
	std::ifstream in;
	std::ofstream out;
	int temp;
	
	for (int x = 0; x < 1024; x++)
	{
		h.push_back(std::vector < bool > ());
		for (int y = 0; y < 768; y++)
		{
			h[x].push_back(false);
		}
	}
	
	in.open("h.txt");
	for (int y = 0; y < 768; y++)
	{
		for (int x = 0; x < 1024; x++)
		{
			in >> temp;
			if (temp == 1)
			{
				h[x][y] = true;
			}
			else
			{
				h[x][y] = false;
			}
		}
	}
	in.close();
	
	out.open("whattheheck.txt");
	for (int y = 0; y < 768; y++)
	{
		for (int x = 0; x < 1024; x++)
		{
			if (h[x][y] == true)
			{
				out << "1";
			}
			else
			{
				out << "0";
			}
			
			if (x < 1024 - 1)
			{
				out << " ";
			}
		}
		out << std::endl;
	}
	out.close();
	
	return 0;
}

The file, h.txt, contains a huge array of 0s and 1s (768 lines with 1024 1s and 0s). I read them into an array (a vector of vectors). Then I try writing them back out into the file, whattheheck.txt, in the same format. Every time I run this, I get all 0s, even if the input file had a large number of 1s. What could be causing this? Should I try something besides a vector of vectors? Am I reading it in wrong? FYI, I'm running it on a SGI Octane2 running IRIX. Thanks. Edit: If it's not already obvious, this is C++.
alvaro
alvaro
The line `in >> temp;' reads in an integer from the file, in decimal. If you want to read a single character you shouldn't use an int variable.

Antheus
Antheus
1101010101010111010110... is one single int which overflows.

Try 'char temp'.
CaspianB
CaspianB
Are there spaces between the 1s and 0s in the in.txt file?

If not, then your "in >> temp" is going to try and read all of them at once into the integral variable.
tendifo
tendifo
Quote:
Original post by alvaro
The line `in >> temp;' reads in an integer from the file, in decimal. If you want to read a single character you shouldn't use an int variable.


I know. The format I'm using goes kind of like this..

0 0 0 1 1 1 ...
0 1 0 0 0 0 ...
...

The 0s and 1s are characters (.. with spaces as delimiters).

If I understand correctly how ifstream works, then "in >> temp" will read either a 0 or a 1 into temp, which is what I want.

Hopefully that makes sense.
CaspianB
CaspianB
I created a quick .txt with your specifications (1s and 0s, 1024 1s or 0s per line, 768 lines, one space between each value) and ran it through the code you listed. It created a matching file.

Are you sure your h.txt file is being opened properly? Check the value of "in" after the open call to ensure it's actually opening the file properly.

...in.open("h.txt");if ( ! in ){   std::cout << "Error opening h.txt" << std::endl;   return 1;}...
alvaro
alvaro
Why didn't you say there were spaces in the file in your first post?

In any case, if you suspect that ifstream is not working, make a test where you simply read the first two numbers and print them out, without the loops, the std::vector or anything else. If that works, keep incorporating elements little by little, testing often. When you see that it stops working as you would expect, it probably means the mistake is in whatever the last piece you added was. Then try to devise a trivial test for that part.

With this process you'll probably figure it out yourself. If you don't, at least you'll have a very short program to post so we can all see what's wrong with it. And if there is nothing wrong with it, you'll have a very short program that exposes a compiler bug (unlikely, but it happens from time to time), so you can report it back to the compiler provider.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.