![]() |
|
![]() |
|
|
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);
}
__________________
|
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Some comments would be nice.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
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.
__________________
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
If they can figure it out, its useless to them. This is the tutorials section - teach.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
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.
__________________
|
|
|
|
|
|
#6 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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:
|
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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! |
|
|
|
![]() |
| 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 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 |