The program I am writing is supposed to read in a txt file and then align the txt depending on the number of characters per line that the user has specified. Since there aren't any limitations to the length of the text it has to be represented using a dynamic data structure. I'm using a Linked list and the professor wants us to read the file in and store each word as a string. The problem I am having is that we are also supposed to be able to account for multiple paragraphs which are denoted by two '\n's. The program that I have written prints out the text properly but isn't able to detect the paragraphs so the text gets printed out with all the paragraphs combined together. I need them to print out in a seperated fashion. My code is posted below, any help would be appreciated. This is my first class involving C programming so some sample code would be helpful.

My Code:

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


/*This is where the nodes are created*/
struct node
{
  char *word;
  struct node *next;
};

/*Variable declaration for pointers*/
struct node *alist = NULL;
struct node *end = NULL;
struct node *begin = NULL;
struct node *aPtr = NULL;
struct node *tempPtr = NULL;

void LinkedList(struct node *j, struct node **last);
void readfile(int numchar);
void freeMemory();
void printList(int numchar);



int main()
{
  /* This variable is for the user specified number of characters per line*/
  int cPerLine = 0;

  printf("Enter how many characters you want per line between 55 and 80.\n");
  scanf("%d", &cPerLine);
  readfile(cPerLine);
  printList(cPerLine);
  freeMemory();
  return 0;
}

void readfile(int numchar)
{

  FILE *filePtr = NULL;
  char c1, c2;
  char *String = malloc(30*sizeof(char));
  char filename[20];

  printf("Enter the name of the file containing your txt.\n");
  scanf("%s", filename);
  if((filePtr = fopen(filename, "r")) == NULL)
  {
    printf("File can't be opened\n");
    exit(1);
  }
  else
  filePtr = fopen(filename, "r");
  fscanf(filePtr, "%s", String);
  alist = (struct node *)malloc(sizeof(struct node));
  alist->word = String;
  alist->next = NULL;
  end = alist;

  while(!feof(filePtr))
  {
   String = malloc(30*sizeof(char));
   fscanf(filePtr, "%s", String);
   end->next = (struct node *)malloc(sizeof(struct node));
   end = end->next;
   end->word = String;
   end->next = NULL;
   if(String == '\n')
   {
    printf("Found new line");
   }

   }
   fclose(filePtr);
   printf("\nThe Text is:  \n\n");
}

/*This method is actually printing the stored txt
  from the list with the proper justification
  and characters per line*/
void printList(int numchar)
{
  end = alist;
  aPtr = alist;
  begin = alist;
  tempPtr = begin;

  while(begin->next != NULL)
  {
   int numSpace[80] = {};
   int NumWSpace = 0;/*This variable keeps track of the spaces within a line*/
   int charLine = 0;
   int leftOverSpace = 0;/*This variable is used to determine leftover space so that it can be filled with spaces*/
   int counter = 0;
   int finalLine = 0;
   int j = 0;
   int i = 0;

   charLine = strlen(end->word)+1;
   counter = 1;
   end = end->next;

   while(charLine+strlen(end->word)+1 < numchar)
   {
     charLine = charLine + strlen(end->word) + 1;
     counter = counter + 1;
     if(end->next != NULL)
     end = end->next;
     else
     {
       finalLine = 1;
     }
   }
   leftOverSpace = numchar - (charLine-1);

   while(leftOverSpace > 0)
   {
     if(NumWSpace < (counter - 1))
     {
       numSpace[NumWSpace] = numSpace[NumWSpace] + 1;
       leftOverSpace = leftOverSpace - 1;
       NumWSpace = NumWSpace + 1;
     }
     else
     NumWSpace = 0;
   }


 /*This loop will print out the words for the line it is on*/

   for( i = 1; i <= counter; i++)
   {
     if(i , counter)
     {
       printf("%s ", aPtr->word);
       for(j = 1; j <= numSpace[i-1]; j++)
       {
         printf(" ");
       }
     }
     else if(i = counter)
     {
       printf("%s\n", aPtr->word);
     }
     if(aPtr->next != NULL)
     aPtr = aPtr->next;
   }
   if(aPtr->next != NULL)
     end = aPtr;
   if(end->next == NULL)
     begin = end;
   printf("\n");
}
}


void freeMemory()
{

   end = alist;
   while(end->next != NULL)
   {
     end = end->next;
   }
   while(alist != NULL)
   {
     if(end == alist)
     {
       free(alist->word);
       free(alist);
       alist = NULL;
       end = NULL;
       aPtr = NULL;
     }
     else
     {
       aPtr = alist;
       while(aPtr->next != end)
       aPtr = aPtr->next;
       free(end->word);
       free(end);
       end = aPtr;
     }
   }
}

Recommended Answers

All 3 Replies

Can you repost your code in Code tags? It's not friendly to read without it.

The program I am writing is supposed to read in a txt file and then align the txt depending on the number of characters per line that the user has specified. Since there aren't any limitations to the length of the text it has to be represented using a dynamic data structure. I'm using a Linked list and the professor wants us to read the file in and store each word as a string. The problem I am having is that we are also supposed to be able to account for multiple paragraphs which are denoted by two '\n's. The program that I have written prints out the text properly but isn't able to detect the paragraphs so the text gets printed out with all the paragraphs combined together. I need them to print out in a seperated fashion. My code is posted below, any help would be appreciated. This is my first class involving C programming so some sample code would be helpful.

My Code:

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


/*This is where the nodes are created*/
struct node
{
  char *word;
  struct node *next;
};

/*Variable declaration for pointers*/
struct node *alist = NULL;
struct node *end = NULL;
struct node *begin = NULL;
struct node *aPtr = NULL;
struct node *tempPtr = NULL;

void LinkedList(struct node *j, struct node **last);
void readfile(int numchar);
void freeMemory();
void printList(int numchar);



int main()
{
  /* This variable is for the user specified number of characters per line*/
  int cPerLine = 0;

  printf("Enter how many characters you want per line between 55 and 80.\n");
  scanf("%d", &cPerLine);
  readfile(cPerLine);
  printList(cPerLine);
  freeMemory();
  return 0;
}

void readfile(int numchar)
{

  FILE *filePtr = NULL;
  char c1, c2;
  char *String = malloc(30*sizeof(char));
  char filename[20];

  printf("Enter the name of the file containing your txt.\n");
  scanf("%s", filename);
  if((filePtr = fopen(filename, "r")) == NULL)
  {
    printf("File can't be opened\n");
    exit(1);
  }
  else
  filePtr = fopen(filename, "r");
  fscanf(filePtr, "%s", String);
  alist = (struct node *)malloc(sizeof(struct node));
  alist->word = String;
  alist->next = NULL;
  end = alist;

  while(!feof(filePtr))
  {
   String = malloc(30*sizeof(char));
   fscanf(filePtr, "%s", String);
   end->next = (struct node *)malloc(sizeof(struct node));
   end = end->next;
   end->word = String;
   end->next = NULL;
   if(String == '\n')
   {
    printf("Found new line");
   }

   }
   fclose(filePtr);
   printf("\nThe Text is:  \n\n");
}

/*This method is actually printing the stored txt
  from the list with the proper justification
  and characters per line*/
void printList(int numchar)
{
  end = alist;
  aPtr = alist;
  begin = alist;
  tempPtr = begin;

  while(begin->next != NULL)
  {
   int numSpace[80] = {};
   int NumWSpace = 0;/*This variable keeps track of the spaces within a line*/
   int charLine = 0;
   int leftOverSpace = 0;/*This variable is used to determine leftover space so that it can be filled with spaces*/
   int counter = 0;
   int finalLine = 0;
   int j = 0;
   int i = 0;

   charLine = strlen(end->word)+1;
   counter = 1;
   end = end->next;

   while(charLine+strlen(end->word)+1 < numchar)
   {
     charLine = charLine + strlen(end->word) + 1;
     counter = counter + 1;
     if(end->next != NULL)
     end = end->next;
     else
     {
       finalLine = 1;
     }
   }
   leftOverSpace = numchar - (charLine-1);

   while(leftOverSpace > 0)
   {
     if(NumWSpace < (counter - 1))
     {
       numSpace[NumWSpace] = numSpace[NumWSpace] + 1;
       leftOverSpace = leftOverSpace - 1;
       NumWSpace = NumWSpace + 1;
     }
     else
     NumWSpace = 0;
   }


 /*This loop will print out the words for the line it is on*/

   for( i = 1; i <= counter; i++)
   {
     if(i , counter)
     {
       printf("%s ", aPtr->word);
       for(j = 1; j <= numSpace[i-1]; j++)
       {
         printf(" ");
       }
     }
     else if(i = counter)
     {
       printf("%s\n", aPtr->word);
     }
     if(aPtr->next != NULL)
     aPtr = aPtr->next;
   }
   if(aPtr->next != NULL)
     end = aPtr;
   if(end->next == NULL)
     begin = end;
   printf("\n");
}
}


void freeMemory()
{

   end = alist;
   while(end->next != NULL)
   {
     end = end->next;
   }
   while(alist != NULL)
   {
     if(end == alist)
     {
       free(alist->word);
       free(alist);
       alist = NULL;
       end = NULL;
       aPtr = NULL;
     }
     else
     {
       aPtr = alist;
       while(aPtr->next != end)
       aPtr = aPtr->next;
       free(end->word);
       free(end);
       end = aPtr;
     }
   }
}[/QUOTE

Can you repost your code in Code tags? It's not friendly to read without it.

Sorry about that, I've reposted the code below.

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.