Read File in C++ Into a Linked List

#1

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 22
  • Joined: 22-July xi

Read a text file into a linked listing

Posted 02 August 2011 - 06:58 PM

The text file contains this:

Quote

Fred Flint 38 Male
Barney Rubble 36 Male
Wilma Flintstone 37 Female
Betty Rubble 36 Female
Pebbles Flintstone four Female
Bam-Bam Rubble 3 Male
Dino Flintstone two Male
Mr. Slate 57 Male
Hoppy Rubble 1 Male person
Great Gazoo 11 Unknown

In that location are tabs(not spaces) before and later the numbers. I take tried reading the data into a linked list two different means. Both unsuccessful then far, plain. Hither is my lawmaking for the first method. This is the fashion I would similar to do it considering I call up that it is more efficient than the 2nd:

#include <stdio.h> #include <stdlib.h> #include <cord.h> #include <stdbool.h>  #define LENGTH_OF_INPUT 35  typedef int KEY_TYPE; typedef struct {     char* name;     KEY_TYPE age;     char* gender; }Information;  typedef struct nodeTag {     DATA character;     struct nodeTag* link; }NODE;  NODE* buildList (NODE* pList); bool getData (FILE* pData, Data character); NODE* insertNode (NODE*pList, NODE* pPre, Information character); void printList (NODE* pList);  int main(void) {     NODE* pList;      pList = Zip;      pList = buildList (pList);      printList (pList);      render 0; }  NODE* buildList (NODE* pList) {     FILE* pData;     NODE* pPre;     Data character;      pPre = Zippo;      if ((pData = fopen("lab6data.txt", "r"))==Nothing)     {         printf("Error opening text file.\n");         get out (100);     }      while (getData(pData, character));     {         pList = insertNode (pList, pPre, graphic symbol);     }      if (fclose(pData) == EOF ) 	{ 	printf("Mistake closing lab6data.txt\north"); 	get out (300); 	}      render pList; }  bool getData (FILE* pData, Information graphic symbol) {     char input [LENGTH_OF_INPUT];     char name [LENGTH_OF_INPUT];     char gender [LENGTH_OF_INPUT];       if (!feof(pData))     {         while (fgets(input, 34, pData))         {             sscanf(input, "%19[^\t]%d %[^\n]", name, &character.historic period, gender);              character.proper name = calloc(strlen(name) + 1, sizeof(char));             strcpy(character.proper name, proper noun);              grapheme.gender = calloc(strlen(gender) + 1, sizeof(char));             strcpy(character.gender, gender);         }         return true;     }      else     {         return false;     }  }  NODE* insertNode (NODE*pList, NODE* pPre, Data grapheme) {     NODE* pNew;      if (!(pNew = (NODE*)malloc(sizeof(NODE))))     {         printf("Retention overflow in insert\n");         get out(200);     }      pNew->character = graphic symbol;      pNew->link = Null;          if (pList == Nada)         {             pList = pPre = pNew;         }         else         {             pPre->link = pNew;             pPre = pNew;         }     return pList; }  void printList (NODE* pList) {     NODE* pWalker;      pWalker = pList;     printf ("\nList contains:\n");      while (pWalker)     {         printf ("%s %d %due south\n", pWalker->character.proper noun, pWalker->character.historic period, pWalker->character.gender);         pWalker = pWalker->link;     }     printf ("<Stop of List>\n\n");     return; }            

This is the code for the alternate method, but I would rather know how to utilize the first. However, whatsoever tips would be appreciated for either of the two.

#include <stdio.h> #include <stdlib.h> #include <cord.h>  #define LENGTH_0F_ARRAY x #define LENGTH_OF_INPUT 35  typedef int KEY_TYPE; typedef struct {     char* name;     KEY_TYPE age;     char* gender; }Information;  typedef struct nodeTag {     Data character;     struct nodeTag* link; }NODE;  NODE* buildList (NODE* pList); void getData (DATA* pChar); NODE* insertNodes(NODE* pList, NODE* pPre, NODE* pCur, DATA* pChar); void printList (NODE* pList);  int primary(void) {     NODE* pList;      pList = NULL;      pList = buildList (pList);      printList (pList);      render 0; }  NODE* buildList (NODE* pList) {     Information* pChar;     NODE* pPre;     NODE* pCur;      pPre = NULL;     pCur = NULL;     pChar = calloc(LENGTH_0F_ARRAY, sizeof(Information));      getData(pChar);      pList = insertNodes(pList, pPre, pCur, pChar);      return pList; }  void getData (Data* pChar) {     FILE* pData;     int charIndex;     char input [LENGTH_OF_INPUT];     char proper noun [LENGTH_OF_INPUT];     char gender [LENGTH_OF_INPUT];      if ((pData = fopen("lab6data.txt", "r"))==Zilch)     {         printf("Error opening text file.\due north");         leave (100);     }      charIndex = 0;     while (charIndex<LENGTH_0F_ARRAY && fgets(input, 34, pData))     {         sscanf(input, "%19[^\t]%d %[^\n]", proper noun, &pChar[charIndex].historic period, gender);          pChar[charIndex].name = calloc(strlen(name) + i, sizeof(char));         strcpy(pChar[charIndex].name, proper noun);          pChar[charIndex].gender = calloc(strlen(gender) + 1, sizeof(char));         strcpy(pChar[charIndex].gender, gender);          charIndex++;     }      if (fclose(pData) == EOF ) 	{ 	printf("Error closing lab6data.txt\n"); 	exit (200); 	}  	return;  }  NODE* insertNodes(NODE* pList, NODE* pPre, NODE* pCur, Information* pChar) {     int charIndex;     NODE* pNew;      if (!(pNew = (NODE*)malloc(sizeof(NODE))))     {         printf("Memory overflow in insert\n"),         exit(300);     }      for (charIndex = 0; charIndex < LENGTH_0F_ARRAY; charIndex++)     {         pNew->character = pChar[charIndex];         pNew->link = Nada;          if (pList == Null)         {             pList = pPre = pNew;         }         else         {             pPre->link = pNew;             pPre = pNew;         }     }      render pList;  }  void printList (NODE* pList) {     NODE* pWalker;      pWalker = pList;     printf ("\nList contains:\n");      while (pWalker)     {         printf ("%s %d %s\n", pWalker->graphic symbol.name, pWalker->character.historic period, pWalker->character.gender);         pWalker = pWalker->link;     }     printf ("<End of List>\n\n");     return; }            

This post has been edited past riddinon24z: 02 August 2011 - 07:thirteen PM


Is This A Skillful Question/Topic? 0

  • +

#2 jimblumberg User is offline

Reputation: 5916

  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Read a text file into a linked list

Posted 02 August 2011 - 07:38 PM

Does this code compile without errors? If not post the complete error letters exactly equally they appear in your development environs.

In the post-obit snippet:

typedef struct {     char* name;     KEY_TYPE age;     char* gender; }DATA;            

Why are you using pointers instead of arrays? Yous accept divers #define LENGTH_OF_INPUT 35 and then why not use char name[LENGTH_OF_INPUT];?

If the program runs have yous checked to encounter if you are parsing the input correctly? Does your structure friction match the file data?

Jim

#3 riddinon24z User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 22
  • Joined: 22-July 11

Re: Read a text file into a linked list

Posted 02 August 2011 - 07:46 PM

The compiler I am using is codeblocks. I am not getting whatever errors or warnings for either of the ii methods. The output for the outset is a line of basically "garbage" and the second one prints the last line of the text file over and over once again.

I have actually used the lawmaking in the by and when I print the contents of the structure for the second method, information technology matches the information in the file. Oh and the reason I am using dynamic memory allocation for the name and gender is because my teacher told u.s. to do and then.

#4 riddinon24z User is offline

  • New D.I.C Caput

Reputation: 0

  • View blog
  • Posts: 22
  • Joined: 22-July xi

Re: Read a text file into a linked list

Posted 02 Baronial 2011 - 08:xiv PM

I'chiliad but thinking out loud. What I'chiliad trying to do is read one line of information from the text file into a node. Then insert that node into my list and put that entire process into a loop until all lines are read. Practise I have the right idea, looking at my code from method 1?

This post has been edited by riddinon24z: 02 August 2011 - 08:xv PM

#five jimblumberg User is offline

Reputation: 5916

  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Read a text file into a linked list

Posted 02 August 2011 - 08:46 PM

Quote

I'm simply thinking out loud. What I'm trying to exercise is read one line of data from the text file into a node. Then insert that node into my list and put that entire process into a loop until all lines are read.

That sounds similar a practiced plan. You might want to insure that y'all are reading the file properly. You can exercise this by press out each particular to the screen and place some kind of delimiter between the fields so yous tin tell if each particular is reading correctly.

Jim

#half dozen riddinon24z User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 22
  • Joined: 22-July 11

Re: Read a text file into a linked list

Posted 02 August 2011 - 08:56 PM

Ok. Here'south what I came upwardly with. I changed the type of the getData office from bool to void. Don't know whether or non that was a good idea. I took the fgets part from getData and put it into the while loop of the buildList function. I too put a printf argument to impress each line of information each time the getData office runs. Sorry if I worded that incorrectly. When I execute, the print statement prints the last line in the text data. I don't understand why information technology doesn't start with the first line and simply jumps to the final. As for my linked list output, information technology's just "garbage" basically. Right at present I'g only worrying about reading the first line of data into a node. I besides allocated the retentiveness for the information in each incoming node. I'1000 not getting any errors or warnings. I don't actually have a question. I don't know what to retrieve.

#include <stdio.h> #include <stdlib.h> #include <string.h>  #ascertain LENGTH_OF_INPUT 35  typedef int KEY_TYPE; typedef struct {     char* name;     KEY_TYPE historic period;     char* gender; }Data;  typedef struct nodeTag {     DATA grapheme;     struct nodeTag* link; }NODE;  NODE* buildList (NODE* pList); void getData (FILE* pData, DATA line, char input []); NODE* insertNode (NODE*pList, NODE* pPre, Information line); void printList (NODE* pList);  int main(void) {     NODE* pList;      pList = Cipher;      pList = buildList (pList);      printList (pList);      return 0; }  NODE* buildList (NODE* pList) {     char input [LENGTH_OF_INPUT];     FILE* pData;     NODE* pPre;     Information line;      pPre = Nada;      if ((pData = fopen("lab6data.txt", "r"))==NULL)     {         printf("Error opening text file.\n");         go out (100);     }      while (fgets(input, 34, pData));     {         getData(pData, line, input);         pList = insertNode (pList, pPre, line);     }      if (fclose(pData) == EOF ) 	{ 	printf("Error endmost lab6data.txt\northward"); 	get out (300); 	}      return pList; }  void getData (FILE* pData, Information line, char input []) {     char name [LENGTH_OF_INPUT];     char gender [LENGTH_OF_INPUT];      sscanf(input, "%19[^\t]%d %[^\northward]", name, &line.historic period, gender);      line.name = calloc(strlen(proper noun) + 1, sizeof(char));     strcpy(line.proper noun, name);      line.gender = calloc(strlen(gender) + 1, sizeof(char));     strcpy(line.gender, gender);      printf("%s %d %south", line.proper name, line.age, line.gender);       return; }  NODE* insertNode (NODE*pList, NODE* pPre, Information line) {     NODE* pNew;      if (!(pNew = (NODE*)malloc(sizeof(NODE))))     {         printf("Memory overflow in insert\n");         exit(200);     }      pNew->graphic symbol.name = calloc(strlen(line.proper name) + 1, sizeof(char));     pNew->character.gender = calloc(strlen(line.gender) + 1, sizeof(char));      pNew->character = line;     pNew->link = NULL;      if (pPre == NULL)     {         pNew->link = NULL;          if (pList == Zilch)         {             pList = pPre = pNew;         }         else         {             pPre->link = pNew;             pPre = pNew;         }     }      return pList; }  void printList (NODE* pList) {     NODE* pWalker;      pWalker = pList;     printf ("\nList contains:\north");      while (pWalker)     {         printf ("%s %d %due south\n", pWalker->grapheme.name, pWalker->character.age, pWalker->character.gender);         pWalker = pWalker->link;     }     printf ("<Stop of List>\n\n");     render; }            

This post has been edited by riddinon24z: 02 August 2011 - 08:57 PM

#7 jimblumberg User is offline

Reputation: 5916

  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Read a text file into a linked listing

Posted 02 August 2011 - 09:xiv PM

I would modify line 82 from:
printf("%due south %d %southward", line.proper noun, line.age, line.gender);
To:
printf("~%s~%d~%s~", line.name, line.historic period, line.gender);
And make sure that yous are reading the lines correctly in the function.

Also this snippet has a couple of problems:

              while (fgets(input, 34, pData));     {         getData(pData, line, input);         pList = insertNode (pList, pPre, line);     }            

How have y'all defined the getData function?

void getData (FILE* pData, Data line, char input []);            

You are passing the structure into the role by value, that means that any changes made in the office are lost when you return from the function. You probably should be passing the function a pointer to the structure.

You first need to insure that you are reading the file correctly.

Jim

#8 riddinon24z User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 22
  • Joined: 22-July xi

Re: Read a text file into a linked listing

Posted 02 August 2011 - 09:31 PM

Oh I estimate I kinda forgot c programming 101. And then that means my insertNode function will as well be affected by your advice. I changed the blazon of the getData function again but this fourth dimension to my structure type Data. My output is now this:

Quote

Dandy Gazoo xi Unknown
List contains:
Bully Gazoo 11 Unknown
<End of List>

Process returned 0 (0x0) execution time : 0.151 s
Press any key to continue.

Why does it bound to the last line? Might it help if I changed the type of getData back to bool like my original style? I'll try it.

#include <stdio.h> #include <stdlib.h> #include <cord.h>  #define LENGTH_OF_INPUT 35  typedef int KEY_TYPE; typedef struct {     char* name;     KEY_TYPE age;     char* gender; }Data;  typedef struct nodeTag {     Data graphic symbol;     struct nodeTag* link; }NODE;  NODE* buildList (NODE* pList); Information getData (FILE* pData, DATA line, char input []); NODE* insertNode (NODE* pList, NODE** pPre, Data line); void printList (NODE* pList);  int main(void) {     NODE* pList;      pList = Zip;      pList = buildList (pList);      printList (pList);      return 0; }  NODE* buildList (NODE* pList) {     char input [LENGTH_OF_INPUT];     FILE* pData;     NODE* pPre;     DATA line;      pPre = Nil;      if ((pData = fopen("lab6data.txt", "r"))==Naught)     {         printf("Fault opening text file.\n");         leave (100);     }      while (fgets(input, 34, pData));     {         line = getData(pData, line, input);         printf("%due south %d %southward", line.proper noun, line.historic period, line.gender);         pList = insertNode (pList, &pPre, line);     }      if (fclose(pData) == EOF ) 	{ 	printf("Error closing lab6data.txt\n"); 	exit (300); 	}      return pList; }  DATA getData (FILE* pData, DATA line, char input []) {     char name [LENGTH_OF_INPUT];     char gender [LENGTH_OF_INPUT];      sscanf(input, "%xix[^\t]%d %[^\northward]", name, &line.historic period, gender);      line.proper name = calloc(strlen(name) + ane, sizeof(char));     strcpy(line.proper noun, name);      line.gender = calloc(strlen(gender) + 1, sizeof(char));     strcpy(line.gender, gender);      return line; }  NODE* insertNode (NODE* pList, NODE** pPre, DATA line) {     NODE* pNew;      if (!(pNew = (NODE*)malloc(sizeof(NODE))))     {         printf("Retention overflow in insert\n");         go out(200);     }      pNew->character.proper noun = calloc(strlen(line.name) + ane, sizeof(char));     pNew->graphic symbol.gender = calloc(strlen(line.gender) + 1, sizeof(char));      pNew->character = line;     pNew->link = Goose egg;      if (pList == NULL)     {         pList = (*pPre) = pNew;     }     else     {         (*pPre)->link = pNew;         (*pPre) = pNew;     }      return pList; }  void printList (NODE* pList) {     NODE* pWalker;      pWalker = pList;     printf ("\nList contains:\n");      while (pWalker)     {         printf ("%s %d %due south\northward", pWalker->character.name, pWalker->grapheme.age, pWalker->graphic symbol.gender);         pWalker = pWalker->link;     }     printf ("<End of List>\north");     return; }            

This post has been edited by riddinon24z: 02 August 2011 - 09:48 PM

#9 riddinon24z User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 22
  • Joined: 22-July eleven

Re: Read a text file into a linked list

Posted 03 August 2011 - 06:22 PM

View Postriddinon24z, on 03 August 2011 - 03:14 AM, said:

I'm simply thinking out loud. What I'm trying to exercise is read one line of data from the text file into a node. And so insert that node into my list and put that unabridged process into a loop until all lines are read. Exercise I accept the right idea, looking at my code from method 1?

If instead I tried to insert the nodes in numerical order based on the historic period, which would be new->.character.age, is information technology illogical or incorrect for me to have my if statement the opposite of my while loop argument for checking the values?

NODE* getDataAndInsert (FILE* pData) {     NODE* pList;     Data line;     char input [LENGTH_OF_INPUT];     char name [LENGTH_OF_INPUT];     char gender [LENGTH_OF_INPUT];      pList = NULL;      while (fgets(input, 34, pData))     {         sscanf(input, "%19[^\t]%d %[^\n]", name, &line.historic period, gender);          line.proper name = calloc(strlen(proper name) + i, sizeof(char));         strcpy(line.proper noun, name);          line.gender = calloc(strlen(gender) + 1, sizeof(char));         strcpy(line.gender, gender);          pList = insertNode (pList, line);     }      render pList; }  NODE* insertNode (NODE* pList, Information line) {     NODE* pNew;     NODE* pPre;     NODE* pCur;      if (!(pNew = (NODE*)malloc(sizeof(NODE))))     {         printf("Memory overflow in insert\n");         go out(200);     }      pNew->character.proper name = calloc(strlen(line.name) + i, sizeof(char));     pNew->character.gender = calloc(strlen(line.gender) + 1, sizeof(char));     pNew->character = line;      pPre = NULL;     pCur = pList;  	printf("%d\n", pNew->character.age);      while ((pNew->grapheme.age > pPre->grapheme.age) && (pCur->link != NULL))     {         traverse (&pPre, &pCur);     }      if (pNew->character.age <= pPre->grapheme.age)     {         pNew->link = pPre->link;         pPre->link = pNew;     }      else if (pCur == Zilch)     {         pNew->link = NULL;         pList = pNew;     }      return pList; }  void traverse (NODE** pPre, NODE** pCur) {     *pPre = (*pCur);     *pCur = (*pCur)->link;      render; }            

#ten riddinon24z User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 22
  • Joined: 22-July 11

Re: Read a text file into a linked list

Posted 03 Baronial 2011 - 06:39 PM

I changed a portion of the insertNode function. Am I non accounting for every situation?

while ((pNew->character.historic period < pPre->character.age) && (pCur->link != NULL))     {         traverse (&pPre, &pCur);     }      if (pNew->grapheme.age >= pPre->character.age)     {         pNew->link = pPre->link;         pPre->link = pNew;     }      else if (pCur->link == NULL)     {         pNew->link = pCur->link;         pPre->link = pNew;     }      else if (pList == Nil)     {         pNew->link = Nix;         pList = pNew;     }      else     {         ;     }      render pList;            

This postal service has been edited by riddinon24z: 03 Baronial 2011 - 06:55 PM

#11 riddinon24z User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 22
  • Joined: 22-July 11

Re: Read a text file into a linked list

Posted 03 August 2011 - 07:46 PM

I keep coming back to something like this:

pPre = Cypher;     pCur = pList;  	printf("%d\due north", pNew->grapheme.historic period);      while ((pNew->character.age < pPre->graphic symbol.age) && (pCur->link != NULL))     {         traverse (&pPre, &pCur);     }      if (pNew->character.historic period > pPre->graphic symbol.age)     {         pNew->link = pPre->link;         pPre->link = pNew;     }      else if (pPre == Nil)     {         pNew->link = pList;         pList = pNew;     }      else if (pCur->link == NULL)     {         pNew->link = pCur->link;         pCur->link = pNew;     }      else     {         ;     }      return pList;            

The first role of the if argument inserts in the center of the list. The second office inserts to an empty list or before the first node. And the third office inserts at the terminate. Or at least that'southward the goal.

#12 riddinon24z User is offline

  • New D.I.C Caput

Reputation: 0

  • View blog
  • Posts: 22
  • Joined: 22-July xi

Re: Read a text file into a linked list

Posted 03 August 2011 - x:fifty PM

Am I making this way more complicated than it should exist? Would it help if I initialized pPre and pCur to something else and become from there instead of this?

pPre = NULL;     pCur = pList;  	printf("%d\due north", pNew->character.age);      while ((pNew->graphic symbol.historic period > pCur->grapheme.age) && (pCur != Zilch) && (pList != Nothing))     {         traverse (&pPre, &pCur);     }      if ((pPre == NULL) || (pList == NULL))     {         pNew->link = pList;         pList = pNew;     }      else if ((pPre != NULL) && (pNew->graphic symbol.age > pPre->character.age))     {         pNew->link = pPre->link;         pPre->link = pNew;     }      else if (pCur == Nil)          {         pNew->link = pCur->link;         pCur->link = pNew;     }      else     {         ;     }      pPre = Goose egg;     pCur = NULL;      render;            

wilsonexpressel.blogspot.com

Source: https://www.dreamincode.net/forums/topic/242087-read-a-text-file-into-a-linked-list/

0 Response to "Read File in C++ Into a Linked List"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel