Sat May 12, 2012 12:28 pm
Sat May 12, 2012 9:45 pm
std::ifstream file("fileName");
std::string line;
while(std::getline(file, line))
{
std::stringstream linestream(line);
std::string data;
std::string strOne;
std::string strTwo;
// If you have truly tab delimited data use getline() with third parameter.
// If your data is just white space separated data
// then the operator >> will do (it reads a space separated word into a string).
std::getline(linestream, data, '\t'); // read up-to the first tab (discard tab).
// Read the integers using the operator >>
linestream >> strOne>> strTwo;
}
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdlib> // for exit function
// This program reads values from the file 'text.txt'
// and echoes them to the display until a negative value is read.
int main()
{
ifstream indata; // indata is like cin
int readNum; // variable for input value
indata.open("text.txt"); // opens the file
if(!indata) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
indata >> readNum;
while ( !indata.eof() ) { //until end-of-file
cout << "Value is " << readNum<< endl;
indata >> readNum; // sets EOF flag if no value found
}
indata.close();
cout << "Finished Reading" << endl;
return 0;
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.