UNABLE TO OPEN A TEXT FILE

Write your question here. Why am I unable to open the text file to read? Errors are in lines 24 and 28.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 // basic file operations
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() 
{
//	OPEN A TEXT FILE AND ENTER DATA
	ofstream myfile;
	myfile.open("example.txt");
	if (myfile.is_open())
	{
		myfile << "Writing this to a file.\n";
		myfile << "Writing second line to file.\n";
		myfile << "Writing third line to file.\n";
	}
	else cout << "Unable to open file!" << endl;
	myfile.close();

//	READ A TEXT FILE AND CLOSE FILE AFTER READING
	string line;
	ifstream myfile;
	myfile.open("example.txt");
	if (myfile.is_open())
	{
		while (!myfile.eof())
		{
			getline(cin, line);
			cout << line << '\n';
		}
		myfile.close();
	}
	else cout << "Unable to open file.\n";
	
	return 0;
}
 


You are redefining 'myfile' with a different type.
How's about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 // basic file operations
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int pause; // Global variable for pause usage

int main() 
{
//	OPEN A TEXT FILE AND ENTER DATA
	ofstream output_file;
	output_file.open("example.txt");
	if (output_file.is_open())
	{
		output_file << "Writing this to a file.\n";
		output_file << "Writing second line to file.\n";
		output_file << "Writing third line to file.\n";
	}
	else cout << "Unable to open file!" << endl;
	//output_file.close(); <----- Closed at end of program

//	READ A TEXT FILE AND CLOSE FILE AFTER READING
	string line;
	ifstream input_file;
	input_file.open("example.txt");
	if (input_file.is_open())
	{
		while (!input_file.eof())
		{
			getline(cin, line);
			cout << line << '\n';
		}
		//input_file.close(); <----- Closed at end of program
	}
	else cout << "Unable to open file.\n";

	// Close files
	output_file.close();
	input_file.close();
	
	// Universal pause usage, similar to Window's style
	// Window's version: System("pause");

	cin >> pause; // System("PAUSE");

	return 0;
}


Changed 'myfile' to 'output_file' and 'input_file'.
Give it a shot.
I changed "myfile" to "output_file" and "input_file" and no longer get an error message. The program ran. However, I now have a problem with the getline function. I changed it to getline(input_file, line) and it does not print any lines.
After a few aggravations and attempts, I've got it running correctly. The full code is below, followed by notes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Basic Input/Output Operations

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int pause; // Global variable for pause usage

int main() 
{

//-----------------------------------------------------------------------------
//	OPEN A TEXT FILE AND ENTER DATA

	ofstream output_file;
	output_file.open("example.txt", ios::beg | ios::trunc); // To file
	
	if (output_file.is_open())
	{
	 output_file << "Writing this to a file.\n";
	 output_file << "Writing second line to file.\n";
	 output_file << "Writing third line to file.\n";
	}

	else cout << "Unable to open file!" << endl; // Error message

	output_file.close(); // Close file

//-----------------------------------------------------------------------------

//	READ A TEXT FILE AND CLOSE FILE AFTER READING

	string line;
	ifstream input_file;
	input_file.open("example.txt", ios::beg); // From file

	cout << "Coming from file..." << endl << endl;
	
	if (input_file.is_open())
	 {
	  while(input_file.good() && !input_file.eof()) // Error check
	   {
		getline(input_file, line);
		cout << line << '\n';
	   }
	 }//end if(file.open)
	 
	else cout << "Unable to open file.\n"; // Error message

	input_file.close(); // Close file

//-----------------------------------------------------------------------------
	
	// Universal pause usage, similar to Window's style
	// Window's version: System("pause");

	cin >> pause; // System("PAUSE");

	return 0; // Success
}


Notes

I moved the closing of the files back to their original positions, I forgot that you were using the same file. I added, within your file openings, that the fstream read from the beginning on both, and for the output to file that it trunc, which means to delete the file, and re-write it (being a basic program, it shouldn't matter). As for the reading from file, I believe since you are reading from the same file, it left the flag at the end of the file, so you were reading in blank lines.

I put in error checking for the file at this line:
1
2
3
4
5
while(input_file.good() && !input_file.eof()) // Error check
	   {
		getline(input_file, line);
		cout << line << '\n';
	   }

It's good programming practice to do so, checking if the file is good and not at the end.
A few other changes in formatting to make it pretty and it should run smoothly.

elite zero,

Thank you for your hard work and troubleshooting. Here are my comments.

1. output_file.open("example.txt", ios::beg | ios::trunc);

a. I did not use ios::beg because it is not one of the modes in the fstream::open documentation.

b. I did not use ios::trunc either. If there was data in the file and I ran the program again, the existing data would be overwritten. There is a case for using ios::trunk; but,, for my situation, it was not applicable.

2. output_file.close(); // Close file I think this is why getline was not working. output_file and input_file were opened at the same time. When out_put file was closed, getline worked.

3. input_file.open("example.txt", ios::beg);

a. Similar to 1.a above, I did not use ios::beg for the same reason.

4. while(input_file.good() && !input_file.eof())

a. I do not see the value in adding input_file.good(). !input_file.eof() controls the Boolean expression logic.

Again, thank you very much for your help.


Topic archived. No new replies allowed.