Maciej
Maciej

Reputation: 133

C++ Read from file and assign to a variables

i have a question. I have a file in format like this:

A B
C D
E F
X Y
X2 Y2
X3 Y3

I know how to assign A,B,C,D,E,F,X,Y (each file i check have this data) but sometimes i have more data like X2 Y2, X3, Y3 and i want to assing these also (without previous knowledge about how many of these are in the file).

Actually my code looks like this:

reading >> a >> b >> c >> d >> e >> f >> x >> y;

Could You help me? Thank You

Upvotes: 1

Views: 6889

Answers (5)

Dave
Dave

Reputation: 348

If I understood you well you want to be sure that all lines in the file must be read.

With the following code I solved the problem for me:

std::ifstream file_reading( filename_path_.c_str(), std::ios::in );
if( file_reading ) {

  std::string buffer;
  unsigned int line_counter = 0;

  while( std::getline( file_reading, buffer ) ) {

        std::istringstream istr;
        istr.str( buffer );

        if( buffer.empty() ) 
          break;

         istr >> x_array[local_counter] >> y_array[local_counter];

             line_counter++;
       }
}

using getline and a while loop you all get all lines in the file and store them in a std::vector which is resizable. The instruction break will quit if no more data are found in the next line.

Upvotes: 0

Christopher Snell
Christopher Snell

Reputation: 17

In the case where you don't know how much data is in the file, it is worth to use a WHILE loop with the condition that you perform the while loop until it reaches the end of the file.

Something like this (ensure you include: iostream, fstream, and string):

int main () {
    string line;
    ifstream thefile ("test.txt");
    if (thefile.is_open()) 
    {
        while (thefile.good() )
        {
            getline (thefile, line);
        }
        thefile.close();
    }
    else 
    {
        cout << "Unable to open\n"; 
    }
    return 0;
}

Upvotes: 0

Shahriar
Shahriar

Reputation: 13804

You can use 2D vector:

Input:

1 2 3
4 5
6
7 8 9

Code:

int  val;
string line;

ifstream myfile ("example.txt");

vector<vector<int> > LIST;

if (myfile.is_open())
{
   while ( getline(myfile,line) )
   {
      vector<int> v;

      istringstream IS(line);

      while( IS >> val)
      {
         v.push_back(val);
      }

      LIST.push_back(v);
   }

  myfile.close();
}

for(int i=0; i<LIST.size(); i++)
{
    for(int j=0; j<LIST[i].size(); j++)
    {
        cout << LIST[i][j] << " ";
    }
    cout << endl;
}

Output:

1 2 3
4 5
6
7 8 9

Upvotes: 0

Pranit Bankar
Pranit Bankar

Reputation: 473

You can input all of them as a string. Give the header as #input and use gets() function to input the entire input. Then work on the string. You can differentiate between the numbers by neglecting space(" ") and new lines ("\n"). Hope this helps!

Upvotes: 0

This solution with vectors might solve the problem:

#include <vector>
#include <string>
#include <iterator>
#include <iostream>
#include <fstream>

using namespace std;

void getItems(vector<string>& v, string path)
{
    ifstream is(path.c_str());
    istream_iterator<string> start(is), end;
    vector<string> items(start, end);
    v = items;
}

int main()
{
    vector<string> v;
    getItems(v, "C:\test.txt");

    vector<string>::iterator it;
    for (it = v.begin(); it != v.end(); it++)
        cout << *it << endl;

    return 0;
}

Note: here i am assuming that your file is in C:\test.txt

Upvotes: 5

Related Questions