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?

Dani AI

Generated

A few quick, practical checks and fixes that help with this kind of problem.

First, confirm where the program is actually looking for the file. Use an absolute path while debugging, or print the current working directory at runtime so you know which folder the relative name will be resolved against. Many IDEs use the project folder or a separate debug working directory, not the folder that contains your source or the one you expect. Also verify the exact filename the OS sees (hidden extensions, trailing spaces or different capitalization can all cause a mismatch).

Second, avoid draining the stream unexpectedly. Do one extraction per array cell and check the stream state immediately; reading “until EOF” inside an inner loop will consume the whole file on the first cell and leave nothing for subsequent cells. A robust approach reads each value once and reports an error if extraction fails or EOF occurs earlier than expected.

Example (modern C++ style; adapt for older compilers):

// read rows x cols integers into map; check for early EOF/bad data
std::ifstream in(path);
if (!in.is_open()) { std::cerr << "Cannot open: " << path << '\n'; return; }
for (int r = 0; r < rows; ++r)
  for (int c = 0; c < cols; ++c)
    if (!(in >> map[r][c])) {
      std::cerr << "Bad data or early EOF at " << r << ',' << c << '\n';
      return;
    }

Finally, build on the earlier replies: as pointed out, check the stream state; as suggested, be mindful of filesystem/OS quirks when naming files. If you still see open failures, try std::filesystem::exists (or the platform equivalent) to prove whether the file is visible to your process, and verify file permissions. These steps will isolate whether the issue is path/OS-related or a logic/reading bug.

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.