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!

What's the format of the file? Is the number being read as a string or an int? Is the file being opened as binary or text? There are a number of things that could throw a wrench into any solution, so you need to provide more information.

ok, I'll try to be as specific as possible:

The program is supposed to be representing a car washing center, on which people is able to buy different kinds of tickets. The file I'm trying to read logs some information, such as the date of the last washings, some info from the user, and so on.

The file is opened as a text, and I know that the number I want to read (the user's remaining credit) is the last thing saved in the file each time it's changed. I want the number to be either read as an int,or as a string and then transformed into an int, whatever is easier, I just want to be able to operate with it in a variable.

Thanks for your help!

Well, presumably the number is on the last line of the file by itself, so a general solution would be to extract the last line:

FILE* fp = fopen("file", "r");

if (fp)
{
    char line[BUFSIZ] = "";

    while (fgets(line, sizeof line, fp))
    {
        /* Nothing to do here */
    }

    if (line[0] == '\0')
    {
        fputs("Remaining credit not found\n", stderr);
    }
    else
    {
        /* Using atoi() and no error checking for brevity only, not recommended */
        printf("Remaining credit: %d\n", atoi(line));
    }

    fclose(fp);
}

The problem is that the line will contain more things than just the number.

The file format will be something like this:

 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, the lines are variable, but the number remains at the end of the line. Or, I can modify the program so the number is at the beginning of the line, it that helps...

The problem is that the line will contain more things than just the number.

Then you need to parse the line to extract the number. This is why I asked what the format of the file was in my initial reply. Unfortunately, I notice that you left out the last line, which I assume is the remaining credit while the file starts with a beginning credit. So I can't say how to parse the last line...

Almost enough details for understanding....

Do you have a function defined to read a line and do something with all the other data yet?

Let's see how many more questions we need to ask.

what do you know ? if you know that it is delimited with space before then read it from end of file till space or read some size and parse and then atoi look Maximum value for a variable of type int. 2147483647

for example i could fseek to end then fseek till space from end and read till end from this space this way you can even know how many bytes you will read

by the way i reccoment you storing it in binary format 4 bytes, this means you memcpy integer to buffer and write this buffer to file, then memcpy those bytes back to integer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.