Coud any one give me a hand?

Aug 26, 2014 at 6:21am
How to read file in array if you know the number of rows but you dont know the number of columns?
Thank you!
Aug 26, 2014 at 6:43am
Read in the entire line then push them into a vector or some sort of dynamic array.


I don't know what data types you are reading in but I will assume they are integers. Though if not it is a very trivial task to change.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::vector<std::vector<int> > data;

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.push_back(tempData);
}



You could accomplish this with static/dynamic arrays too if you really wanted to but would be a little bit more complicated.
Last edited on Aug 26, 2014 at 6:44am
Aug 26, 2014 at 6:48am
Yes I'm reading integer type of files. If you going to write with comments its gonna be much better for me. And could you show me other way with dynamic arrays? Thank you!
Last edited on Aug 26, 2014 at 6:53am
Aug 26, 2014 at 6:54am
Just count the spaces in the entire line (str contains 1 line). This can be used to determine the number of "items" you must read in to the array. Also so you can create it dynamically. You could alternatively read in the line twice, the first time counting and the second time adding to the array. There may be a better solution but I can't really think of one.
Aug 26, 2014 at 6:56am
I've got:
1000 4 // 1000 means money, 4 means rows
and there is the prices in the shelfs:
3 15 360 8
5 2 1 145 99 300
4 700 600 900 800
2 8 1
And here I'm stick, coz I don't know how to read columns without exact number of columns
Last edited on Aug 26, 2014 at 6:58am
Aug 27, 2014 at 8:36am
Its not working...
Aug 27, 2014 at 8:46am
Well what exactly have you tried? How do we know what isn't working?
Aug 27, 2014 at 9:43am
#include <fstream>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <string>


using namespace std;

const char CDfv[] = "Duom.txt";
const char CRfv[] = "Result.txt";
const int CMax = 500;
//------------------------------------------------------------------------------

void skaityti (int & p, int & n, char kaina[CMax]);
void spausdinti (int p, int n, int kaina[CMax]);

//------------------------------------------------------------------------------
using namespace std;

int main()
{

int p;
int n;
char kaina[CMax];

skaityti (p, n, kaina);

return 0;
}

void skaityti (int & p, int & n, char kaina[CMax])
{

ifstream fd(CDfv);

string line;
int a = 0;
int b = 0;

while (getline(fd,line))
{

cout << line << endl;

}



}
How I need to find out how many integers there is in every row?
Last edited on Aug 27, 2014 at 11:50am
Aug 27, 2014 at 6:13pm
Like I said earlier you could either read in each row twice first time counting second time doing something with it or you could count the number of spaces. Kind of how you would count the number of words in a sentence.
Topic archived. No new replies allowed.