Error C2039 despite having header files

***UPDATE***
So i googled my error and determined error C2039 was a missing header file (that all i could find online) I have the required header files but it is still giving me the error:

Error 1: error C2039: 'getline' : is not a member of 'std::basic_ofstream<char,std::char_traits<char>>'

I still cannot figure out what i am doing wrong. Can anyone point me in the write direction?



I need to write a program to open a file and display it to the screen. Im having issues with line 39. I am not quite sure how to display each line of text in the file i have opened. And also how would i make the program only display 24 lines of the opened file at a time? I want the user to hit enter before i show more text from the opened file. I can also post the file if needed but its just basically a random story or something. Any help would be appreciated! thank you.



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
 //Chapter 12 Program 1
//Reads data from one file and displays it
//Written by 

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

using namespace std; 

//Global constant
ofstream shipText;

//Function prototype
void showFile(ofstream &);

int main()
{ 
	shipText.open("shipText.txt", ios::in);
	cout << " Opening file... " << endl; 

	//If file open failed notify the user
	if (shipText.fail())
	{
		cout << " There was an unknown error and the file could not be opened. ";
	}
	else //Display shipText
	{
		showFile(shipText);
		shipText.close();
		cout << " The file has been displayed and is now closed. ";
	}
}

//************************************************************************************************
//ShowFile function to display shipText                                                          *
//************************************************************************************************

void showFile(ofstream &file)
{
	string textLine; 
	int counter = 0; 
	char textCh();

	while (!shipText.eof())
		if (counter > 24)
		{
			textCh();
			counter = 0;
		}

	getline(shipText, textLine, '\n');
	cout << textLine << endl;
	counter++;
}
Last edited on
The getline() function expects an input stream not an output stream.

Why is shipText a global variable? You're passing it into your functions so it really should be local to main().

I originally had the shipText in main() but i got errors in my function saying shipText was undefined so i switched it to a global variable and it solved the problem.

And also im still confused by getline. I want to get the lines from the text and display them in my program (24 lines at a time). So would i use something like outfile.put(char)? But that only copies one character. Thanks for your help and response!

***EDIT*** i wrote a simpler version of the program and it compiles but does not display anything. Why is that? I also places the txt file it is suppose to read from in the same folder as the cpp file. Here is my code. Help on either code would be much appreciated!



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
//Chapter 12 Program 1
//Reads data from one file and displays it
//Written by 

#include <iostream>
#include <fstream> 
#include <string>
using namespace std;

//Global constant
fstream shipText;


int main()
{
	string input;

	shipText.open("shipText.txt", ios::in);
	cout << " Opening file... " << endl;

	//If file open successful
	if (shipText)
	{
		getline(shipText, input);
	
		while (shipText)
		{
			cout << input << endl;
			getline(shipText, input);
		}
		shipText.close();
	}
	else
	{
		cout << " Error File coule not be opened. ";
	}
	return 0;
}
Last edited on
Why is shipText a global variable? This is a program with a single function there is no need for the global variables. Also it is not a constant.

What does your file contain?

Sorry i posted the wrong code. I fixed those issues, but im still having the same error. I believe its happening before my while loop. this is the file i am trying to display in my program.


No one is unaware of the name of that famous English shipowner, Cunard.
In 1840 this shrewd industrialist founded a postal service between Liverpool and Halifax, featuring three wooden ships with 400-horsepower paddle wheels and a burden of 1,162 metric tons.
Eight years later, the company's assets were increased by four 650-horsepower ships at 1,820 metric tons, and in two more years, by two other vessels of still greater power and tonnage.
In 1853 the Cunard Co., whose mail-carrying charter had just been renewed, successively added to its assets the Arabia, the Persia, the China, the Scotia, the Java, and the Russia, all ships of top speed and, after the Great Eastern, the biggest ever to plow the seas.
So in 1867 this company owned twelve ships, eight with paddle wheels and four with propellers.
If I give these highly condensed details, it is so everyone can fully understand the importance of this maritime transportation company, known the world over for its shrewd management.
No transoceanic navigational undertaking has been conducted with more ability, no business dealings have been crowned with greater success.
In twenty-six years Cunard ships have made 2,000 Atlantic crossings without so much as a voyage canceled, a delay recorded, a man, a craft, or even a letter lost.
Accordingly, despite strong competition from France, passengers still choose the Cunard line in preference to all others, as can be seen in a recent survey of official documents.
Given this, no one will be astonished at the uproar provoked by this accident involving one of its finest steamers.
No one is unaware of the name of that famous English shipowner, Cunard.
In 1840 this shrewd industrialist founded a postal service between Liverpool and Halifax, featuring three wooden ships with 400-horsepower paddle wheels and a burden of 1,162 metric tons.
Eight years later, the company's assets were increased by four 650-horsepower ships at 1,820 metric tons, and in two more years, by two other vessels of still greater power and tonnage.
In 1853 the Cunard Co., whose mail-carrying charter had just been renewed, successively added to its assets the Arabia, the Persia, the China, the Scotia, the Java, and the Russia, all ships of top speed and, after the Great Eastern, the biggest ever to plow the seas.
So in 1867 this company owned twelve ships, eight with paddle wheels and four with propellers.
If I give these highly condensed details, it is so everyone can fully understand the importance of this maritime transportation company, known the world over for its shrewd management.
No transoceanic navigational undertaking has been conducted with more ability, no business dealings have been crowned with greater success.
In twenty-six years Cunard ships have made 2,000 Atlantic crossings without so much as a voyage canceled, a delay recorded, a man, a craft, or even a letter lost.
Accordingly, despite strong competition from France, passengers still choose the Cunard line in preference to all others, as can be seen in a recent survey of official documents.
Given this, no one will be astonished at the uproar provoked by this accident involving one of its finest steamers.
No one is unaware of the name of that famous English shipowner, Cunard.
In 1840 this shrewd industrialist founded a postal service between Liverpool and Halifax, featuring three wooden ships with 400-horsepower paddle wheels and a burden of 1,162 metric tons.
Eight years later, the company's assets were increased by four 650-horsepower ships at 1,820 metric tons, and in two more years, by two other vessels of still greater power and tonnage.
In 1853 the Cunard Co., whose mail-carrying charter had just been renewed, successively added to its assets the Arabia, the Persia, the China, the Scotia, the Java, and the Russia, all ships of top speed and, after the Great Eastern, the biggest ever to plow the seas.
So in 1867 this company owned twelve ships, eight with paddle wheels and four with propellers.
If I give these highly condensed details, it is so everyone can fully understand the importance of this maritime transportation company, known the world over for its shrewd management.
No transoceanic navigational undertaking has been conducted with more ability, no business dealings have been crowned with greater success.
In twenty-six years Cunard ships have made 2,000 Atlantic crossings without so much as a voyage canceled, a delay recorded, a man, a craft, or even a letter lost.
Accordingly, despite strong competition from France, passengers still choose the Cunard line in preference to all others, as can be seen in a recent survey of official documents.
Given this, no one will be astonished at the uproar provoked by this accident involving one of its finest steamers.

This is my code:


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
//Chapter 12 Program 1
//Reads data from one file and displays it
//Written by 

#include <iostream>
#include <fstream> 
#include <string>
using namespace std;


int main()
{
	//Variables 
	string input;
	fstream shipText;

	//Open the file
	shipText.open("shipText.txt", ios::in);
	cout << " Opening file... " << endl;

	//If file open successful
	if (shipText)
	{
		cout << " Opening the file was a success! ";
		getline(shipText, input);
		
		while (shipText)
		{
			cout << input << endl;
			getline(shipText, input);
		}
		shipText.close();
	}
	else
	{
		cout << " Error File coule not be opened. ";
	}
	return 0;
}
Last edited on
I can't reproduce your problems. The program seems to run and produce the desired output.

Do you get the error message "Error File coule not be opened"?

Also note your program could be simplified slightly:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main()
{
	//Variables
	string input;
	ifstream shipText("shipText.txt", ios::in);

	cout << " Opening file... " << endl;

	//If file open successful
	if (shipText)
	{
		cout << " Opening the file was a success! ";

		while(getline(shipText, input))
		{
			cout << input << endl;
		}
	}
	else
	{
		cout << " Error File coule not be opened. ";
	}
	return 0;
}


And note that depending on how you ran the program the current working directory can be at a location different that you seem to expect. If you're running the program through the command line then the file needs to be in the location of the executable which may be different than where the .cpp file is located.

Last edited on
Most of my problems came from my first code. My second code compiles but doesnt display anything. It displays "opening the file was a success" however once i press enter to continue the program closes. Im also running it through MS visual studio without debugging.

I see the changes you made to the if statement to make it more simple. It makes it a lot easier to understand! I need to improve my programming style. Thanks for your help!

My final question is now that my program has read the contents of ShipText how would i output that to a brand new file? Would i use outfile.put(input) after line 22 or between 29 and 30 (in my code) i didnt want to copy your work to turn in so i didnt make those changes.


And also THANK YOU SOOOOOOOOOOOO MUCH!!! You have helped me a lot and i truly appreciate you taking the time out of your day to help a struggling student :)
Last edited on
If you can print the information to the screen properly, then printing to a file is almost identical. Read the line then print the line using the output stream insertion operator<<.

That sounds simple enough. Thank You! I still wasnt able to see my program run properly. It still exits when i press enter after opening the file and not displaying anything. How did you run the code? Was it also through visual studio? I also placed the shipText file in the same folder as the .exe but still no luck.


***EDIT***
So i looked into my shipText document and realized i did not press save when i put the story in so it was just a blank document LOL thats why it was nnot displaying anything. I forgot to hit save twice! But thanks for your help!
Last edited on
Topic archived. No new replies allowed.