Hi,

I would appreciate any help with the problem I am having. I am trying to read text from a file (currently just 'yes' or 'no') and print the out the results in this format:
1 yes
2 no
3 no
etc.

I have been able to write a some code that will print out what is in the file and home many total lines are in the file, just not together like I was hoping. Eventually I would like to get it so it prints out just the no's and there corresponding line.

Here is my code for reading the file:

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

int main () {

	char buffer [256] ;
	fstream myfile("c:\\readFrom\\example2.txt", ios::in) ;

	//myfile.open("example2");
	if (!myfile){ 
		cout << "Unable to open file!" << endl;
		system("Pause") ;
		exit(1) ;
	} else {
	cout << "contents of file: \n" ;
	int count = 0 ;
	string line ;
	while (myfile.getline(buffer, 4)) 
		
		cout << buffer << endl ;

	cout << "\n\n***End of File*** \n" ;

	myfile.close() ;

	system("PAUSE") ;
	}
	return 0;

and to print lines (some of the below code I got from this forum :) ) :

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

int main() {

char buffer [256] ;
ifstream file ;
file.open("/Users/DC/Desktop/example2.txt") ;

if (!file) { 
cout << "Unable to open file." << endl;
}
cout << "Contents of File: " << endl ;
string line ;
vector<string> lines ;
while(file.getline(buffer, 100)) lines.push_back(line) ;
cout << buffer << endl ;
cout << "total lines: " << lines.size() << '\n';
//file.close() ;
}

Recommended Answers

All 4 Replies

Just count the lines as they are being read

std::string line;
int counter = 0;
while ( getline(myfile, line) ) 
{
    ++counter;
    cout << left << setw(4) << counter <<  line << "\n";	
}

Hi,

Let the input file be
Yes
No
No

I would make this program with the following logic.
I will open the input file, and store a line in a string variable (by getline function) and increment the counter variable, will do this till the end of file. Close the file.

Open the file again, take a variable (i for example), run a loop till it is less than counter variable, output i, and get the line from the input file, and output it. This will go on till i is less than counter. The ouput will be like this:

1 Yes
2 No
3 No

Now close the input file again. Open it again now. Now get another string variable and store the line/word I would like to compare with the line/word of the input file (like No). Run a loop again from j to counter, put the if condition in the loop (if (your_string == file_string)), output j and infront of it output the word/line. increment the j like

ifstream infile;

infile.open ("input.txt");

for (int j = 0; j < counter; j++)
{
    infile >> file_string;        //use getline function to get a line

    if (your_string == file_string)
    {
         cout << j << " " << your_string << endl;
    }
}

infile.close ();

I hope you've got the idea. Closing and opening the file again and again has the benefit, that when you open the file again, you start from the beginning of file. And do close it before opening it again, or you'll start from somewhere middle of the input file.
If you have to compare many words, put the for loop in a function and send that string in the function one by one.

Thank you both so much for your help!!

I was also able to print just the no's by adding an 'if' statement:

if (line == "no") {
		cout << counter << " " << line << endl ;
		}

Thanks again :)

Great news :) Now you can go on to more advanced problems.

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.