I have a file called fruit.txt like this
apple 2 in place A
banana 3 in place B
peach 4 in place C
what is the total?
then I want to use c++ to read this file line by line but I only want the fruit name and number and omit other information. For example, the first line, I want to read "apple" and write into string name[i]
and read 2 and write into int num[i]
and omit the "in place A". Keep this process until the end but also omit the last line "what is the total".
The following headers are allowed: <iostream>, <fstream>, <sstream>, <iomanip>, <string>, <cstdlib>.
So how to implement this in C++?