Reading txt files

This is a discussion on Reading txt files within the C Programming forums, part of the General Programming Boards category; I'm very new to C and I'm learning about file IO. Right now I'm trying to read from a txt ...

  1. #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?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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).

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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

  4. #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.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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.

  6. #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

  7. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread


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