8

In C++, I want to read one text file with columns of floats and put them in an 2d array.

First line will be the 1st column and so on.

The size of the array is unknown, it depends on the lines and columns that may vary.

I've tried with "getline", "inFile >>", but all changes I made have some problems.

For example, Is there a way to remove the unnecessary rows/lines after the values are there?

File looks like this (+/-):

  • chars "\t" chars "\t" chars "\n"
  • float "\t" float "\t" float "\t" float "\n"
  • float "\t" float "\t" float "\t" float "\n"
  • float "\t" float "\t" float "\t" float "\n"

Thanks

Till now I have this:

int ReadFromFile(){

ifstream inFile;   
ofstream outFile;

int nLinActual = 0;
const int nCol = 9;
const int nLin = 10;

// open file for reading
inFile.open("values.txt");  
// checks if file opened
if(inFile.fail()) {
    cout << "error loading .txt file reading" << endl; 
    return 1;
}   
// open file for writing
outFile.open ("outArray.txt");  
// checks if file opened
if(outFile.fail()) {
    cout << "error loading .txt file for writing" << endl; 
    return 1;
}

// Doesn't read the first line
string dummyLine, dummyLine2, dummyLine3;
getline(inFile, dummyLine);

// Declares Array
float values[nLin][nCol];

//Fill Array with -1
for(int l=0; l<nLin; l++)
    for(int c=0; c<nCol; c++)
        values[l][c] = -1;

// reads file to end of *file*, not line 
while(!inFile.eof()) {

    for (int i=0; i<nCol; i++) {
        inFile >> values[i][nLinActual];
    }
    i=0;    
    ++nLinActual;
}

// Check what's happening
cout << endl;
for(int l=0; l<nLin; l++){
    for(int c=0; c<nCol; c++){
        cout << values[l][c] << "\t";
        outFile << values[l][c] << "\t";
    }
    cout << endl;
    outFile << endl;
}

inFile.close();
outFile.close();


return 0; 

}

3
  • 1
    Best to post some code and give more details about the "problems" you had with it. Commented Sep 30, 2013 at 19:56
  • What does the file look like? Can you edit your post? Commented Sep 30, 2013 at 20:06
  • My code works now, please see below. Commented Sep 30, 2013 at 20:20

2 Answers 2

8

The easiest way is to use a vector:

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

int main()
{
    std::fstream in("in.txt");
    std::string line;
    std::vector<std::vector<float>> v;
    int i = 0;

    while (std::getline(in, line))
    {
        float value;
        std::stringstream ss(line);

        v.push_back(std::vector<float>());

        while (ss >> value)
        {
            v[i].push_back(value);
        }
        ++i;
    }
}

Update: You said you needed it to be done using raw C arrays. Sure, this can be done:

int main()
{
    std::ifstream in("in.txt");
    std::string line;

    float v[9][10];
    int i = 0, k = 0;

    while (std::getline(in, line))
    {
        float value;
        int k = 0;
        std::stringstream ss(line);

        while (ss >> value)
        {
            v[i][k] = value;
            ++k;
        }
        ++i;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

shouldn't you increment the counter outside inner loop?
and now you missed a semicolon :)
Is it possible to do with arrays? I've posted my code till now
But how I can do it without knowing the size of each array?
2

I think this might help you. Use a vector of a vector of type float as you are not aware of the count of number of items. This code assumes that you have a float numbers separated by space at each line.

fstream fs;
fs.open("abc.txt",ios::in);
vector<vector<float>> floatVec;
string strFloat;
float fNum;
int counter = 0;
while(getline(fs,strFloat))
{
    std::stringstream  linestream(strFloat);
    floatVec.push_back(std::vector<float>());
    while(linestream>>fNum)
        floatVec[counter].push_back(fNum);
    ++counter;
}

3 Comments

Normaly the numbers in the text file are separated by tabs or some blank spaces. Is it possible to do it with arrays?
@0x499602D2 actually it compiled for me but threw an exception. thanks for pointing it out!!
@JMG although it is possible but you shouldn't be using arrays when you are not sure of the dimensions. just declare the max sized array and use 2 indexes for dimensions in the loops itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.