Hello this is simple and fast question. How do I make the while loop (the best way) to read a file ? I can do this (down below) but it's not good looking (for example what if I had like data1 to data10).
1 2 3
ifstream in("duomenys.txt");
int data1, data2;
while(in >> data1 && in >> data2)
Assuming your data is whitespace-delimited (spaces, tabs, newlines)
(for example what if I had like data1 to data10).
1 2 3 4 5 6 7 8 9 10 11 12 13
ifstream in("duomenys.txt");
int data[10];
for (int i = 0; i < 10; i++)
{
if (in >> data[i])
{
// success path
}
else
{
// failed to read in number, something went wrong
}
}
If you don't know how many pieces of data you have, use a vector.
e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Example program
#include <iostream>
#include <fstream>
#include <vector>
int main()
{
std::ifstream fin("t.txt");
std::vector<int> data;
int element;
while (fin >> element)
{
data.push_back(element);
}
}
Okay, it's a bit more complicated if you don't know in advance how many columns there are.
I would use getline combined with a stringstream to parse how many columns there are from the first line, and then make a vector of sequences (vector of vector) for each column. I'll post an example in a bit.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
int main()
{
using Sequence = std::vector<int>;
std::ifstream fin("d.txt");
// first goal: Figure out how many columns there are
// by reading in and parsing the first line of the file
std::vector<Sequence> sequences;
{
std::string first_line;
std::getline(fin, first_line);
std::istringstream iss(first_line); // used to separate each element in the line
int element;
while (iss >> element)
{
sequences.push_back(Sequence()); // add empty sequence
sequences.back().push_back(element); // insert first element
}
}
// First line and all sequences are now created.
// Now we just loop for the rest of the way.
bool end = false;
while (!end)
{
for (size_t i = 0; i < sequences.size(); i++)
{
int element;
if (fin >> element)
{
sequences[i].push_back(element);
}
else
{
// end of data.
// could do extra error checking after this
// to make sure the columns are all equal in size
end = true;
break;
}
}
}
// print results
for (size_t i = 0; i < sequences.size(); i++)
{
std::cout << "seq " << i << ":";
for (int elem : sequences[i])
{
std::cout << ' ' << elem;
}
std::cout << '\n';
}
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
usingnamespace std;
using TYPE = string; // the type of your data; use string if unknown
int main()
{
string filename = "duomenys.txt";
vector< vector<TYPE> > data;
ifstream in( filename );
for ( string line; getline( in, line ); )
{
stringstream ss( line );
vector<TYPE> row;
for ( TYPE d; ss >> d; ) row.push_back( d );
data.push_back( row );
}
cout << "Your data:\n";
for ( auto &row : data )
{
for ( auto &item : row ) cout << setw( 10 ) << item << ' ';
cout << '\n';
}
}
btw I purposefully avoided making a stringstream each time just as a self-challenge, but that is definitely conciser code. Although one difference is that it's on a row-basis instead of column-basis.
Each row in the text file must have the same number of values.
Ah yep
To be fair, NumPy is not part of the Python standard library. You could also find/make a C++ library that could call "loadtxt" to load in data from a file.