-1

i want to count file chars like this :

#include <stdio.h>
#include <stdlib.h>

int main(){
    int d=0;
    char filename[25];
    FILE *file;
    printf("Enter file name to be read (Text File!) : ");
    gets(filename);
    file = fopen(filename, "rt");
    if(file == NULL){
        printf("Failed to open file...");
        exit(1);
    }
    fseek(file, 0L, SEEK_SET);
    while( !feof(file) ){
        fseek(file, 1L, SEEK_CUR);
        d++;
    }
    printf("%d", d);
}

after that im printing d and it value is 0.. and the file dose have chars. about 150 chars..

RandomSeed
  • 29,301
  • 6
  • 52
  • 87
shlomi93
  • 1
  • 1
  • 1
    What is the `"t"` in the call to `fopen()` for? – alk Mar 15 '14 at 15:50
  • @alk: It's Windows standard for 'text' as opposed to 'binary' file. It isn't necessary; it is allowed on Windows. – Jonathan Leffler Mar 15 '14 at 15:56
  • 2
    When I run this on my Debian system, the `while` loop never terminates, because the `fseek()` call doesn't set the file's EOF flag. (Seeking past the end of a file isn't an error, since you can seek and then write to make the file larger.) – Wyzard Mar 15 '14 at 15:57
  • Ahyes ... that's been looong. Thanks @JonathanLeffler – alk Mar 15 '14 at 15:58
  • _For a text stream, either offset shall be zero, or offset shall be a value returned by an earlier successful call to the ftell function on a stream associated with the same file and whence shall be SEEK_SET.,_ – BLUEPIXY Mar 15 '14 at 16:01
  • From where please? @BLUEPIXY – alk Mar 15 '14 at 16:07
  • @alk [n1256.pdf](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) 7.19.9.2 The fseek function -4 – BLUEPIXY Mar 15 '14 at 16:10
  • Interesting, so you want to imply the way the OP uses `fseek()` is undspecified bahaviour? @BLUEPIXY – alk Mar 15 '14 at 16:14
  • @alk It is the use of non-standard. – BLUEPIXY Mar 15 '14 at 16:21
  • If the objective is to 'count chars', does it matter if a new line may be represented by 1 or 2 bytes? If no, simply use `fstat` to get the file *length*; if yes, include logic to check each code read. – Jongware Mar 15 '14 at 16:42
  • "rt" means "read text" – shlomi93 Mar 15 '14 at 20:52

3 Answers3

2

fseek() allows you to seek beyond the mark of EOF.

To get the file size, you could fseek(file, 0, SEEK_END) and then call ftell(file).

To read character by character, you could use for (i = 0; fgetc(file) != EOF; i++);.

Arjun Sreedharan
  • 11,003
  • 2
  • 26
  • 34
  • thats the strangest thing. im doing it like that: .. (i dont know how to add code tags...im new) for (i = 0; fgetc(file) != EOF; i++); and i get i = 0 but there is chars in the file.. is i fwrite it to array then all works great. but i wand to malloc the array. – shlomi93 Mar 15 '14 at 21:02
  • @user3423498 worked out well for me. here's the code i tested http://pastebin.com/ej9tyPzL – Arjun Sreedharan Mar 15 '14 at 21:20
  • did it like you said. fseek(file, 0, SEEK_END) and then call ftell(file). – shlomi93 Mar 15 '14 at 21:37
1

I'm wondering if the program ever returns, as per fseek()'s documentation feof() shall never return anything <>0:

A successful call to the fseek() function clears the end-of-file indicator for the stream


To determine the file size using fseek() do:

FILE * pf = ...;

fseek(pf, 0, SEEK_END);
long size = ftell(pf);
alk
  • 69,737
  • 10
  • 105
  • 255
  • ...thus making this program just a slightly odd example of [“while( !feof( file ) )” is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) –  Mar 15 '14 at 16:02
  • 1
    ftell : The return value is implementation-defined if the text stream. – BLUEPIXY Mar 15 '14 at 16:27
0

i did it like this at the end and it work.

fseek(file, 0, SEEK_END);

and then call ftell(file);

befor i tryed to do it like that :

for (i = 0; fgetc(file) != EOF; i++);

and it didnt worked...but now it is 0.0 Thank you all..!

shlomi93
  • 1
  • 1