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() ;
}