• 0

Problems reading files in C++


Question

for (int i = 0; i < 200; ++ i)

{

TheFile = "C:\\Documents and Settings\\Administrator\\Desktop\\Signal\\" + m_Symbol + ".txt";

MyFile.open(TheFile.c_str());

if (MyFile.is_open() == true)

{

// about to get here, but I can't read what is in the file

if (!MyFile) cout << "We have a problem" << endl;

getline(line, MyFile);

}

MyFile.close();

}

I'm basically running this loop every second. So I'm trying to open and close 200 files every second. When a file is present I can get past the MyFile.is_open() part, but I can't read what is in the file. "we have a problem" always comes up. Would anyone have a clue as to what I'm doing wrong?

Thanks

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

it would help to know what kind of object MyFile is. Also, from what I can see here, MyFile is declared somewhere else, could you be using it in another thread?

Link to comment
Share on other sites

  • 0

I don't know, I've rewritten it with a few logical fixes and it works, although I'm not sure why (!MyFile) would evaluate to true if you have been able to open the file, it doesn't make much sense to me.

Here's my working code :

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
using namespace std;

int main() {	

	string TheFile = "I:\\Frank Luna\\Chapter 9\\ColoredCubeDemo\\Vertex.h";
	string line;
	fstream MyFile;

	for (int i = 0; i &lt; 200; ++ i)
	{
		MyFile.open(TheFile.c_str());

		if (MyFile.is_open())
		{
			cout &lt;&lt; "File is open!" &lt;&lt; endl;
			// about to get here, but I can't read what is in the file
			if (!MyFile) {
				cout &lt;&lt; "We have a problem" &lt;&lt; endl;
			}
			getline(MyFile, line);
			cout &lt;&lt; line &lt;&lt; endl;

			MyFile.close();
		} 
		else {
			cout &lt;&lt; "Cannot open file " &lt;&lt; TheFile &lt;&lt; endl;
		}
	}
	system("pause");
}

I've made a few changes :

- getline(line, MyFile) changed to getline(MyFile, line) - it would simply not compile !

- MyFile.close() moved inside the if (MyFile.is_open()), because there's no point closing a file that's not open

- Added some messages for feedback

- Declared the variables outside the for loop

- Added system("pause")

Link to comment
Share on other sites

  • 0
I don't know, I've rewritten it with a few logical fixes and it works, although I'm not sure why (!MyFile) would evaluate to true if you have been able to open the file, it doesn't make much sense to me.

Here's my working code :

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
using namespace std;

int main() {	

	string TheFile = "I:\\Frank Luna\\Chapter 9\\ColoredCubeDemo\\Vertex.h";
	string line;
	fstream MyFile;

	for (int i = 0; i &lt; 200; ++ i)
	{
		MyFile.open(TheFile.c_str());

		if (MyFile.is_open())
		{
			cout &lt;&lt; "File is open!" &lt;&lt; endl;
			// about to get here, but I can't read what is in the file
			if (!MyFile) {
				cout &lt;&lt; "We have a problem" &lt;&lt; endl;
			}
			getline(MyFile, line);
			cout &lt;&lt; line &lt;&lt; endl;

			MyFile.close();
		} 
		else {
			cout &lt;&lt; "Cannot open file " &lt;&lt; TheFile &lt;&lt; endl;
		}
	}
	system("pause");
}

I've made a few changes :

- getline(line, MyFile) changed to getline(MyFile, line) - it would simply not compile !

- MyFile.close() moved inside the if (MyFile.is_open()), because there's no point closing a file that's not open

- Added some messages for feedback

- Declared the variables outside the for loop

- Added system("pause")

You do realize you basically just did his homework for him right?
Link to comment
Share on other sites

  • 0
You do realize you basically just did his homework for him right?
No, this was his code with a few mistakes corrected. Also, I doubt his homework (if this is homework, it's just an assumption on your part) really consist in printing 200 times the first line of a file. Even if so, he had done 95% of it already.
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.