-1

I want to read from a file containing an unknown list of 4 integers each. So each line of the file contain something like this"12.0 43.0 19.0 77.0" I thought of using a 2 dimensional array but I don't know the size of the file because its a very big file. I ended up reading each line as a string and used a "while(!in_file.eof())" so I can reach the end of the file but the problem is, I need to get the numbers individually form each line.

Here's a snippet of my code

Alan Birtles
  • 22,711
  • 4
  • 22
  • 44
  • [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – 463035818_is_not_a_number Mar 30 '21 at 04:03
  • ***"while(!in_file.eof())"** so i can reach the end of the file*. Unfortunately this often reads past the end of the file because it checks to see if you've reached the end of the file BEFORE reading and finding the end of the file. – user4581301 Mar 30 '21 at 04:04
  • 1
    12.0 43.0 19.0 77.0 arent integers – 463035818_is_not_a_number Mar 30 '21 at 04:05
  • 1
    Don't post images of code. They're hard to search, opaque to the visually impaired, blocked by firewalls, and are tricky to compile. Post the code as text. You'll find suggestions about how to format it so it doesn't look like garbage in the help linked from the question page. – user4581301 Mar 30 '21 at 04:07

1 Answers1

0

You don't need to know how many lines are in your file or how many numbers are in each line.

Use std::vector< std::vector< int > > to store data. If in each line, integers are separated with a space, you can iterate a string of integers and add them one by one to a vector:

#include <iostream>
#include <string>    
#include <stream>
#include <vector>
using namespace std;

int main()
{
    string textLine = "";
    ifstream MyReadFile("filename.txt");
    vector<vector<int>> main_vec;

    // Use a while loop together with the getline() function to read the file line by line
    while (getline(MyReadFile, textLine)) {
    
        vector<int> temp_vec; // Holdes the numbers in a signle line
        int number = 0;

        for (int i = 0, SIZE = textLine.size(); i < SIZE; i++) {

            if (isdigit(textLine[i])) {
                number = number * 10 + (textLine[i] - '0');
            }
            else { // It's a space character. One number was found:
                temp_vec.push_back(number);
                number = 0; // reset for the next number
            }
        }

        temp_vec.push_back(number); // Also add the last number of this line.
        main_vec.push_back(temp_vec); 
    }

    MyReadFile.close();

    // Print Result:
    for (const auto& line_iterator : main_vec) {
        for (const auto& number_iterator : line_iterator) {
            cout << number_iterator << " ";
        }

        cout << endl;
    }
}

Hint: This code works for only integers. For floats, you can easily identify the indices of spaces in each line, get substring of your textLine, and then convert this substring to a float number .

ArashV
  • 131
  • 7