dimanche 15 novembre 2020

Is there a way to read the numbers on consecutive specific lines in C++?

Let's assume I have a program which takes as an input a file (numbers.in) which contains on the first line a number n, on the second line n numbers separated using a space, and on the third line, some other n numbers separated by a space.

I do know how much n is, for I read it form the first line. I don not know how to jump on the second line and read ONLY the numbers on that line, then to thew third etc.

I want to save the numbers on the second line in a vector A in the same order they are on that line and in a vector B the numbers on the third line, as shown. So far I have this:

    #include <stream>
    #include <string>
    using namespace std;
    ifstream fin("numbers.in");
    ofstream fout("result.out");
    int main()
    {  int N;
    string line;
    fin>>N;
    long long A[N];
    unsigned long long B[N];
    for(int i=0; i<=N-1; i++)
    {  
        while (!fin.eof())
        {
            A[i]=getline(fin, );
        }
    }

    for(int i=0; i<=N-1; i++)
    {  
        while (!fin.eof())
        {
            B[i]=getline(fin, );
        }
    }

How do I do that?

Aucun commentaire:

Enregistrer un commentaire