-3

how can i find out how many characters are in a text file. I tried the following method:

#include <stdio.h>

    int main(){
      long size;
      fseek( fopen("file.txt", "r+a"), 0, SEEK_END );
      printf( "%ld", ftell( fopen("file.txt", "r+a") ) );

      return 0;
    }

But outputs 0(The text file is not empty)

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
MbProger
  • 15
  • 6
  • 2
    You seek and tell two different streams! – Some programmer dude Jan 05 '20 at 18:47
  • 3
    You're clearly reopening the file, why do you expect this to work?? – Jonathon Reinhart Jan 05 '20 at 18:48
  • How do i make it work? – MbProger Jan 05 '20 at 18:49
  • `fopen()` opens a file and returns a handle. Use the handle to further manipulate the file. You call `fopen()` twice and get two handles. It is like you open two different files. The current position in the first file is set to its end (`fseek(..., 0, SEEK_END)`) then you ask the current position of the second file, that is obviously `0`. – axiac Jan 05 '20 at 18:49
  • 1
    Note that the information is stale the moment you get it. The file could have been changed immediately after the seek. Without locking or other controls, you can only tell how many characters the file **had** a short time in the past, not how many it **has** now. – Eric Postpischil Jan 05 '20 at 18:52
  • 1
    Does this answer your question? [How do you determine the size of a file in C?](https://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c) – anatolyg Jan 05 '20 at 18:58

2 Answers2

0

Right now you're opening the file multiple times and each returned stream is independent.

You need to open the file once, and use that handle for all operations.

Also r+a is not a valid mode. If you just want to read, use r. If you want to do something else, state that in your question.

#include <stdio.h>

int main(void) {
  FILE *f = fopen("file.txt", "r");


  if (f == NULL) {
    fprintf(stderr, "Failed to open file\n");
    return 1;
  }

  fseek(f, 0, SEEK_END);
  printf( "%ld", ftell(f));

  fclose(f);
  f = NULL;

  return 0;
}
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • 1
    One thing to keep in mind - this will return the number of stored characters (bytes) in the file. Which is fine for everything but Windows. I don't believe it will account for the text mode translation of `\r\n` chars on disk to logical `\n`. I'm not sure what the OP wants to do or if this matters, but thought it worth calling out. – selbie Jan 05 '20 at 19:00
0

There is a good way to get the size of a file (so the number of characters since one character is one byte). But I'm not sure if it's cross compatible. Linux uses \n but windows uses \r\n, I'm not sure how stat count this. But here it is:

#include <stdio.h>
#include <sys/stat.h>

int main()
{
    struct stat sb;

    if (stat("test", &sb) == -1) {
            perror("stat");
            return 1;
    }
    printf("characters : %d\n", sb.st_size - 1);
    return 0;
}

stat takes the file path in first parameter (here test)

Lucas Gras
  • 961
  • 1
  • 7
  • 22