cA5.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <cA5_proto.h>
#include <Windows.h>
int main(int argc, char *argv[])
{
char *filename = argv[1];
WIN32_FIND_DATA filedata = {0};
int fileSize = 0;
FILE *fp = NULL;
unsigned char *buffer = 0;
//Checking for proper syntax
if(argc != 2)
{
printf("Syntax: cA5.exe \"Filename\"\n", argv[0]);//Syntax error message
return;
}
printf("Target file is %s\n", argv[1]);//Acknowledges user input by displaying intended file
fileSize = getSmallFileLength(filename);//Calling function to populate WIN32_FIND_DATA struct
buffer = (unsigned char *)malloc(filedata.nFileSizeLow);//Allocating memory to buffer
if(buffer == NULL)
{
printf("There was an error allocating memory for file contents!\n");//Error message
}
readWriteFileContents(fp, buffer, fileSize, argv[1]);
free(buffer);//Freeing up the allocated memory
}
/*
* FUNCTION : readWriteFileContents
*
* DESCRIPTION : Reads the file contents then writes it to the file 'contents.txt'
*
* PARAMETERS : char *filename : pointer to argv[1] "Name of intended file on command line"
* FILE *fp : uses the file pointer used to open the file
* int fileSize : lets the writing know when to stop the process
*
* RETURNS : 0 : returns to main menu
*/
int readWriteFileContents(FILE *fp, unsigned char *buffer, int fileSize, char *filename)
{
int i = 0;
fp = fopen(filename, "r");
if(fp == NULL)
{
printf("There was an error found!\n");//Error message
return 1;
}
else
{
fread(buffer, 1, fileSize, fp);
fclose(fp);
fp = fopen("contents.txt", "w");
if(fp == NULL)
{
printf("There was an error opening contents.txt! Make sure it's in the current directory!");//Error message
return 1;
}
else
{
while(i <= fileSize)//Runs fprintf until it reachs the end of the file
{
if((i % 10) == 0)
{
fprintf(fp, "\n");
i++;
}
else
{
fprintf(fp, "%03d ", buffer[i]);
i++;
}
}
fclose(fp);
return 0;
}
return 0;
}
return 0;
}
fileUtilities.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "cA5_proto.h"
#include <Windows.h>
/*
* FUNCTION : getSmallFileLength
*
* DESCRIPTION : Used to populate the WIN32_FIND_DATA Struct, giving us the required
* : nFileSizeLow value
*
* PARAMETERS : const char *filename : pointer to argv[1] "Name of intended file on command line"
*
* RETURNS : nFileSizeLow : returns to main menu
*/
int getSmallFileLength(const char *filename)
{
WIN32_FIND_DATA filedata = {0};
HANDLE hFind;
//Function only works for files below 2,147,483,647 bytes, limitation in function
FindFirstFile(filename, &filedata);//Populates WIN32_FIND_DATA struct according to proper file name
if(hFind == INVALID_HANDLE_VALUE) //Error message
{
printf("There was an error!");
return -1;
}
else if(filedata.nFileSizeLow < 1)
{
return 0;
}
return filedata.nFileSizeLow;
FindClose(hFind);
}
Getting a heap corruption problem at the fread(); line in the second module function in cA5.c.
Basically this program takes in a file, finds the nFileSizeLow value and reports it back to the main function. When that is done, it takes the file and allocates the proper amount of memory depending on the file size. It then takes in the file bytes then spews them out into a new file called 'contents.txt' in a nicely formatted package.
Everything goes well until it gets to the portion before writing the contents out to the new file. (Around line 60)
cA5_proto.h just includes the prototypes for the functions in this project