How to find file size?

This is a discussion on How to find file size? within the C Programming forums, part of the General Programming Boards category; Is there any simple way to find out what the size of a DOS file is using C?...

  1. #1
    Unregistered
    Guest

    How to find file size?

    Is there any simple way to find out what the size of a DOS file is using C?

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    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;
    }

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    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...

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >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...

  5. #5
    CuriousJay
    Guest

    Question

    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);

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    My question is.. how can I grab the size of a file opened using ofstream?
    using

    outSampleFile.seekg(0,ios::end);

    to get to the end of the file and -

    outSampleFile.tellg();

    to get the number of bytes.

  7. #7
    CuriousJay
    Guest
    Thanks zen,

    seekg() and tellg() are not member functions of ofstream, but seekp() and tellp() are, so they worked =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 10:13 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21