Is there any simple way to find out what the size of a DOS file is using C?
Is there any simple way to find out what the size of a DOS file is using C?
Code:#include <stdio.h> int main(void) { int filesize = 0; char filename[] = "C:\\myfile.txt"; FILE* file = fopen(filename,"r"); /* open the file for reading */ if(file == NULL) /* if the file wasn't found return */ { printf("Unable To Open file\n"); return -1; } fseek(file,0L,SEEK_END); /* go to the end of the file */ filesize = ftell(file); /* get the filesize in bytes... */ printf("Filesize:%d\n",filesize); fclose(file); return 0; }
an alternative to seeking it and getting the position of the file pointer would be use filelength () and fileno ()... since you are using the f* functions, and filelength () only accepts handles, convert the FILE * to a handle using fileno ()... something like this...
length = filelength ( fileno (file_pointer));
i believe filelength () is in <stdlib.h> and fileno () is in <io.h>...
hasafraggin shizigishin oppashigger...
>i believe filelength () is in <stdlib.h> and fileno () is in <io.h>...
io.h is a compiler specific header... these functions may not exist in io.h...
thought that method does seem somewhat(ha a lot) better...
I know this pertains more to C++ then C, but this thread to have the same idea..
My question is.. how can I grab the size of a file opened using ofstream?
So, if I have some file,
//
ofstream outSampleFile("C:\sample.txt", ios:ut);
//
How (without using a FILE *) can I get the size of the file?
long filesize = iWishSomethingLikeThisWouldWork(outSampleFile);
usingMy question is.. how can I grab the size of a file opened using ofstream?
outSampleFile.seekg(0,ios::end);
to get to the end of the file and -
outSampleFile.tellg();
to get the number of bytes.
Thanks zen,
seekg() and tellg() are not member functions of ofstream, but seekp() and tellp() are, so they worked =)