How about my problem is as mentioned in the title, I have knowledge of how to make simple and double linked lists in c++, but I have no idea how to pass information from a text file to a simple list. I have a program built in c++ with indexes on files and my program does all of this:
- Add
- To show
- Search
- Remove
- Modify
- show indices
But the only option I'm missing is Display Orderly . For this they ask me to use a TDA and I have to take the data from the text file and add it to the TDA to later sort and display it. My project handles 2 text files since it is for indexes, one is Datos.txt (saves all the information) and another is Indice.txt (saves the indexes).
The data requested when adding is the following:
cout << "Codigo: ";
cin.getline(Dis.codigo,10);
cout << "Nombre: ";
cin.getline(Dis.nombre,30);
cout << "Cantante: ";
cin.getline(Dis.cantante,30);
cout << "Genero: ";
cin.getline(Dis.genero,20);
cout << "Precio: ";
fflush(stdin);
cin.getline(Dis.precio,10);
cout << "Discografia: ";
cin.getline(Dis.discografia,20);
cout << "Disco agregado correctamente" << endl;
system("pause");
ofstream a("datos.txt",ios::app);
ofstream b("indice.txt",ios::app);
a.write((char*)&Dis,sizeof(Dis));
a.seekp(0,ios::end);
Ind.pos=a.tellp()/sizeof(Dis);
strcpy(Ind.codigo,codigo);
b.write((char*)&Ind,sizeof(Ind));
b.close();
a.close();
This data is saved in the Data.txt file and the code is saved in Index.txt along with a position. I hope that my problem statement makes me understand that it is only how to add the data that is in the .txt to a simple linked list and display it in an orderly manner.
I try to understand the situation.
Mission : Dump two text files into a list of disks .
➢ Creation of a TDA or TAD (abstract data type)
The first thing is to create the structure
Dis
(disk), in this way.Note: This type of structure cannot come in a text file, since a
string
is a pointer to a string of characters, which have a dynamic size. That is, all integers occupy 32 or 64 bits depending on the architecture, it is always the same number, but a string,"Pedro"
, does not occupy the same as"Juan"
, the first 4 and the second 5, and despite that, both are of type string, then you can't dosizeof
.➢ Manage a file
For file handling, the standard library is needed
stdio
, itmain
should look like this. Hestdlib
is needed from time to time, so we leave it, without paying attention to what it is for.For this it is necessary to know how files are handled in C , although it also works in C++ since it is a cumulative language. So I found the following link dealing with that topic:
C Programming - File Handling - WikiBooks .
FILE
.Pseudo-code:
➢ Open a file
A list of opentype parameters for the fopen function are:
In our case we are going to use Non-Binary Read , that is
"r"
, since we are handling filestxt
(plain text) and not images or audio.Code: These are steps 1 and 2 on the same line.
➢ Read a file
In our case we are interested in
fgetc
, since the structureDis
cannot come in a file.Code: This makes it read one character at a time.
➢ Close a file
Code:
We have already seen the basic operations about file operations. Now we are left to think about how to dump it to the data structure
Dis
.Since you didn't show an example text file for how the data can come from the disk, I'm going to assume it has the following format . Moreover, I think that in this way it is not necessary to read the index file , so I will not touch that topic.
For that, two exceptions must be taken into account, any character except spaces, (that is,
' '
,'\x20'
), or a line break, ('\n
,\r
,\x0D
,\x0A
). The space means the next field in the structure, the line break, the next disk.➢ How to read a file with this new format?
Code: We take the previous code about reading the character and add things to it.
Note: I leave some things undone so you can practice in peace, they are not that difficult either.
➢ Organization of a structure
In the following example I am going to order the disks by code. It should be clarified that the most efficient ordering method is the one that allows binary search, this is only effective and is slow compared to the other.
Note: Again, I'll challenge you with those little details.