#FILE handling

29 messages · Page 1 of 1 (latest)

turbid spire
#
#include <stdio.h>

int main()
{    
    // Opens file, if file does not exist, create it. Position at end
    FILE *pF = fopen("runcount.txt", "r+");
    if (pF == NULL)
    {
        fprintf(pF, "run #1\n");
        fclose(pF);
    }
    else
    {
        char runcounter[100];
        fscanf(pF, "%s", runcounter);
        int count = runcounter[6];
        count = count + 1;
        runcounter[6] = count;
        fprintf(pF, "%s", runcounter);
        printf("%s", runcounter);
    }
    
    return 0;
}```
jolly dockBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

turbid spire
#

I'm trying to make a run counter which replaces the contents of a .txt file with +1 each time it's run

#

and also print it to the terminal

#

any ideas where i might hav egone wrong?

#
  if (feof(pF) == 0)
#

I also tried that

#

How do i check if a file is empty?

twilit copper
# turbid spire ```c #include <stdio.h> int main() { // Opens file, if file does not ex...

// Opens file, if file does not exist, create it. Position at end

"r+"

Opens a file to update both reading and writing. The file must exist.
https://www.tutorialspoint.com/c_standard_library/c_function_fopen.htm

turbid spire
#

Yes but if it doesn't exist, i want to create it

#

so i can't use r+

#

There are two cases

  1. If it doesn't exist, create it and add the text
  2. If it exists, replace 1 integer from it
#

So open it with w for 1.

#

and r+ for 2

#

Since none applies, i went with a+

twilit copper
turbid spire
#

That is wrong..

#

that only checks if the file exists or not.

twilit copper
#

What ? look at the answers not the question

twilit copper
# turbid spire that only checks if the file exists or not.

you can do this

FILE *pF = fopen("runcount.txt", "a+");
if (pF != NULL) {
  fseek(pF, 0, SEEK_END);
  int size = ftell(pF);

  if (0 == size) {
  }
}

WIth a+ like you said to create if it doesn't exist, then check if properly open, then check if size is 0 (empty)

turbid spire
#

Lovely i got it to work

#

now i just have problem replacing the number

#
#include <stdio.h>

int main()
{
    // Opens file, if file does not exist, create it. Position at end
    FILE *pF = fopen("runcount.txt", "a+");

    fseek(pF, 0, SEEK_END);
    long size = ftell(pF);

    if (0 == size)
    {
        rewind(pF);
        fprintf(pF, "run #1\n");
        printf("run #1\n");
    }
    else
    {
        int runcounter;
        fscanf(pF, "run #%d", runcounter);
        runcounter = runcounter + 1;
        freopen("runcount.txt", "w", pF);
        fprintf(pF, "run #%d", runcounter);
    }

    fclose(pF);
    return 0;
}
#

For the love of god please someone

#

;-;

#

Why isn't it replacing the number?

#

and incrementing it

#

in my else

#

!solved