简体   繁体   中英

getline with ints C++

I have a file

0 3 2

1 2 3

4 5 6

6 8 1

Where the first number for each line is the row, the second number is the column, and the third number is the data contained in that row, column. This will be a given [8][8] array so I have already initialized everything to 0, but how can I store each of these data values? For example, I want [0][3] =2 and [1][2] = 3. I would like to keep track of the line on which I found that row, col, and data value. So, how can I correctly insert these values into my 2-D array?

int rowcol[8][8];
    for (int i=0; i < 9; i++)
        for (int j=0; j < 9; j++)
        {
            rowcol[i][j] =0;
        }

 ifstream myfile;
int nums;
myfile.open(text.c_str());
while (!myfile.eof()) {
    myfile >> nums;
    numbers.push_back(nums);
}
for (int i=0; i < numbers.size(); i++)
{

   //Not sure what the best approach here would be and I'm not even sure if I should have done a vector...

}

Why do you read into numbers vector, why don't you directly write to rowcol when reading every line?

// Check myfile and not only myfile.eof()
int row, column, value;
while(myfile >> row >> column >> value) {
    rowcol[row][column] = value;
}

This code does not check that there are only 3 numbers in one line, depending on the requirements you might need to add a check for that.

Simply read the row, col val and update thw rowcol:

int rowcol[8][8];

for (int i=0; i < 9; i++)
    for (int j=0; j < 9; j++)
    {
        rowcol[i][j] =0;
    }

myfile.open(text.c_str());

while (!myfile.eof()) {
    int row, col, val;
    myfile >> row >> col >> val;
    rowcol[row][col] = val;
}

A better solution can be have a number that indicates the number of row:

// file struct
2
0 0 1
0 1 2

template< typename T >
inline T read( std::istream& is )
{
    T res;
    is >> res;
    return res;
}     

int rowcol[8][8];

for (int i=0; i < 9; i++)
    for (int j=0; j < 9; j++)
    {
        rowcol[i][j] =0;
    }

myfile.open(text.c_str());

const size_t COUNT = read<int>( myfile );

for ( int i = 0; i < COUNT; ++i )

    int row = read<int>( myfile );
    int col = read<int>( myfile );
    int val = read<int>( myfile );

    rowcol[row][col] = val;
}

Your code is invalid. If you defined an array one dimension of which has size equal to 8 then in a loop you should use condition i < 8 not i < 9 as you wrote

int rowcol[8][8];
    for (int i=0; i < 9; i++)
        for (int j=0; j < 9; j++)
        {
            rowcol[i][j] =0;
        }

Moreover such initialization can be done when the array is being defined

int rowcol[8][8] = {};

As for the code that reads records from the file then you should check that each line contains exactly three numbers and the first two numbers have acceptable values for indexes of the array.

you can use vectors

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

using namespace std;


vector<int> split(string line,char delm)
{
    vector<int> tokens;
    string num="";
    for(int i=0;i<line.length();i++)
    {
        char c = line[i];
        if(c == delm)
        {
            tokens.push_back(atoi(num.c_str()));
            num="";
        }else
        {
            num+=c;
        }
    }
    return tokens;
}

string text="file.txt";


int main()
{
    string line = "";
    ifstream infile;

    infile.open(text.c_str());
    vector<vector<int>> mydata;

    while (!infile.eof())
    {
        getline(infile, line);
        mydata.push_back(split(line,' '));
    }
    infile.close();

    system("pause");
    return 0;
}

if you want to convert it to array

int rowcol[8][8];
    for (int i=0; i < mydata.size(); i++)
    {
        vector<int> d = mydata[i];
        for (int j=0; j < d.size(); j++)
        {
            rowcol[i][j] =d[j];
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM