I am trying to write a program that will take a text file and parse the words into a linke dlist that will be then printed to the screen with a user defined number of characters per line and I am running into some runtime issues with opening and procesing the file. Any help would be appreciated. Here is the code I have:
* Header Files */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
/* Structure for Node */
struct node {
char* str;
int length;
struct node *nxtPtr;
};
/* Global Variables */
typedef struct node NODE;
char *strFileName = NULL;
NODE *head;
void insertAfter(NODE *toIns);
char* getFileName();
int processFile(char *filename);
void printData(int count);
/* Main Function */
int main()
{
int counter;
strFileName = getFileName();
printf("%s", strFileName);
counter = processFile(strFileName);
if(counter == 0){
printf("\n", "The Program Will Now Exit");
system("PAUSE");
return 0;
}
printData(counter);
printf("\n", "The Program Will Now Exit");
system("PAUSE");
return 0;
}
/*=========================================================================*/
/* Function to insert a new node into the list */
void insertAfter(NODE *toIns){
NODE *temp, *temp2;
if(head == NULL){ //checking to see if empty list
// if so, make toIns the head
head = (NODE *)malloc(sizeof(NODE));
head = toIns;
head->nxtPtr = NULL;
}
else{
// else, insert at end of list
temp = head;
// get to the last node
while ( temp->nxtPtr != NULL ){
temp = temp->nxtPtr;
}
// copy toIns to temp2 and point last node to temp2
temp2 = (NODE *)malloc(sizeof(NODE));
temp2 = toIns;
temp2->nxtPtr = NULL;
temp->nxtPtr = temp2;
}
}
/*=========================================================================*/
/* Get File Name */
char* getFileName()
{
char* filename;
char string[100];
int i = 0;
int temp;
printf("\t |-------------------------------------------------| \n");
printf("\t | Welcome to the File Justification System | \n");
printf("\t | Enter the File Name and the Program | \n");
printf("\t | Will Format the Text According to the | \n");
printf("\t | Specified Number of Characters per Line | \n");
printf("\t |-------------------------------------------------| \n");
printf("\t Please Enter the name of the file: ");
fflush(stdin);
scanf("%s", string);
temp = strlen(string);
string[temp] = '\0';
if(strlen(string) == 0)
{
printf("\n Invalid file name entered, please try again. \n");
getFileName();
}
filename = string;
return filename;
}
/*=========================================================================*/
/* Process the File into a Linked List */
int processFile(char *filename)
{
char c[100];
int i = 0;
int count = 0;
FILE *fptr;
NODE *temp;
printf("%s \n", filename);
fptr = fopen("C:\\stuff.txt", "r");
if(fptr == NULL){
printf("File not found error");
return count;
}
else{
c[i] = (char) fgetc(fptr);
while((int)c[i] != EOF){
//first time white space encountered we strcpy for the node value
if(c[i] == ' ' ){
c[i] = '\0';
strcpy(temp->str, c);
temp->length = strlen(c);
temp->nxtPtr = NULL;
insertAfter(temp);
count += 1;
}
//trim additional amounts of white space
while(c[i] == ' '){
i++;
c[i] = (char) fgetc(fptr);
}
//if tab or end line character found then cpy string for node
if(c[i] == '\n' || c[i] == '\t'){
c[i] = '\0';
strcpy(temp->str, c);
temp->length = strlen(c);
temp->nxtPtr = NULL;
insertAfter(temp);
count += 1;
}
//accounts for multiple tabs or returns
while(c[i] == '\n' || c[i] == '\t'){
i++;
c[i] = (char) fgetc(fptr);
}
}
fclose(fptr);
return count;
}
}
/*=========================================================================*/
/* Print Data*/
void printData(int count){
int lineSize;
int walker = 0;
int accumulator = 0;
NODE *traverse;
NODE * temp;
printf("Please enter the number of characters per line: ");
scanf("%d", &lineSize);
if(lineSize < 55 || lineSize > 80){
printf("Please Enter a Valid Number (Range 55 - 80) \n");
printf("Please enter the number of characters per line: \n \t");
scanf("%d", &lineSize);
}
traverse = head;
printf("The File is Formatted as Follows: \n");
while(walker <= count){
if(traverse->length + accumulator < lineSize){
printf("%s ", traverse->str);
accumulator = accumulator + 1 + traverse->length;
temp = traverse->nxtPtr;
free(traverse);
traverse = temp;
}
else{
accumulator = 0;
printf("\n %s ", traverse->str);
accumulator = accumulator + 1 + traverse->length;
temp = traverse->nxtPtr;
free(traverse);
traverse = temp;
}
}
}