How do I tell how many bytes are in a file?

also is EOF 1A like it is in assembly using windows?
For your first question: http://www.cplusplus.com/forum/windows/10853/

EOF is usually -1, but your best best is to use std::fstream::good() (C++ I/O) or ferror() (C I/O) or to compare it with the macro EOF (defined in stdio.h/cstdio).
Last edited on
Let me explain more I want to make a program that reads in four bytes at a time and I need to test to see while fileRemaining(in bytes) >3
Last edited on
Well, using fseek/ftell/rewind every four bytes would be slow, you could try something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
size_t file_remaining(FILE* stream, size_t read)
{
        static size_t size = 0;
        if (!(size)) {
                fseek(stream, SEEK_END, 0);
                size = ftell(stream);
                rewind(stream);
        } else {
                size -= read;
        }

        return size;
}


Usage:
1
2
3
4
5
6
7
FILE* file = fopen("myfile.txt", "r");
size_t read = 0;
size_t buffersize = file_remaining(file, 0);
char* buffer = malloc(buffersize);

while (file_remaining(file, read))
        read += fread(buffer, 1, buffersize, file);
Last edited on
is the EOF counted as a byte when I read in? I don't want it to be counted if it is.
Last edited on
You can use stat...

1
2
3
struct stat st;
stat(filename, &st);
size = st.st_size;
Topic archived. No new replies allowed.