C++ Generating code
#include <sstream>
#include <fstream> #include <vector> #include <iterator> #include <algorithm> using namespace std; void put_into_vector( ifstream& ifs, vector<int>& v ) { string s; getline( ifs, s ); istringstream iss( s ); // not the fastest method ... copy( istream_iterator<int>( iss ), istream_iterator<int>(), back_inserter( v ) ); } int main() { vector<int> line_1, line_2; ifstream ifs( "C://data.txt" ); put_into_vector( ifs, line_1 ); put_into_vector( ifs, line_2 ); } Is there a way to read automatically each line of the data.txt depending on how many lines the file has? I can get the number of lines that it has with: int line_count = 0; char ch; ifstream iFile("c:/data.txt"); if (! iFile) { cout << "Error opening input file" << endl; system("pause"); return -1; } while (iFile.get(ch)) { switch (ch) { case '\n': line_count++; break; } cout << line_count; Thanks in advance :) |
You could always test to make sure that you're not at the EOF. I don't know how to do this in C or C++, but it's done like so in a few other languages:
Code:
Java |
It's similar in C++ as well:
Code:
if (!fstream.eof()) Alunduil |
Good to know. :)
|
All times are GMT -5. The time now is 05:43 AM. |