Help extracting line from text file

ok, so I've been running in circles for a while and I really need help with this.
How do I extract the line of text from the file? I can print the contents of the file but I can figure out how to extract the text line by line.

//
// main.cpp

//
//
#include<string>
#include <iostream>
#include <fstream>
#include <cmath>
#include<cstdlib>

using namespace std;
//------------------------------global variables
int NumberOfLines = 0;
//-----------------------------function declarations
string GetStudentList();
string GetClassHours ();
string OpenStudentFile (string);
void PrintLine (string);
//-----------------------------create structures
struct Student_Info {
string FirstName;
string LastName;
int StudentID;
int GPA;
};
//-----------------------------main body
int main()
{
string File1;
cout << "Welcome to the GradeCaculator Deluxe." << endl;
cout <<"Enter the file name for the list of Students, including the extension::" << endl;
getline(cin, File1);
OpenStudentFile(File1);
// GetStudentList ();
// GetClassHours ();
// getline(File1) ;
return 0;
}//end main
string OpenStudentFile(string File1)
{
ifstream inputFile;
string word;
inputFile.open(File1.c_str());
if(!inputFile.is_open())
{
exit(EXIT_FAILURE);
}
cout << "\nThe file has been successfully opened for reading.\n";
while (getline (inputFile, File1))
{
++NumberOfLines;
cout << File1 << endl; //prints contents of text file
}
cout << "Number of Lines in text file is: " << NumberOfLines << endl;//prints the number of lines in the text file

return File1;
}
Hi, here is my solution, it`s not to elegant, but maybe You can make something useful from this mess:
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<string>
#include <iostream>
#include <fstream>
#include <cmath>
#include<cstdlib>

#include <vector>// ******************************************NEW LINE***********************

using namespace std;
//------------------------------global variables
int NumberOfLines = 0;
//-----------------------------function declarations
string GetStudentList();
string GetClassHours();
string OpenStudentFile(string);
void show_lines(vector<string>&);//********************NEW LINE************************
void PrintLine(string);
//-----------------------------create structures
struct Student_Info {
	string FirstName;
	string LastName;
	int StudentID;
	int GPA;
};
//-----------------------------main body
int main()
{
	vector<string>lines;//************************************NEW LINE**************************

	string File1;
	cout << "Welcome to the GradeCaculator Deluxe." << endl;
	cout << "Enter the file name for the list of Students, including the extension::" << endl;
	getline(cin, File1);
	OpenStudentFile(File1);

	
	lines.reserve(NumberOfLines);//****************************************NEW LINE******************************
	//to avoid reallocation - vector size is set to number of lines
	//now content of text file is loaded into vector:
	ifstream inputFile;
	string word;
	inputFile.open(File1.c_str());
	while (getline(inputFile, word))
	{
		lines.push_back(word);
	}
	//show contents of separate lines, asking for line number; You can cut any line and write results to You structure
	show_lines(lines);//********************************NEW LINE****************************
	

	// GetStudentList ();
	// GetClassHours ();
	// getline(File1) ;
	return 0;
}//end main
string OpenStudentFile(string File1)//******************************vector<string>ln*********************
{
	ifstream inputFile;
	string word;
	inputFile.open(File1.c_str());
	if (!inputFile.is_open())
	{
		exit(EXIT_FAILURE);
	}
	cout << "\nThe file has been successfully opened for reading.\n";
	while (getline(inputFile, File1))
	{
		++NumberOfLines;
		
		cout << File1 << endl; //prints contents of text file**********************************excluded from program, contents in container
		
	}
	
	cout << "Number of Lines in text file is: " << NumberOfLines << endl;//prints the number of lines in the text file
	inputFile.close();//*********************NEW LINE***close stream**********************
	return File1;
}
//function to show contents of lines, by numbers, without error checking!!
void show_lines(vector<string>&ln_full)
{
	int vect_size = ln_full.size();
	cout << "Lines in file: " << vect_size << endl;
	cout << "Enter a number to show line:\n";
	int number = 0;
	while (number < vect_size)
	{
		cin >> number;
		cout << ln_full[number-1];
	}

}


I use Visual Studio 2013, and it works fine.
Thanks for responding. Unfortunately, I cant use vectors. I tried that route and the teacher got a little snippy with me for not following directions.
use an array then?
First of all you are doing loop wrong. your code should be:

while( getline( fileReader,lineIn ) ) {
}
Second, lines:

if( fileReader.is_open() )
and

fileReader.close();
are redundant. As for speed. I would recommend using regular expression:

std::regex reg ( "(value1#)|(value#2)|(value#3)(\\d+)" );
while( getline( fileReader,lineIn ) ) {
std::smatch m;
if( std::regex_search( lineIn.begin(), lineIn.end(), m, reg ) ) {
std::cout << "found: " << m[4] << std::endl;
}
}
Of course you would need to modify regular expression accordingly.

Unfortunately, iostreams are known to be pretty slow. If you would not get enough performance you may consider to replace fstream with FILE * or mmap.

http://www.besanttechnologies.com/company/corporate-training
Topic archived. No new replies allowed.