I'm trying to read integers in a .txt file into a 2 dimensional array (map).

Here is the function:

void loadmap(int mapnum)
	{
		 int x;
		 ifstream inFile;

		 inFile.open("level1.txt");
		 if (!inFile) 
		 {
				cout << "Unable to open file";
				//exit(1); // terminate with error
		}


		for(int countline = 0; countline <= 9; countline++)
		{
			for(int count = 0;count <= 9; count++)
			{
				while(inFile >> x)
				{
				map[count][countline] = x;
				}
			}
		}
		memcpy(resetmap,map,sizeof resetmap);
		inFile.close();

	}

The problem is, the function does not seem to find the file (level1.txt). The file is in the root folder for the program. The console just outputs "unable to open file." and all the values in the array (map) default to 0.

Why might this be?

Recommended Answers

All 6 Replies

I don't think it doesn't find it but your method of checking whether it has is wrong.
Instead try

if (inFile.is_open())

I don't think it doesn't find it but your method of checking whether it has is wrong.
Instead try

if (inFile.is_open())

No joy with that, I've changed the code to:

void loadmap(int mapnum)
	{
		 int x;
		 ifstream inFile;

		 inFile.open("level1.txt");
		 if (!inFile.is_open()) 
		 {
				cout << "Unable to open file";
				//exit(1); // terminate with error
		}


		for(int countline = 0; countline <= 9; countline++)
		{
			for(int count = 0;count <= 9; count++)
			{
				while(inFile >> x)
				{
				map[count][countline] = x;
				}
			}
		}
		memcpy(resetmap,map,sizeof resetmap);
		inFile.close();

	}

But the result is the same. I've tried moving the file around in case it's in the wrong folder. In fact, it's now in every folder :P That hasn't fixed it either.

I'm trying to read integers in a .txt file into a 2 dimensional array (map).

Here is the function:

void loadmap(int mapnum)
	{
		 int x;
		 ifstream inFile;

		 inFile.open("level1.txt");
		 if (!inFile) 
		 {
				cout << "Unable to open file";
				//exit(1); // terminate with error
		}


		for(int countline = 0; countline <= 9; countline++)
		{
			for(int count = 0;count <= 9; count++)
			{
				while(inFile >> x)
				{
				map[count][countline] = x;
				}
			}
		}
		memcpy(resetmap,map,sizeof resetmap);
		inFile.close();

	}

The problem is, the function does not seem to find the file (level1.txt). The file is in the root folder for the program. The console just outputs "unable to open file." and all the values in the array (map) default to 0.

Why might this be?

I now have the file in every concievable folder the program could be reading from and it still doesn't seem to find it. I can't see the problem! Could it be that this is a fault with Windows 7 or VS2008?

Found the issue, turns out the file was saved as "level1.txt.txt"... Whoops :S

You can set windows to show all file extensions, whereas typically it will "hide extensions for known file-types" which is the folder option you uncheck.

You can set windows to show all file extensions, whereas typically it will "hide extensions for known file-types" which is the folder option you uncheck.

Ah that will prevent me making this mistake again! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.