Hi, I'm having trouble with an assignment I'm doing. My code is split across 3 files. My header, main, and functions, so I'll post all three.
Header: cA6_proto.h
Code:
#define MaxLineSize 100
#define TRUE 1
#define FALSE 0
// libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <malloc.h>
#include <Windows.h>
// prototypes
int getSmallFileLength(const char *filename);
void printWordsWrap(char *line[100], int maxLineLength);
functions: functions.c
Code:
#include"cA6_proto.h"
int getSmallFileLength(const char *filename)
{
// variable declarations:
WIN32_FIND_DATA fileData = {0};
HANDLE h = FindFirstFile(filename, &fileData);
int fileLength = -1;
if (h != INVALID_HANDLE_VALUE)
{
fileLength = fileData.nFileSizeLow; // sets fileLenght to size of file
FindClose(h); // closes files
}
return fileLength;
}
void printWordsWrap(char *line[100], int maxLineLength)
{
int i = 0;
int j = 0;
char *space = " ";
int counter = -1;
int startPosition = 0;
for (i = 0; i < maxLineLength; i++)
{
if (strcmp(space, line[i]) != 0)
{
counter++;
}
if (strcmp(space, line[i]) == 0)
{
if (counter < maxLineLength)
{
for (j = startPosition; j < counter; j++)
{
printf("%c", line[j]);
}
startPosition = counter;
}
else
{
printf("\n");
for (j = startPosition; j < counter; j++)
{
printf("%c", line[j]);
}
startPosition = counter;
counter = 0;
}
}
}
return;
}
And my mainline:
Code:
#include"cA6_proto.h"
int main(int argc, char *argv[])
{
// variable declarations
FILE *fp = NULL;
int fileLength = 0;
int columnWidth = 40;
char fileChars[100] = {0};
int wordWrap = FALSE;
int i = 0;
char *s = "-s";
char *w = "-w";
char *space = " ";
char *printWord[MaxLineSize] = {0};
int stringCompare = 0;
if (argc < 2)
{
printf("\n no file input on command line \n");
return 2; // command line operations error
}
else
{
if ((stringCompare = strncmp(s, argv[1], 2)) == 0)
{
// [-s] exists on the command line, therefore the file will print with a 'word wrap' properties (not splitting up words at all)
wordWrap = TRUE;
if ((stringCompare = strncmp(w, argv[2], 2)) == 0) // scans second argument to check for presence of -w
{
if (sscanf(argv[2], "-w%d", &columnWidth) != 1) // scans the user's '-w' statement to check for a number, if none is found sets it to the default of 40
{
columnWidth = 40; //error getting user width, set to 40 instead
}
}
}
if ((stringCompare = strncmp(w, argv[1], 2)) == 0) // scans first argument for a -w, indicating a custom width
{
if (sscanf(argv[1], "-w%d", &columnWidth) != 1) // scans the user's '-w' statement to check for a number, if none is found sets it to the default of 40
{
columnWidth = 40; //error getting user width, set to 40 instead
}
}
for (i=1; i < argc; i++)
{
const char *fileName = argv[i];
if ((fp = fopen(fileName, "r")) == NULL) // open user given file to read from
{
printf("\n error opening file to read from \n");
fclose(fp); // closes file and quits if there is an error opening it
return 1; // can't open file error
}
/*fileLength = getSmallFileLength(fileName);
if (fileLength < 1)
{
printf("\n error getting length of file \n");
fclose(fp); // close file
return 4; // fatal error: goes to end of program (exits)
}
if ((fileChars = (char *) malloc(fileLength)) == NULL)
{
printf("\n error allocating memory \n"); // quits program if memory is not allocated correctly
fclose(fp); // closes file
free(fileChars); // frees memory
return 4; // fatal error: closes program
} */
while ((fgets(fileChars, MaxLineSize, fp)) != NULL) // MaxLineSize is a #define to 100
{
printWordsWrap(fileChars, columnWidth);
}
fclose(fp);
}
}
return 0;
}
My issues are occurring at the print statement in line 70 of main. It doesn't like the fileChars parameter I give it and I'm not sure why.
Also I have the get fileLength and malloc commented out, because I'm not sure I need it, do I need it?