Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

how to read file to link list

//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
therock_80
Asked:
therock_80
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:


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
 
bkrahmerCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now