| | |
How to read and sort data in a textfile (ascending order)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
I've been surfing the net for hours now looking for a good example of reading and sorting data from a textfile.
But I can't find a good one that is working.
Can somebody give me a simple example code of reading and sorting data from a textfile in ascending order plz?
The textfile will just contain numbers and name just like this one
and when read and sorted, it will be displayed like this
Thanks in advance for someone who could help me.
But I can't find a good one that is working.
Can somebody give me a simple example code of reading and sorting data from a textfile in ascending order plz? •
•
•
•
5 jose
2 juan
3 miguel
1 luis
4 manuel
•
•
•
•
1 luis
2 juan
3 miguel
4 manuel
5 jose
•
•
Join Date: Feb 2009
Posts: 141
Reputation:
Solved Threads: 2
0
#2 31 Days Ago
Do you have any code you worked on at all?
As the rules state above this section, we help those who show work.
I'll help you get started
As the rules state above this section, we help those who show work.
I'll help you get started
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstrea> file input output stream library using namespace std; int main() { ifstream fin; //declare fin as a variable of input file stream which is used to read IN from a file fin.open("text.txt"); //open file //Read from file //Store data in array, or array of structs. //Use a sorting algorithm to put the data in order. return 0; }
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
0
#4 31 Days Ago
Do you know how many items or a maximum number of items there are in the file? If not an array probably isn't the best container to hold the data, unless that it is the container type you are required to use.
Do you know about classes and structs, because they will make the task much easier.
The basic protocol for loading the array might look something like this:
Do you know about classes and structs, because they will make the task much easier.
The basic protocol for loading the array might look something like this:
C++ Syntax (Toggle Plain Text)
declare an int to act as an index and initialize it to an appropriate value declare an array of class objects of an appropriate size declare an ifstream and associate it with an appropriate file if unable to open file { tell user problem occured and exit program or do something more elegant } while (you are able to read in an object into the current element of the array using the ifstream and the number of objects is below the capacity of the array) { increment the current index }
Klatu Barada Nikto
![]() |
Similar Threads
- ascending order (C++)
- How to Sort Data in grid view from table using vbscript (JavaScript / DHTML / AJAX)
- put linked lists in ascending order (C++)
- Ascending order in arrays (C)
- how can we sort data from text file (C++)
- Array of objects, sorting it in ascending order of a certain attribute. (Java)
- C++ checking if numbers from a file are in ascending order (C++)
- Random number with ascending order help (C++)
Other Threads in the C++ Forum
- Previous Thread: Help formatting data..
- Next Thread: infinite while loop
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






