I had got an assignment for sorting some data..
"Make a c++ program which takes data from a TEXT FILE, then sort it"..
i made this program..

#include <iostream.h>         
#include <conio.h>            
int main ()                    
{                              
clrscr();                   
int sort[8];               
cout<<"Enter DATA to be Sorted\n";      
for(int get=0;get<=7;get++)                 
cin>>sort[get];                               //cin saves the values in array SORT
for(int a=0; a<=7;a++)                //FOR loop runs 8 times(0-7)..8-Passes
{                                     //Loop BODY starts
for(int b=0;b<=7; b++)       
{                             
int save=sort[b];      
if(sort[b]>sort[b+1]) 
{                          //if-BODY starts
sort[b]=sort[b+1];         //Assigns value, if sort[b+1] is greater than sort[b]
sort[b+1]=save;            //Assign value of save to b+1
}                          //IF body ends
}                             //Nested FOR body ends
}                                      //First FOR body ends
cout<<"\nSorted data is:";
for(int i=0;i<=7;i++)                          //FOR loop to print the values
cout<<sort[i]<<" ";                            //COUT displays the sorted array
getch();                       //Freezes screen until gets a character from keyboard
return 0;                      //Return Statement
}

Now he is saying that this code gets data from Keyboard, while it is supposed to get from a TEXT FILE,, so he suggested to use some sile STREAMS....
Can any one help me,, how to do it..??

what data does the text file contain. In order to read the file you have to know what the file contains.

You can use ifstream for that purpose. Assuming that you have a file named numbers.txt (containing numbers),
you could read it as follows

ifstream infile("numbers.txt");
int nn;
while( infile >> nn )    // Read one number at a time
{
    printf("%d\n", nn);  // Display the number read
}

I dont know..He has just said us to get data from a text file and sort it using File Streams..

Well you better decide how you want the file because you can't do a thing until you know what the file contains. Your teacher probably left that up to you.

The easiest way is to create a text file with one number per line -- you can create the file with Notepad or any other text editor. Then just read the file as previously shown.

Its giving me "Undefined symbol ifstream"and Undefined Symbol Infile..:(

did you include <fstream> ?

Yeaa..i included it,,but i was including <ofstream>..and i also forgot to include <stdio>..!!
well thanks alOOt..!!

But now im unable to sort them out....because in arrays, values are assigned to a[0], a[1], and so on....but how can we sort values from a disk-file by above code, which i used for keyboard values..!!

The following is pretty much like your original construct using an array of ints,
the difference is that the numbers are read from the file instead of the keyboard

const int MAX_NUM = 8;
int sort[MAX_NUM] = {0};
int count = 0;  // Count of numbers in the 'sort' array
ifstream infile("numbers.txt");

// At max, read MAX_NUM numbers into the int array 'sort'
for( ; count < MAX_NUM && infile >> sort[count]; count ++)
{
    cout << sort[count] << "\n";
}
cout << "Read " << count << " numbers ...\n"; // Display the count of numbers read

If Im using PRINTF at 5th step..then in result first it shows data/numbers from the disk-file..and then it prints.."Read 8 numbers..."
its not sorting the data..and if im not using PRINTF..then it simply displays the string"Read..."
and secondly im not able to get that in above code..which line is used to assign values of disk-file to the Sort Array..??

Please tell me..that which line in above code is assigning the values of Disk-file to the array SORT....??
and secondly above code is just showing the String..its not sorting data..Quite helpless,im ..!!

>>..that which line in above code is assigning the values of Disk-file to the arra
line 7 infile >> sort[count] >>and secondly above code is just showing the String..its not sorting data..
All you asked was how to read the data from disk instead of from the keyboard, so that's all he posted. you already posted the sorting code on your original post. Just do what you did before.

Thanks, So much to you both for solving my problem..
Thank You..!!

Okay..n whats happening in 2nd step, Why there we initialize array to 0; and then whats the purpose of >> operater..

Secondly in FOR-LOOP can we use two variables; like there we used one variable "count" in initialization and in increament, while in conditional part, we used "infile>>sort[]",which doesnt invole count..

>>Why there we initialize array to 0
To insure all array elements have a known initial value. May programs have gone sour because of uninitialized variables. Its a good habbit to initialize all variables and arrays when they are declared.

>>and then whats the purpose of >> operater
That is the file extraction operator, it reads one item from the file into the specified array element.

>>Secondly in FOR-LOOP can we use two variables; like there we used one variable "count"
>>in initialization and in increament, while in conditional part, we used "infile>>sort
>>[]",which doesnt invole count

Yes, you can have any number of statements there. The infile >> sort[count] just says while not end-of-file and extracts another item from the file and puts it into the array

It means that >> is a cin operator....
okay..
>> and we can also initialize count=0 in for loop as well na....
>>and what abt (count<max_num) in for loop..

It means that >> is a cin operator....
okay..
>> and we can also initialize count=0 in for loop as well na....
>>and what abt (count<max_num) in for loop..

Are you asking or telling? >> is used by cin too but it also applies to all other input streams in <fstream> and <iostream>

Was Asking, because i thought >> only works with cin..
n wht abt count=0; is it necessary to initialize it in MAIN()..cant we initialize it FOR-LOOP..??
and whatz the purpose of count<max_num..??

You are asking some very basic questions that most tutorials can probably answer, such as this one. Start at the top of that tutorial and take your time reading through it.

How to find the Loop-Invariant of sorting program like of Bubble Sort..??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.