Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 21st, 2005, 2:17 PM   #1
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Determilning file-size in C (code snippet)

This is more of a code snippet than a tutorial... if you have any questions i'll be more than happy to answer them though.

#include <stdio.h>

void error(char *errMsg);

int main(int argc, char *argv[]) {
    if(argc != 2) {
        char errMsg[1024];
        snprintf(errMsg, 1024, "Usage: %s <filename>\n", argv[0]);
        error(errMsg);
    }


    FILE *fl = fopen(argv[1], "r");

    if(fl == NULL) {
        char errMsg[1024];
        snprintf(errMsg, 1024, "Could not open file \"%s\"\n", argv[1]);
        error(errMsg);
    }

    fseek (fl, 0, SEEK_END);
    long size = ftell(fl);
    fclose(fl);

    printf("%s:\n", argv[1]);
    printf("    %.2f Megabytes\n", (double)size/1048576);
    printf("    %.2f Kilobytes\n", (double)size/1024);
    printf("    %d Bytes\n", size);

    return 0;
}

void error(char *errMsg) {
    printf("%s", errMsg);
    exit(0);
}
__________________

tempest is offline   Reply With Quote
Old Feb 21st, 2005, 4:40 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Some comments would be nice.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 21st, 2005, 8:28 PM   #3
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
I'm too lazy to add comments, anyone who has C/C++ knowledge will be able to tell what that does anyway... it's not that complicated.
__________________

tempest is offline   Reply With Quote
Old Feb 22nd, 2005, 11:36 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
If they can figure it out, its useless to them. This is the tutorials section - teach.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 22nd, 2005, 6:15 PM   #5
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
The first line of my post said that it was a code snippet instead of a tutorial, yes this is the tutorials forum but in the forums descriptions it states its purpose for both Code Snippets AND Tutorials. As i said before, if anyone has any questions, they are free to ask and i will respond with the best of my ability to try and give them the most accurate answer i can...

By the way, alot of programmers can look at something they've never seen before and just guess at what it's doing by previous knowledge and intelligent assumptions in regards to the program flow. So if they don't know how to do it, but are still able to look at it and tell what it does... then it IS useful to them my friend.
__________________

tempest is offline   Reply With Quote
Old Feb 23rd, 2005, 10:41 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I phrased that badly. What I meant was, the code is kinda cryptic. The only people who'll be able to figure it out are people who know how to do it already. Though I didn't see this:
Quote:
Originally Posted by tempest
if you have any questions i'll be more than happy to answer them though.
So I guess it's time for me to shut up.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 6th, 2005, 9:02 PM   #7
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
hey, i made something similar... ill comment this baby too

// Including Functions
#include <cstdlib>
#include <iostream>
#include <sys\types.h> 
#include <sys\stat.h> 
//

using namespace std; // console window

int FileSize( const char * szFileName ) // from The CodeProject
{ 
  struct stat fileStat;  // declare a structure and the name of it
  int err = stat( szFileName, &fileStat ); //i know what it does, just cant put it in words, sorry
  if (0 != err) return 0;  // if file not found, print "0"
  return fileStat.st_size;  // when file found return size
} 

int main() // what the program first executes
{
    char buffer[100]; //makes a char to store data
    cout << "Get the size of a file! \n"; //prints what it says
    cout << "Please enter the exact path to the file: "; // prints what it says (ask's user to enter file path fo file)
    cin.getline(buffer,100); // gets what the user just typed in after they pressed enter
    cout << "Size: " << FileSize(buffer) << " bytes \n"; // uses the FileSize function up above to get the file size of the user input
    system("pause"); // the "Press any key to continue..." caption appears and does just that
}

i am only a noob to C++ so some of the comments are probably wrong, and one i know what it means but cant explain it.. =/ but hey, i tried!
layer is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
converting string to float beginnerCCC C 22 Oct 2nd, 2006 11:59 PM
OnlineTextEditor.Com! Sane Show Off Your Open Source Projects 43 Jun 16th, 2006 8:55 AM
Code snippet: Temporary files in C... tempest C 1 Oct 20th, 2005 9:58 AM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:48 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC