Saving Linked List

This is a discussion on Saving Linked List within the C++ Programming forums, part of the General Programming Boards category; Hi everyone; I made a linked list with a class called Patient, but in the loop where I go through ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    40

    Saving Linked List

    Hi everyone;
    I made a linked list with a class called Patient, but in the loop where I go through each element, the programme doesnot work correctly(it Halts). It doesnt generate any compiler errors, it stops working whenever the function is provoked.
    The following is the code of the function to save all entries in a file
    :

    Code:
    void save(Patient* pNDS)
    {
         FILE *fp;  // Declare a pointer to a file structurepNDS=pHead;
         fp = fopen("D:junk.txt", "w+");
         pNDS=pHead;
         while(pNDS)
         {//Cheap try to save in file
                       // This opens a file called
                          //junk.dat for writing (and reading)
                          
                           fprintf(fp, "%s\t",pNDS->m_Name);
                           fprintf(fp, "%d\t",pNDS->m_ID);
                           fprintf(fp, "%d\n",pNDS->m_DOB);
                           fprintf(fp, "%s\t",pNDS->m_Address);
                           fprintf(fp, "%s\t",pNDS->m_PreMed);
                           
                           pNDS=pNDS->pNext;
         }
         fclose(fp);
    
    }

    If anyone can help or even comment, that would be great..............Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I am unaware of any file system where "D:junk.txt" is a valid filename. (EDIT: What am I thinking? It is valid on linux; don't know why I thought colon was a reserved character. However, I suspect you're not on a *nix system, so you will need a slash or two.) Even if you happen to be on such a file system, you should always check whether or not your fopen actually opened something. (EDIT: Also, post your C problems in the C forum.)
    Last edited by tabstop; 06-11-2011 at 04:52 PM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    40
    No I dont think that is the problem, I tried it just now and worked the same way. I used the same file writting code in the add(Patient* pNDS) function (that adds new objects to the list), it worked just fine, only problem is that it erases the previous entries and save only the last object.........so, any other ideas, maybe the sequence is wrong.?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You tried what just now and what worked the same way?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    Code:
    void save(Patient* pNDS)
    Code:
         pNDS=pHead;
    Why do you pass pNDS to save() then overwrite with the value of pHead?

    Tim S.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    40
    Good point there stahta, so I tried this code and doesnt work either:
    Code:
    inline void save(Patient* pNDS)
    {
         Patient* pL=pHead;
         FILE *fp;
         fp = fopen("D:\junk.txt", "w+");
    
         while(pL)
         {
                          
                           fprintf(fp, "%s\t",pNDS->m_Name);
                           fprintf(fp, "%d\t",pNDS->m_ID);
                           fprintf(fp, "%d\n",pNDS->m_DOB);
                           fprintf(fp, "%s\t",pNDS->m_Address);
                           fprintf(fp, "%s\t",pNDS->m_PreMed);
                           
                           pL=pL->pNext;
         }
         fclose(fp);
    
    }

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    40
    Oh, By it I meant the backslash in the file name.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    40
    Here is the hole code:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <string.h>
    #include <fstream>
    using namespace std;
    
    class Patient
    {
          public:
                  static int count;
                  int m_ID;
                  int m_DOB;
                  char m_PreMed[128];
                  char m_Address[128];
                  char m_Name[128];
                  Patient* pNext;
    };
    
    Patient* pHead=0;
    int Patient::count=0;
    
                          
    // add function is outside the class(not member function)
    void add(Patient* pNDS, int& refcount)
    {
          //add new object
          pNDS->pNext=pHead;
          pHead=pNDS;
          //fill in its member data            
                      // Improvised part..WORKS!!
                      cout<<"Enter the Patients name (use underscores for spaces): ";
                          char namebuffer[128];
                          cin>>namebuffer;
    
                      strncpy(pNDS->m_Name,namebuffer,128);
                          pNDS->m_Name[127]='\0';
                          
                          cout<<"\nEnter the Patients ID: ";
                          int PatientID;
                          cin>>PatientID;
                          pNDS->m_ID=PatientID; 
                          
                          cout<<"\nEnter the Patients year of birth: ";
                          int PatientDOB;
                          cin>>PatientDOB;
                          pNDS->m_DOB=PatientDOB;
                          
                          cout<<"\nEnter the Patients address: ";
                          char addressbuffer[128];
                          cin>>addressbuffer;
                          strncpy(pNDS->m_Address,addressbuffer,128);
                          pNDS->m_Address[127]='\0';
                          
                          cout<<"\nEnter Patients previous medications: ";
                          char premedbuffer[128];
                          cin>>premedbuffer;
                          strncpy(pNDS->m_PreMed,premedbuffer,128);
                          pNDS->m_PreMed[127]='\0';   
                          
                          refcount++;
                 }
                 
                 
                 
                 
    
    
    // Save to file ()
    inline void save(Patient* pNDS)
    {
         Patient* pL=pHead;
         FILE *fp;
         fp = fopen("D:\junk.txt", "w+");
    
         while(pL)
         {
                          
                           fprintf(fp, "%s\t",pNDS->m_Name);
                           fprintf(fp, "%d\t",pNDS->m_ID);
                           fprintf(fp, "%d\n",pNDS->m_DOB);
                           fprintf(fp, "%s\t",pNDS->m_Address);
                           fprintf(fp, "%s\t",pNDS->m_PreMed);
                           
                           pL=pL->pNext;
         }
         fclose(fp);
    
    }
                 
                 
    int main()
    {
        Patient objpat;
        Patient::count=0;
        //menu simulation
        
        char cChoice;
        do
        {
             cout<<"\tChoose the Operation by selecting the appropriate number:- "<<endl;
            cout<<"1) Create new Patient file.\n2) View All "<<Patient::count<<" entries.\n3) Quit."<<endl;
            
            int msg;
            cin>>msg;
        switch(msg)
        {
                   case 1:
                        { 
                            add(& objpat, Patient::count);
                            break;
                        }
                   case 2:
                        {
                            
                                break;
                        }
                   case 3:
                        
                        save(& objpat);
                        cChoice='Q';
                        break;
                   default:
                           cout<<"Invalid number";
        }
        }while(cChoice!='Q');
        
        system("PAUSE");
        return 0;
    }
    Please it has been bugging me for so long.............Thanks for your time.

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    "D:\\junk.txt"

    backslash is an escape character.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    A single backslash signifies escape to use a backslash in a literal you must use two backslashes:
    Code:
    fp = fopen("D:\\junk.txt", "w+");
    I am unaware of any file system where "D:junk.txt" is a valid filename. (EDIT: What am I thinking? It is valid on linux; don't know why I thought colon was a reserved character. However, I suspect you're not on a *nix system, so you will need a slash or two.)
    Actually that could be a valid file name in a Windows command shell as well.

    Jim
    Last edited by jimblumberg; 06-14-2011 at 11:42 AM.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Why are you mixing c and c++ file IO? Stick to C++ filesteams.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Thoughts:
    - Dump char. Use std::string.
    - Don't use cin >> with strings. Especially not C-style strings. That's just a disaster waiting to happen. Use std::getline for std::string.
    - Don't mix C and C++ I/O. Stick with C++ I/O.
    - Don't mix cin >> and std::getline. Or alternatively, use std::cin.ignore(...) after cin >>. This should become a problem after you switch.
    - Don't use str(n)cpy. Use std::string and the normal assignment operator.
    - Fix indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 05:37 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. Saving and Loading Linked Lists With File i/o
    By Red Army in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2002, 03:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21