?
Solved

how to read file to link list

Posted on 2003-02-26
2
Medium Priority
?
247 Views
Last Modified: 2013-12-14
//Read Title,Want value, Have value, Names of people to Linklist
//where Names is a string(Who) declared in a struct node waitHead
//which is declared in stockNode:
//as: newNode->waitHead->Who
//Problem is i can not read the string to Who string ??
//nameType declared as char [50];
//Thank for help




code:--------------------------------------------------------------------------------
typedef struct waitNode* waitPtrType;
struct waitNode
{
     nameType  Who;
     waitPtrType  Next;
};

typedef struct stockNode* stockPtrType;
struct stockNode
               {
                    itemClass Item;
                    waitPtrType waitHead,waitTail;
                    stockPtrType Next;
               };        

          stockPtrType Head;


void inventory::readFileBackup()

{
    nameType title; //Character array
    int want,have,i=0;
    itemClass newItem;
   
    ifstream iFile;

    iFile.open("data.txt", ios::in);
       if(!iFile)
          cout <<"No records in the data.txt file!!"<<endl;
        else
     {
        //First 3 string i read:          
     while(iFile>>newItem.Title>>newItem.Want>>newItem.Have)
            {//insert node in front of link list
              stockPtrType newNode = new stockNode;

               newNode->Item = newItem;            
           newNode->Next = Head;
                 
          while(iFile>>title)//How to read single string
              {
             strcpy(newNode->waitHead->Who,title);
          newNode->waitHead = newNode->waitHead->Next;
              }
            Head = newNode;                          
                     
            }//end of while    
       }
       iFile.close();
}
--------------------------------------------------------------------------------


Content of "data.txt" :

Title1 23 334
Bill
John
Peter

Title2 34 5
Eric
John

//Problem how to read data file like in this format?.
Thanks
0
Comment
Question by:therock_80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8029109


If you're using Borland's compiler, just derive a class from TStrings and use the built-in methods.

class TMyStrings : public TStrings
{
};




TMyStrings MyStrings;

MyStrings.LoadFromFile ("Data.Txt");



MyStrings now contains an array of AnsiStrings, one for each line in the original file.


Kdo
0
 
LVL 2

Accepted Solution

by:
bkrahmer earned 200 total points
ID: 8030604
This is one of the things I hate about C++.  There doesn't seem to be a lot of utility functions in the streams classes.  What I would do is read a line at a time using fgets, and then tokenize the line which has the three fields.  Also, consider using the function ios::good instead of "if (!file)".  You can still write good object-oriented code while still utilizing the basic C library.  
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question