2

I am pretty new to c++. I am trying to read a file in line by line and store the input into several arrays. Because I don't know the size of input file, I have this to get the number of lines in the file

while (std::getline(inputFile, line)){
    ++numOfLines; 
    std::cout << line << std::endl;
}

Now I want to use the numOfLines as the size of arrays, but i cannot get it run by having this

std::string *firstName= new std::string[numOfLines];
std::string *lastName= new std::string[numOfLines];

for (int i = 0; i < numOfLines; ++i)
{      
    line >> firstName[i];
}

I guess it is because it has reached the end of the file after the while loop. But I do not know how to solve this problem. Is there a way to scan the input file in and store the value into array at the same time?

Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
Yuhe Zhu
  • 47
  • 1
  • 10
  • 4
    Your problem is using an array in the first place - use a std::vector and put the strings into the vector in a single pass. –  Sep 02 '18 at 23:29

2 Answers2

2

If you use std::vector you don't need to know ahead the lines count. You can use vector method push_back to insert new elements into it. Try use something like this:

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

using namespace std;

int main()
{
    std::vector<std::string> first_names;
    std::string line;
    ifstream input_file;

    while (std::getline(input_file, line)){
        first_names.push_back(line);
    }

    for (size_t i = 0; i < first_names.size(); i++) {
        std::cout << first_names[i] << std::endl;
    }

    return 0;
}
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
0

I don't know if you have ever taken a course related to Data Structures & Algorithms, in which you will learn to use Containers (such as: vector, deque, list, etc.) instead of Primitive Data Structures.

Please notice that although the follow example chooses vector as its container, it could vary according to different contexts. Say you are handling gigantic mount of data, you might want to use list instead`1,2,3.

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

// alias long type
// @see: https://en.cppreference.com/w/cpp/language/type_alias
using NameVector = std::vector<std::string>;

int handleLine(std::string line, NameVector &firstNames)
{
    // TODO implement your line handler here
    firstNames.push_back(line);

    return 0;
}

int handleFile(std::ifstream inputFile, NameVector &firstNames)
{
    std::string line;

    for (int lineNum = 1;

        // invoke `good` to check if there is any error
        inputFile.good()
        &&
        std::getline(inputFile, line);

        lineNum++)
    {
        std::cout << "Current line number : (" << lineNum << ")" << std::endl;
        std::cout << "Current line content: (" << line << ")" << std::endl;

        handleLine(line, &firstNames);
    }

    return 0;
}

int main()
{
    std::string path;                           // = R"(HERE GOES YOUR FILE PATH)";
                                                // Using **Raw string**
    std::ifstream inputFile { path };           // Initialize `inputFile`
    NameVector firstNames;

    handleFile(inputFile, firstNames);

    for (auto firstName : firstNames)
    {
        std::cout << firstName << std::endl;
    }

    return 0;
}
KaiserKatze
  • 1,521
  • 2
  • 20
  • 30