0

I have a file input, in which i have the following data.

1 1Apple 2Orange 10Kiwi
2 30Apple 4Orange 1Kiwi

and so on. I have to read this data from file and work on it but i dont know how to retrieve the data. I want to store 1(of 1 apple) as integer and then Apple as a string. I thought of reading the whole 1Apple as a string. and then doing something with the stoi function.

Or I could read the whole thing character by character and then if the ascii value of that character lies b/w 48 to 57 then i will combine that as an integer and save the rest as string? Which one shall I do? Also how do I check what is the ASCII value of the char. (shall I convert the char to int and then compare, or is there any inbuilt function?)

3
  • Is this a question for your school?? You hv been posting such questions and smtimes deleting it immediately.. Commented Aug 22, 2011 at 18:28
  • If you can change the format of the file, use a character to separate the count and the fruit. Commented Aug 22, 2011 at 18:28
  • I am sure we dnt mind answering these questions but you should try and do it by yourself and it will help you in the long run.. Start posting code and show whatbyou have written and then start askin what can you improve on Commented Aug 22, 2011 at 18:30

2 Answers 2

1

How about using the fscanf() function if and only if your input pattern is not going to change. Otherwise you should probably use fgets() and perform checks if you want to separate the number from the string such as you suggested.

Sign up to request clarification or add additional context in comments.

3 Comments

-1 for suggesting fscanf. It is almost impossible to write a parser that behaves correctly on malformed input with *scanf.
@Zack: Did you miss the "and only if" part?
No, although I may have misconstrued it. I thought Milan meant the format of the data would not change in the future, but *scanf are only reliable when you don't have to worry about errors in the data, which is a much stronger constraint.
0

There is one easy right way to do this with standard C library facilities, one rather more difficult right way, and a whole lot of wrong ways. This is the easy right way:

  1. Read an entire line into a char[] buffer using fgets.
  2. Extract numbers from this line using strtol or strtoul.

It is very important to understand why the easier-looking alternatives (*scanf and atoi) should never be used. You might write less code initially, but once you start thinking about how to handle even slightly malformed input, you will discover that you should have used strtol.

The "rather more difficult right way" is to use lex and yacc. They are much more complicated but also much more powerful. You shouldn't need them for this problem.

2 Comments

The problem with atoi is that it does no error checking. The problem with *scanf is more subtle; it's ok in most (it won't choke if you try to interpret "hello" as a number, for example), but its behavior on numeric overflow is undefined.
Where did you get that scanf has UB on overflow? I know atoi does, but scanf should behave like strtol etc. on overflow unless I'm remembering horribly wrong (which is a possibility since I usually don't use scanf :-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.