0

I am trying to get the amount of lines in a file in order to make an array to store the characters of each line. I think the problem is with the two while loops iterating at the same time.

#include <vector>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::string;

int main(int argc, char *argv[]){
    cout << "input file" << endl;
    string file;
    cin >> file;
    ifstream inFile;
    inFile.open(file, ios::in);
    cout << "success1";
    if (inFile.is_open()) {
        vector<double> inputs;
        string line;
        string s;
        int current;
        int sTotal;
        while(!inFile.eof()) {
            getline(inFile, s);
            sTotal++;
        }
        while (!inFile.eof()) {
            getline(inFile, line);
            cout << "success2";
            char *lineArr = new char[line.length()][sTotal];
            for(int j = 0;j < sTotal;j++){
                for (int i = 0; i < sizeof(lineArr); i++) {
                    lineArr[i] = line[i];
                }
            }
        }
    }else cout << "fail";
}

Any help would be appreciated. Im probably looking at this all wrong.

  • What is the actual problem you perceive? What do you expect the code to do, what does it do instead? As a new user here, please take the [tour] and read [ask]. Also, take a look at https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons. – Ulrich Eckhardt Apr 21 '21 at 07:22

1 Answers1

0

A main issue with your code is that you consume the stream in the first while loop and then try to do it again. They are not "iterating at the same time", they are two successive loops with the same exit condition. In virtually every case that results in the second one not executing at all.

Also, if you are allowing yourself to use str::string and std::vector, avoid using char*, it only adds confusion.

Try a single loop, using a vector<string> to push each line you get and print in the end the length of the loop. Have a look at these links: std::vector, getline

A rough sketch of what I mean:

vector<string> lines;
string line;
while(getLine(inFile, line)){
  lines.push(line);
}
cout << lines.size() << endl;

Then if you want to count the words per line, iterate again through the vector, and split each element; there are several ways to do that as well.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
Miguel
  • 1,288
  • 6
  • 18
  • Your loop can and should be simplified to `while(getline(inFile, line)){ lines.push(line); }` instead – Remy Lebeau Apr 20 '21 at 19:16
  • Sure it *can*, but not necessarily *should*. For the sake of clarity it doesn't hurt to make more explicit for when the loop stops. – Miguel Apr 20 '21 at 21:31
  • What is not explicit about the loop directly checking if `getline()` is successful? Checking `eof()` like you are does not handle a read failure, for instance, so you can get stuck in an endless loop. The way I showed is actually safer – Remy Lebeau Apr 20 '21 at 21:35
  • Well, I guess you're right there. I'm changing it then. – Miguel Apr 21 '21 at 06:20