Read from file to linked list c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xxzl
    New Member
    • May 2015
    • 4

    Read from file to linked list c++

    I need to read a txt.file in a linked list , but nothing is displayed .
    Code:
    #include <cstdio>
    #include <fstream>
    #include <iostream>
    using namespace std;
     
    struct MyStruct
    {
        char name[20];
        char surename[20];
    };
     
    int main()
    {
        
        int N;
        cout<<"enter N: ";
        cin>>N;
        MyStruct apartment;
        ofstream f;
        f.open("C:\\Wext1.txt");
        for(int i=0; i<N; i++)
        {
            cout<<"enter name: ";
            cin>>apartment.name;
            f << apartment.name << " ";
            cout<<"enter surename: ";
            cin>>apartment.surename;
            f << apartment.surename << " "<<endl;
        }
    	return 0;
    }
    
    #include <iostream>
    #include<fstream>
    using namespace std;
    struct MyStruct
    {
        char name[20];
    	char surename [20];
        MyStruct *next;
    };
    void create_list(MyStruct* &h1,ifstream &f)
    {
    MyStruct* temp;
    h1=new MyStruct;
    temp=h1;
    f>>temp->name>>temp->surename;
    while(f.peek()!='\n')
    {
    temp->next=new MyStruct;
    temp=temp->next;
    temp->next=NULL;
    f>>temp->name>>temp->surename;
    }
    }
    int main()
    {
        ifstream file("C:\\Wext1.txt");
    	if(!file.is_open())
    		return -1;
    MyStruct *list;
    create_list(list,file);
    while (list!=NULL)
    {
        cout<<list->name<<list->surename<<endl;
        list=list->next;
    }
    file.close();
        return 0;
    
    
    
    }
  • kiseitai2
    New Member
    • Jul 2007
    • 93

    #2
    First, I see you need to close the output file (f.close()) in the first part. However, because the program calls the destructor at the end of its execution, it shouldn't be a problem in this program. The issue is permissions. You are trying to write to a directory in which the program does not have write permission. If you try "Wext1.txt" instead of "C:\\Wext1.txt, " you will see your file near the executable with all the data. Alternatively, you can run the program as administrator and it will be able to create the file at C! Then, the other program will be able to open a valid file and actually read its data!

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You won't be able to read the file this way:

      Code:
      f>>temp->name>>temp->surename;
      name and surename are char arrays.

      1) there is no guarantee the arrays have been fully entered.

      2) the >> operator stops scanning on the first whitespace charcter. "Jo Ann Smith" will place Jo in name and Ann in surename. Then Smith will show up in the next element as the name.

      If you have arrays of 20 for name and surename, be sure to write 20 bytes for each array. Then do not use >> to read the file. Instead, do an actual read and specify 20 bytes for each array.

      Unless you know the format of the file when it was written, you won't be able to read it later.

      3) There are two MyStruct definitions. And they have different members.

      4)The linked list should look like:

      Code:
      struct LinkedList
      {
          Node* thelist;
      };
      struct Node
      {
          MyStruct* data;
          Node* next;
      };
      as an example.

      Personally, I would write a program that is just a linked list and get that debugged. Then I would reuse the code (except for main()) in the MyStruct program

      5) Lastly, try to write modular functions rather than using serial logic.

      Comment

      • kiseitai2
        New Member
        • Jul 2007
        • 93

        #4
        Oops, I assumed this person understood how the shift operator worked but was trying to find out why nothing was getting outputted. I also assumed we are looking at the code for two different programs tagged in a single set of code tags. Hence, why we see two different MyStruct definitions (with the second one being the actual linked list node structure) and two main functions. Now that s/he knows about file access permissions, I recommend following weaknessforcats ' suggestions!

        P.S. What would we do without you? :D

        Comment

        • xxzl
          New Member
          • May 2015
          • 4

          #5
          I want to say thank you Weaknessforcats for what you have to explain everything in detail and really try to help. I started learning C ++, just five months ago and is still not good at programming. I can not understand the construction of the program when it is necessary to read from a file in a linked list. Could you show an example of how to read from a file in a linked list, or say where about it normally is written.

          Comment

          Working...