C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-10-2009, 11:15 AM   #1
Registered User
 
Join Date: Mar 2009
Posts: 10
Question 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?
Tbrez is offline   Reply With Quote
Old 03-10-2009, 11:21 AM   #2
Registered User
 
Join Date: Sep 2006
Posts: 2,516
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).
Adak is online now   Reply With Quote
Old 03-10-2009, 11:22 AM   #3
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,135
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
C_ntua is offline   Reply With Quote
Old 03-10-2009, 11:34 AM   #4
Registered User
 
Join Date: Mar 2009
Posts: 10
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.
Tbrez is offline   Reply With Quote
Old 03-10-2009, 12:04 PM   #5
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,135
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.
C_ntua is offline   Reply With Quote
Old 03-10-2009, 02:36 PM   #6
Registered User
 
Join Date: Mar 2009
Posts: 10
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
Tbrez is offline   Reply With Quote
Old 03-10-2009, 03:40 PM   #7
Registered User
 
Join Date: Mar 2009
Posts: 10
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
Tbrez is offline   Reply With Quote
Reply

Tags
fgets, file, input, txt

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading .dat files from a folder in current directory... porsche911nfs C++ Programming 7 04-04-2009 09:52 PM
reading the nth item from a txt file Opel_Corsa C++ Programming 1 11-18-2006 05:58 PM
reading files hiya C++ Programming 7 05-21-2005 11:40 AM
Reading files in a directory roktsyntst Windows Programming 5 02-07-2003 10:04 AM
Need Advice in reading files jon C Programming 4 10-07-2001 07:27 AM


All times are GMT -6. The time now is 01:10 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22