If you have a numbers in columns and rows, and you know just the number of rows it is 4, so how you need to find out every single column and write those numbers to array? I found the way just with string and then get line, but I don't know what to do next.. Any one could help?
First make sure that you're using the string overload for "getline()". Once you have the data loaded into a string, break it up with the "std::string.find()" and "std::string.substr()" member functions:
Then if you need to use the numerical data convert it with one of the "strto*()" functions in the 'cstdlib' header file. Or string streams, personally I would use one of the functions from the 'cstdlib' header. That part is dealers choice.
What does the data source look like? It doesn't have to be in English, but meaningful names for your variables would be helpful. Lithuanian isn't exactly a dead language and is easy enough to Google.
//------------------------------------------------------------------------------
using namespace std;
int main()
{
int p;
int n;
int price[CMax];
read(p, n, price);
return 0;
}
void read(int & p, int & n, int price[CMax])
{
ifstream fd(CDfv);
string line;
while (getline(fd,line))
{
cout << line << endl;
}
}
Basicly it's doesn't matter what they mean, I just need to find out how to read files to array's and then use them. That's it, sound's easy, but its not :D
Lets paste it here, except this time with a fixed number of rows (4), even though your previous thread did claim that you do read the number of rows from the input and therefore we cannot possibly know the number of rows ahead:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
constint Rows { 4 };
std::vector<int> data [Rows];
for ( int row = 0; row < Rows; ++row )
{
std::vector<int> tempData;
std::string str;
std::getline( in, str );
std::istringstream ss(str);
int temp;
while ( ss >> temp )
{
tempData.push_back( temp );
}
data[row] = tempData;
}