Thread: Help reading number at the end of file

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    Exclamation Help reading number at the end of file

    Hi to everyone, this is my first post in this site. I hope you'll be able to help me with mi little issue, as I've been trying to solve it for hours without results.


    The question is simple: I have a text file, and at the end of it there's a number. I want to be able to read that number, and use it as an int variable. I don't know the size of the number, only that it's at the end of the file.


    I'm in kind of a rush right now, as this is a school job, so every help would be appreciated.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, post whatever code you do have, and we'll help you. It might also help to post the text file (if it's not too big), or at least a small sample of the format so we know what the data looks like.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    2
    Quote Originally Posted by anduril462 View Post
    Well, post whatever code you do have, and we'll help you. It might also help to post the text file (if it's not too big), or at least a small sample of the format so we know what the data looks like.
    ok, the code is kinda big it, it's a big project. Basically, it represents a car washing system, where every user has a file in which some personal information is recorded.



    Basically, in the part I'm stuck with, the file will contain something like this:

    Code:
    saldo inicial:100
    26/1/12 20.21           A              95
    27/4/12 12.21           B              88
    29/4/12 06.05           C              80
    26/5/12 20.21           A              72
    As you can see, everytime the user makes a transaction, the date and hour is recorded, along with the kind of service they ordered and their remaining credits (last columns).What I want to read is that last number in the last line.If it helps, I can make that number to be at the beginning of the line instead, but I have no idea about how to read the number in a specific line, and much less about how to store it as an int in a variable...

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    EDIT: If the code is big, just post the relevant parts. How big is big anyway? A few hundred lines is not a big deal, though less is better if I don't need all of it.

    Okay, so since you have a text file, I would use fgets to read a line, then use sscanf to parse a line. What you want to do is keep reading lines until you reach the end of the file. When you do, fgets will return NULL, and your buffer will still contain the last line read, i.e. the last line of the file:
    Code:
    char buf[MAX_LINE_LEN];  // #define that to be a sensible number
    while (fgets(read into buf) != NULL) {
    }
    // fgets has return NULL, hopefully because you're at the end of the file.  Let's check
    if (feof(fp)) {
        if (sscanf(buf, pick a format string and parameters as necessary) == ???) {
            you found the last number of the last line, do something with it
        }
    }
    else {
        must be some kind of error reading the file, report it
    }
    I am leaving it up to you to read up on the fgets and sscanf functions to fill in the remaining parts of this. You are in school to learn after all, and me handing you a complete solution would rob you of that. A simple Google search should turn up the official documentation for the functions, and dozens (hundreds? thousands?) of tutorials on those functions.

    I will tell you, if you just want to skip the date, time and letter code columns in the file, I would use the string format specifier along with the assignment suppressor modifier to just skip over them.

    Give it your best shot, and post back if you have trouble.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file (one double number in each line)
    By zmarcoz in forum C Programming
    Replies: 13
    Last Post: 01-11-2012, 04:00 PM
  2. Reading large number from file
    By waxydock in forum C Programming
    Replies: 38
    Last Post: 04-14-2007, 05:12 AM
  3. Reading large number from file
    By waxydock in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2007, 05:08 AM
  4. reading a number from a file
    By the_head in forum C Programming
    Replies: 2
    Last Post: 10-02-2003, 09:25 PM
  5. Reading Certain number of bytes from a file
    By (TNT) in forum Windows Programming
    Replies: 6
    Last Post: 01-14-2002, 08:35 PM