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;
		}
	}
	
}

>>I am running into some runtime issues with opening and procesing the file

What, speficially, are the problems you have? compiler errors? If yes, then post a few of them.

>>I am running into some runtime issues with opening and procesing the file

What, speficially, are the problems you have? compiler errors? If yes, then post a few of them.

Not any compiler errors, the program compiles fine. I am having run time errors, specifically with a null pointer exception when trying to retrieve the file pointer in the function processFile(char *). I am gathering there may be some pointer issues when trying to build nodes and the linked list, however, I am not able to walk through the program that far because of the error with my fopen() statement. Tried to hardcode a file name within same directory and still having issues

line 157: variable temp is just an unallocated pointer. You have to allocate memory for it before it can be used. temp = malloc(sizeof(NODE)); add that between lines 156 and 157.

I hadn't even gotten that far really because the fopen keeps returning a null pointer

thank you for the help by the way, my friend asked me to help and my C is a little rusty obviously...much appreciated

When fopen() returns a NULL pointer it means that it can't find the file you want to open. Check your computer to see that the file is in the location you said it is in the program. line 142 ignores the filename that is passed to the function and just tries to open c:\stuff.txt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.