Recently I started learning C ++ and met this problem:

File input.txt

5 7 1 6 4 3 4 4 3 4 5 6 7 8 

Do not tell me how to write 5 elements from the 3rd line of the input.txt file and 7 from the 4th line into 2 arrays.

  • Describe the format of the source data in more detail. - gecube

1 answer 1

  1. Create arrays
  2. Open file
  3. Skip line
  4. Skip line
  5. Read numbers to the end of the line in the first array
  6. Read numbers to the end of the line in the second array
  7. Close file

For the case of a single line in the file:

 in.open(f); char z; int i = 0; while(!in.eof()) { in >> z; a[i++] = z - '0'; } in.close(f); 

The case of a few lines remains to you as an exercise.

  • I doubt that the answer is correct. It is not taken into account that in this example the number of elements on the lines is indicated. At least, such a feeling. But this is a matter of the format of the source data. Further, the issue of working with "rubber" arrays is not considered. - gecube