+ 1

Working with files. Help please. C++.

As written from a file like this: —————————————- text.txt: FirstMassive: 3 8 1 0 5 SecondMassive: 8 3 5 1 8 —————————————- How to make that from a text file the numbers are written into an array while ignoring the first word in the line (the name of the array). That is, the program must write numbers from the text file into an array, but ignore the first word in each line. Working on c++.

5th Sep 2018, 8:22 PM
Глеб
Глеб  - avatar
13 Answers
+ 2
Почитай про регэкспы (regexp). Это очень мощный инструмент по разбору строк. Если пользоваться "подручными" средствами, то можно пропустить в строке всё до : и вычитывать потом числа. Посмотри ещё на stringstream. С его помощью можно "перечитать" вытащенную из файла строчку.
6th Sep 2018, 6:18 AM
Sergey Ushakov
Sergey Ushakov - avatar
+ 2
Глеб Can I see the code you are working on? I don't want to write something different with what you do.
6th Sep 2018, 5:02 AM
Ipang
+ 2
Okay, when you are done with code for reading file as string, let me know, I'll get back to you later.
6th Sep 2018, 5:11 AM
Ipang
6th Sep 2018, 7:58 PM
Sergey Ushakov
Sergey Ushakov - avatar
+ 1
I don't know c++ well but you should jump one line and then split the second line beetween spaces and get all values after the first one (excluded) and then repear the same thing for the second line
5th Sep 2018, 8:27 PM
Daniele Bonomi
Daniele Bonomi - avatar
+ 1
I see 3 8 1 0 5 and 8 3 5 1 8 on the example, five numbers on each line. Are all the rows equally contain 5 numbers? are there rows with less or more than five numbers? if so, then dynamic container such as vector would be more suitable opposed to array. Also are all these numbers single digit as shown in the example?
5th Sep 2018, 9:19 PM
Ipang
+ 1
Глеб Is it possible that any of the number is more than one digit? e.g. FirstMassive 14 63 7 24 9 SecondMassive 8 23 5 1 48 And is there a variety in count of number per line? I mean is there a possibility a line has more or less than five numbers? e.g. FirstMassive 3 8 1 0 5 SecondMassive 8 3 5 1 8 7 ThirdMassive 6 3 9 1 Also, it would help if you post the code you are working on so we could solve this?
6th Sep 2018, 4:46 AM
Ipang
0
I perfectly understand both vectors and arrays. it is important for me to make it so that by using the fstream library I could ignore the first word in each line and write down the number that stands after this word in some variable in my program
6th Sep 2018, 4:33 AM
Глеб
Глеб  - avatar
0
Yes, maybe more than 1 number. About the array, I said for an example. to me the fact of missing the first word is important.For example: text.txt: [Speed]: 500 [Health]: 1000 We must record the number of speed and lives in the variable speed and hp, respectively. Make sure that the words [Speed]: and [Health]: are ignoring in the notebook. How to do it.
6th Sep 2018, 4:56 AM
Глеб
Глеб  - avatar
0
I have not had time to write anything grandiose: https://code.sololearn.com/c5ZSPF542984/?ref=app
6th Sep 2018, 5:06 AM
Глеб
Глеб  - avatar
0
English please
6th Sep 2018, 6:43 AM
Daniele Bonomi
Daniele Bonomi - avatar
0
Sergey Ushakov , если вам не трудно могли бы вы предоставить простой пример того что вы сказали
6th Sep 2018, 7:07 AM
Глеб
Глеб  - avatar
0
Спасибо, буду разбираться.
6th Sep 2018, 8:08 PM
Глеб
Глеб  - avatar