-4

I have a task where I have to read different sections of an input file(.txt) of integers in c++. The file contains an unknown number of positive integers, each separated by white-space with several sentinel values of -1 placed randomly in the list to "break-up" the list into sections and another -1 at the end of the file.

Here is a sample of my input file(.txt):

    3 54 35 4 9 16 -1 14 57 32 4 6 8 41 2 -1 5 6 54 21 3 -1

Here is what I've attempted so far:

    int data[20],                       
        index = 0;

    ifstream fin;
    fin.open("data_file.txt");

    while (index < 20 && data[index] != -1 && fin >> data[index])
    {
        cout << data[index] << endl;
        index++;
    }

I can't get this to read past the first SV even if I repeat this while loop. It always just starts at the beginning of the file.

How do I read again STARTING AFTER the first SV to the second SV? The only methods I know involve reading a file from beginning to end. How do I read seperate sections?

Thanks in advance for any help, Cheers

Greg
  • 3
  • 5
  • You need to post what you have tried, or at least a list of hypotheses that you think are plausible. – Donald Sep 27 '15 at 20:19
  • Is the file text or binary? – Andrey Nasonov Sep 27 '15 at 20:20
  • Sorry Donald, I didn't know where to begin so I didn't post any code. Andrey, the file is text. Sorry, I forgot to include that. I apologize for not knowing who to post questions properly but I am very novice – Greg Sep 27 '15 at 20:24
  • Also, if a text file, then what do you mean by lists & sections? In a file, you have lines that may contain 1 long string, or numerous strings. – Donald Sep 27 '15 at 20:25
  • this file just has positive integers seperated by white-space and 3 sentinel values mixed in there to "break-up" the integers into a section like this: – Greg Sep 27 '15 at 20:29
  • 5 6 7 2 5 3 -1 8 6 4 7 5 -1 6 89 54 2 4 6 3 -1 – Greg Sep 27 '15 at 20:29
  • Read the file until it finds a -1. Something along the lines of `while (infile >> val && val != -1)` The first part reads into val and checks to see that you got a value from the file. The second checks whether or not you found a -1. – user4581301 Sep 27 '15 at 20:32
  • Thank you, but how do I go from there to read the numbers that follow the first -1. If I run that again, it will start at the beginning of the list. I need to start where I left off after the first read – Greg Sep 27 '15 at 20:37
  • Either just keep going after the first SV or store the SV location for later using `std::ifstream::tellg()`. – Galik Sep 27 '15 at 20:42
  • @Greg: _"I apologize for not knowing who to post questions properly but I am very novice"_ When you signed up to SO (just two weeks ago), you were given a Tour and linked to the Help Centre, which explains that to you. So being new is no excuse. – Lightness Races in Orbit Sep 27 '15 at 21:06
  • I read that and thought that I posted a clear question. What did I do wrong other than posting code that I knew would be wrong? I guess in hindsight, I could have posted what the input file looked like but other than that, I thought I was posting a relevant and clear question. Thanks for beating me with a stick master, I will always post code now even if it is jibberish. – Greg Sep 27 '15 at 21:14
  • To all, I have edited the question and included code to attempt to make it more clear. – Greg Sep 27 '15 at 21:46

1 Answers1

0

It sounds like you just want to group information from the file. I will not provide code since you didn't, but I may help you with the logic:

  1. Create a file object, 2d vector, and a string
  2. Read from the file object to the string
  3. if the value is equal to "-1", then add a new row. Else, add a new column

The result will be a 2d vector with the rows being each group, and the columns being each positive number in that group.

Donald
  • 633
  • 5
  • 16
  • Sorry again about the code, donald but I knew whatever I typed would be wrong. Is a vector the same as an array? – Greg Sep 27 '15 at 20:42
  • @ Greg They are both "containers". A vector is way more powerful and easy to use. I updated my answer with some links. – Donald Sep 27 '15 at 20:47