Reading txt files
I'm very new to C and I'm learning about file IO. Right now I'm trying to read from a txt file and I know that the first thing in the txt file is some number, as large as the largest possible int value. I think I'm missing something because what I'm attempting to do is input it, with either fgets or multiple fgetc 's. Then I convert each individual byte or ASCII code to an integer, and by counting how many I read in, I can multiply each one by its respective weight and add them to produce the actual integer I'm after. This just seems overly complicated and I feel as though I'm missing a much easier way.
So my question reduces to:
What is the best way to read a multi-digit integer from a txt file and have the integer available to work with in one's program?
Yes, there is a much easier way. :)
Use fgets and put the line of text, into a char buffer.
Then use sscanf() on the buffer, to pick out your numbers, words, etc. You can get the whole number, all at once with sscanf(), but you have to be careful with it - it wants to quit the first time it runs into something it's not set up to handle as far as data goes.
fgets() is also good for safely getting data, without the fear of over-running your buffer (which will crash your entire program, usually).
Well, depends where exactly is the int value. You can use the scanf family, but fgets is safer. But to get right to the point:
1) Is the int just somewhere in the text file?
2) Is it separated with spaces, commas or something else from the rest of the text?
If you answer those we can give a solution. Details kind of matter in I/O. Look at the scanf functions by the way, even though they can create bugs
Thank you adak. That makes a lot of sense. I have read about sscanf but didn't put it together with file IO.
In terms of the int, it is the first thing in the file and is seperated by a blank line from the rest of the file. I think I'll manage with sscanf but I would love to hear about other solutions C_ntua.
In this case even scanf will work. But, better use fgets+sscanf for safety. There is an issue with overflow with sscanf if I am not mistaken, was mentioned in another topic. Thus strtoi or strtol (string to int/long) is better to use instead of sscanf. They are both really easy to use. But stroi/l are more appropriate , since they do exactly what you want. Have a slightliy better performance also. Details, details I know I know :)
Note that the biggest int is an unsigned long int.
Thanks a ton! That helps a lot.
One follow up question though
Once I've read in this int, I need to process the rest of the file. The file pointer supplied to fgets isn't incremented past the read int (I think). Is file positioning, such as fseek, the best method for moving past this int? Or is there some method where the file pointer will point past the previously read int?
Thanks a lot
Nvm on my follow up question. I was getting confused because the size of my buffer kept the fgets from reading in the \n so my next one was producing garbage. Thank you both for you help :)