Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-26-2007, 05:49 PM
Panserbjorn Panserbjorn is offline
Newbie
 
Join Date: Oct 2007
Posts: 2
Rep Power: 0
Panserbjorn is on a distinguished road
Default Linked lists of strings (C)

I'm working on a program to justify a block of text. The step I'm stuck on now (haven't gotten to the justification part), is reading in the text file into a linked list of strings. I'm 99% sure I have the code that identifies what is and isn't a string working, but when I print the linked list, I either get blanks or single non-regular characters in each spot. My weakness is pointers, and I suspect that my pointers are screwing this up royally. If you could take a look and help me out, I'd much appreciate it. I'm sorry if this is too much code to post on the forum (I'm new here).

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 insert(LISTNODEPTR *, char *);
void printList(LISTNODEPTR);
int isChar(char);

main()
{

	FILE *fptr;
	LISTNODEPTR startPtr = NULL;
	int maxLine = 0;
	char file_name[20];
	int count = 0;
	char c;
	char d;
	char word[50];

	printf("Type in the name of the file containing the text: \n");
	scanf("%s",file_name);
	fptr=fopen(file_name,"r");

	int numChar; /* number of characters per line */

	printf("How many characters per line?: ");
	scanf("%d", &maxLine);

	c = fgetc(fptr);

	while (d = fgetc(fptr)) != EOF) {

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

			/* 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)) {
				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;

				char *newword = malloc(sizeof(word));
				newword = word;
				insert(&startPtr, newword);

				c = d;
				count = 0;
			}


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



	}

	fclose(fptr);

	printList(startPtr);
}


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


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

      previousPtr = NULL;
      currentPtr = *sPtr;

      while (currentPtr != NULL && value > currentPtr->data) {
         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);
}

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

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

      printf("NULL\n\n");
   }
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 10-26-2007, 05:58 PM
Panserbjorn Panserbjorn is offline
Newbie
 
Join Date: Oct 2007
Posts: 2
Rep Power: 0
Panserbjorn is on a distinguished road
Default

Well, I figured out why I was getting odd results for the linked list.. the printList command was printing %c instead of %s. However, new problem. The test file I'm using starts with this:

A knowledge and appreciation

When I print the linked list, it starts with the word knowledge, but misses the A. Any ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
adding new nodes in a linked list while looping emda321 C and C++ 2 06-18-2007 03:27 AM
VB strings to C DLL malachi1984 Visual Basic Programming 0 05-11-2007 06:36 AM
Strings clookid PHP Tutorials 2 01-13-2007 04:23 PM
Up to date lists Crane Shareware Sites 2 11-30-2006 04:22 PM
Lists, Stacks, and Queues Sionofdarkness Python 2 07-26-2006 09:38 PM


All times are GMT -5. The time now is 02:21 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads