![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programming Guru
![]() |
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);
}
__________________
|
|
|
|
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| converting string to float | beginnerCCC | C | 22 | Oct 3rd, 2006 12:59 AM |
| OnlineTextEditor.Com! | Sane | Show Off Your Open Source Projects | 43 | Jun 16th, 2006 9:55 AM |
| Code snippet: Temporary files in C... | tempest | C | 1 | Oct 20th, 2005 10:58 AM |
| After execution - Error cannot locate /Skin File? | wchar | Visual Basic | 1 | Mar 5th, 2005 10:04 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 5:12 PM |