Write a C program that displays the size of a file. This project must contain th
ID: 3777141 • Letter: W
Question
Write a C program that displays the size of a file. This project must contain the three source files.
• Take an input filename as the first (and only) argument on the command line using argc
and argv in main(). Put main() in mini8.c.
o If there is anything else after the filename on the command line, display an
appropriate error message and quit the program.
• Define a function called getSmallFileLength() in a file you create called fileUtilities.c. It
takes a name of a file as a parameter as a char *. Call FindFirstFile() and return the
nFileSizeLow value from the WIN32_FIND_DATA struct you declared, unless:
o if the parameter is a NULL pointer, return -1 before calling FindFirstFile().
o if FindFirstFile indicates an error, return -2.
o if nFileSizeHigh is non-zero, return -3.
• In main(), find the size of the input file using a call to getSmallFileLength() as mentioned
above. The function call must have the input filename passed as a parameter. Store the
return value from the call in a variable.
o Use a switch statement to determine which of the following should be done:
If the getSmallFileLength() call indicates an error, display an appropriate
error message and quit the program.
If the getSmallFileLength() does not indicate an error, display the size of
the file.
• In a file you create called fileUtilities.h:
o create three constants representing the three error codes using #define
use the constants in getSmallFileLength() and in main()
o put your prototype for getSmallFileLength()
• Do not get user input from the keyboard in your program. At all. I'm serious.
• All error messages must have appropriate wording and correct spelling and grammar.
• Create an appropriate and complete function comment for getSmallFileLength(),
including description of all return values.
• Do not use global variables, goto, or exit().
• Appropriate programming style as discussed in class must be used.
Hints:
• You must use #define and not const for the error constants, since const will not work as
cases in a switch statement.
• The reason that you are doing this with getSmallFileLength is that you have now created
a utility function that you could use in other programs if you have the need.
• Visual C++ will not compile your call to FindFirstFile successfully unless you make a
change to the Project Properties to "Use Multi-byte Character Set" instead of using
Unicode. This was covered in the inverted lecture. You must change this from the default
in order to get your program to work!
#include <stdio.h>
#pragma warning(disable: 4996)
int main(int argc, char *argv[])
{
int argNum = 1;
printf("Here's the command line contents: ");
printf("Command: %s ", argv[0]);
for (argNum = 1; argNum < argc; argNum++)
{
printf("Arg #%d: %s ", argNum, argv[argNum]);
}
Explanation / Answer
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include "fileUtilities.h"
#pragma warning(disable: 4996)
using namespace std;
int getSmallFileLength(char* chPtr) {
if(chPtr == NULL) {
return 1;
}
WIN32_FIND_DATA data;
if(INVALID_HANDLE_VALUE == FindFirstFile(chPtr,&data)) {
return 2;
}
return data.nFileSizeLow;
}
int main(int argc, char *argv[])
{
int argNum = 1;
printf("Here's the command line contents: ");
printf("Command: %s ", argv[0]);
for (argNum = 1; argNum < argc; argNum++)
{
printf("Arg #%d: %s ", argNum, argv[argNum]);
}
int result;
try {
result = getSmallFileLength(argv[1]);
switch (result) {
case 1: printf("Error: Name of file missing: ");
return 1;
case 2: printf("Error: %d ",HRESULT(GetLastError()));
return 2;
default: printf("FileSize is %d ",result);
return 3;
}
}
catch(exception e) {
return 2;
}
}
=====================
fileUtilities.h
=======
#define NO_FILE_NAME 1
#define ERROR_FIND_FILE 2
#define GRACE 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.