I am currently working on a project where we must read in a file and then print it back type justified. There must also be a specific number of characters per line based upon user Input with the extra characters being distributed spaces. I am reading in each character from the file storing them individually in an array and then placing the whole word into the linked list(I believe). I am not very experienced with C/C++ so I am not sure I am doing this correctly. I have printed the strings which I believe are being fed into the list and mostly they print as expected but occasionally there is random stuff at the end of the word aka diamond becomes diamondssjkd with the extras being random characters or symbols. The biggest problem I am having(if the words are being read in correctly) is coming up with a method which prints the words right now I am just printing either blank spaces or one letter.Here is my code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct listNode {  /* self-referential structure */
   char *data;
   struct listNode *nextPtr;
};

typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;

void readfile();
void insert(LISTNODEPTR *,char *);
void deleteList(int count, char *);
void printList(LISTNODEPTR,int c);
int isChar(char);

main()
{
    int charPerLine = 0;
    readfile();
    printf("How many characters between 55 and 80 per line? \n");
	scanf("%d", &charPerLine);
	while(charPerLine > 80 || charPerLine < 55){
	    printf("The character you entered was not in the specified range try again.\n");
        printf("How many characters between 55 and 80 per line? \n");
        scanf("%d", &charPerLine);
	}
}

void readfile(){
    FILE *fptr;
    LISTNODEPTR sPtr = NULL;
	int charPerLine = 0;
	char file_name[20];
	int count = 0;
	char c;
	char d;
	char word[50];

	printf("Type in the name of your file followed by .txt. \n");
	scanf("%s",file_name);

	if((fptr = fopen(file_name, "r")) == NULL)
    {
    printf("The file can not be found or does not exist.\n");
    exit(0);
    }//end if


	c = fgetc(fptr);

	while (c != EOF) {

	    d = fgetc(fptr);

			/* if two new lines in a row */
			if ((c == '\n') && (d == '\n')) {

                word[count] = '\n';
                count++;
                word[count] = '\n';

				char *newword = malloc(sizeof(LISTNODE));
				newword = word;
				insert(&sPtr,newword);
				deleteList(count, word);

				c = d;
				count = 0;

			}
			/* if not two non-characters in a row */
			else if ((isChar(c) == 0) && (isChar(d) == 0)) {
				c = d;
			}
			/* if c and d are both characters */
			else if ((isChar(c) == 1) && (isChar(d) == 1)) {
				//printf("%c , %c\n", c, d);
				word[count] = c;
				c = d;
				count++;
			}
			/* if c is character and d is space, return, tab, or new line */
			else if ((isChar(c) == 1) && (isChar(d) == 0)) {
				word[count] = c;
				//printf("%c, %d \n", c, count);
				char *newword = malloc(sizeof(LISTNODE));
				newword = word;
				insert(&sPtr, newword);
				deleteList(count, word);

				c = d;
				count = 0;
			}
			/* if d is start of new word */
			else if ((isChar(d) == 1) && (isChar(c) == 0)) {
				c = d;
			}

	}//end while

	fclose(fptr);
	printList(sPtr,charPerLine);
}//end readfile


int isChar(char c) {
	if ((c >= 33) && (c <= 126)) {
		return 1;
	}
	return 0;
}

/* insert into list */
void insert(LISTNODEPTR *sPtr, char *value)
{
   LISTNODEPTR newPtr, previousPtr, currentPtr;

   newPtr = malloc(sizeof(LISTNODE));
   printf("%s ", value);


   if (newPtr != NULL) {    /* is space available */
   	  newPtr->data = value;
      newPtr->nextPtr = NULL;

      previousPtr = NULL;
      currentPtr = *sPtr;

      while (currentPtr != NULL) {
         previousPtr = currentPtr;          /* walk to ...   */
         currentPtr = currentPtr->nextPtr;  /* ... next node */
      }

      if (previousPtr == NULL) {
         newPtr->nextPtr = *sPtr;
         *sPtr = newPtr;
      }
      else {
         previousPtr->nextPtr = newPtr;
         newPtr->nextPtr = currentPtr;
      }
   }
   else
      printf("%c not inserted. No memory available.\n", value);
}

void deleteList(int count, char *word){
    int i = 0;
    for(i = count; i > 0; i--){
    word[i] = (int) NULL;
    }

}//end delete

/* Print the list */
void printList(LISTNODEPTR currentPtr, int numchar)
{
    if (currentPtr == NULL)
      printf("List is empty.\n\n");
    else {
      printf("The list is:\n");

      while (currentPtr != NULL) {
         printf("%s --> ", currentPtr->data);
         currentPtr = currentPtr->nextPtr;
      }

      printf("NULL\n\n");
   }

}

Edited 5 Years Ago by Narue: Added code tags

This article has been dead for over six months. Start a new discussion instead.