473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Read from file to linked list c++

4
I need to read a txt.file in a linked list , but nothing is displayed .
Expand|Select|Wrap|Line Numbers
  1. #include <cstdio>
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. struct MyStruct
  7. {
  8.     char name[20];
  9.     char surename[20];
  10. };
  11.  
  12. int main()
  13. {
  14.  
  15.     int N;
  16.     cout<<"enter N: ";
  17.     cin>>N;
  18.     MyStruct apartment;
  19.     ofstream f;
  20.     f.open("C:\\Wext1.txt");
  21.     for(int i=0; i<N; i++)
  22.     {
  23.         cout<<"enter name: ";
  24.         cin>>apartment.name;
  25.         f << apartment.name << " ";
  26.         cout<<"enter surename: ";
  27.         cin>>apartment.surename;
  28.         f << apartment.surename << " "<<endl;
  29.     }
  30.     return 0;
  31. }
  32.  
  33. #include <iostream>
  34. #include<fstream>
  35. using namespace std;
  36. struct MyStruct
  37. {
  38.     char name[20];
  39.     char surename [20];
  40.     MyStruct *next;
  41. };
  42. void create_list(MyStruct* &h1,ifstream &f)
  43. {
  44. MyStruct* temp;
  45. h1=new MyStruct;
  46. temp=h1;
  47. f>>temp->name>>temp->surename;
  48. while(f.peek()!='\n')
  49. {
  50. temp->next=new MyStruct;
  51. temp=temp->next;
  52. temp->next=NULL;
  53. f>>temp->name>>temp->surename;
  54. }
  55. }
  56. int main()
  57. {
  58.     ifstream file("C:\\Wext1.txt");
  59.     if(!file.is_open())
  60.         return -1;
  61. MyStruct *list;
  62. create_list(list,file);
  63. while (list!=NULL)
  64. {
  65.     cout<<list->name<<list->surename<<endl;
  66.     list=list->next;
  67. }
  68. file.close();
  69.     return 0;
  70.  
  71.  
  72.  
  73. }
  74.  
  75.  
May 30 '15 #1

✓ answered by kiseitai2

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!

4 3119
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!
May 30 '15 #2
weaknessforcats
9,208 Expert Mod 8TB
You won't be able to read the file this way:

Expand|Select|Wrap|Line Numbers
  1. f>>temp->name>>temp->surename;
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. struct LinkedList
  2. {
  3.     Node* thelist;
  4. };
  5. struct Node
  6. {
  7.     MyStruct* data;
  8.     Node* next;
  9. };
  10.  
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.
May 30 '15 #3
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
May 30 '15 #4
xxzl
4
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.
May 30 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the...
3
by: Francis Bell | last post by:
Hello, I'm trying to read data from a file and then insert that into a linked list. The way I have it, the program compiles, however, I'm getting a segmentation fault error message when I run...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
2
by: Subodh | last post by:
Hi All, I want to use a C++ API in a static lib that returns a linked List in C# I am planning to P/Invoke to perform the Interop, I would like to know which way will be better to interop a...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
19
beacon
by: beacon | last post by:
As you can probably tell from the title, I'm a little frustrated with linked lists. I'm working on a homework assignment and it just isn't making sense to me. I've read and read and read on the...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
8
by: dmp | last post by:
What are Linked list? Please somebody show some ready made programs of linked list
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: antar2 | last post by:
Hello, I can not find out how to read a file into a list of lists. I know how to split a text into a list sentences = line.split(\n) following text for example should be considered as a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.