file handling doubt in C++???.....

when a text file is given in this format which is ordered and has no commas or any characters-
60
Rajkumar
70
Harish
...

its clear that the file needs to first read an integer and then a string without encoutering any other character.

but if the file has an order like
ABB, 15
Acc, 15, 18, 20
...
or is unordered, how can the data be read knowing that the file pointer will encounter a string or a number??
You can read it as string anyway and then check the read string for its contents.

You can use isdigit and isalpha in <cctype> for this:
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/
http://www.cplusplus.com/reference/clibrary/cctype/isalpha/

Just be sure to check every char for getting correct results (not array of chars)
is the file content in the particular constant format ..( like )
input.txt
ABB , 15 18 , 10
CCA , 15, 19 , 90
GMM, 60 , 30 , 49


or


20 ,ABB , 15 18 , 10
39, CCA , 15, 19 , 90
45, GMM, 60 , 30 , 49

Last edited on
its like-

ABB, 8 , 9 , 10
BBA, 3, 4, 5,
CCA, 4, 6, 7, 9

there is a string name, followed by integers.

the objective of the program is to compare the numbers in two files under the same string name
like, the above text file is file 1 and the second file contains-

ABB-I, 7, 5, 4
BBA-II, 5, 6, 6
CCA-I, 3, 4 , 6 , 7

to understand the problem, let the strings be company names and the numbers that follow are the details of the companies opening, closing , nse, bse..etc.. prices for a day.

I need to take these two different files of day1 and day2 and compare( or manipulate) the numbers of the particular company.

But here is the basic problem, how do i identify and store the numbers when i read the file?
You could use string::find http://www.cplusplus.com/reference/string/string/find/ to find the comma, and then use substr http://www.cplusplus.com/reference/string/string/substr/ to pick out the data.
Read a line.
Then from that line read one string, and numbers till the end.
Just ignore the separator.
Topic archived. No new replies allowed.