Apr 25, 2011 at 7:53pm UTC
hellow every body
now i create a file excel which i enter data in it
ofstream fout("Test.xls", ios::out);
but every time i run my programme it create a new file and replace it with old
so i want only one file which it create
thx in advance
Apr 25, 2011 at 7:57pm UTC
ios::out overwrites by default, you might want to add the ios::app flag to append content. btw, aren't xls files binary?
Apr 25, 2011 at 8:05pm UTC
@ OP: Zeillinger nailed it, you're better off using ".tab" or ".csv" which are both Excel compatible but will not throw up an error when you try to open it. Also ios::trunc will always overwrite the old file with the new one.
Apr 25, 2011 at 8:31pm UTC
@ computergeek01 me try to use ios::trunc and also create new file every time i run my programme
Apr 25, 2011 at 8:36pm UTC
ios::trunc overwrites any file with the same name. There is no built in way to create a new file name every time you run the program, you would need to write code for that.
Apr 25, 2011 at 8:37pm UTC
so me have 4 case
so i want for exmpale when i enter to case 1 and save date and then enter to case 2 and save data under case 1 data .........
Apr 25, 2011 at 8:40pm UTC
Just don't close your file until right before you end your application and you'll be fine using ios::trunc.
Apr 25, 2011 at 8:54pm UTC
thats my code
so i want you tell me where i add the syntax excatly
int main()
{
ofstream fout("Test.xls", ios::trunc);
ofstream fout1("Time.xls ", ios::trunc);
quick q;
int choose;
cout<<" welcome in our quick sort programme\n\n";
cout<<"1-pivot in middle \n\n" ;
cout<<"2-pivot is random \n\n" ;
cout<<"3-pivot median \n\n" ;
cout<<"4-pivot is The median of 0.1% of the data \n\n";
cout<<"your choose : " ;
cin >> choose;
system("cls");
_sleep(500);
cout<<"enter the size \n";
cin>>q.size;
q.arr=new int[q.size];
cout << "enter the range of numbers \n\n";
// the range of random numbers
cin>>q.c; cout<<"-------------->\n";
cin>>q.d;
q.randfun();
/*
cout << "\nBefore sorting:\n\n ";
for (int i = 0; i < q.size; i++)
{
cout << q.arr[i] << " ";
}
cout<<"\n"<<"\n";
cout<<" loading sorting" <<setw(10)<<"........\n\n ";
_sleep(1000);
*/
switch(choose)
{
case 1:
{
fout1<<"Time When pivot in middle \n";
fout << "\nNormal sorting :\n";
clock_t before=clock();
q.quicksort(q.arr,0,q.size-1);
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after=clock();
fout1 << "Normal Time is : " << ((after-before)*1.0)/CLOCKS_PER_SEC << " Second\n";
clock_t before1=clock();
reverse(q.arr,q.arr+q.size);
fout<<"\nReverse sorted\n\n";
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after1=clock();
fout1 << "Reverse Time is : " << ((after1-before1)*1.0)/CLOCKS_PER_SEC << " Second\n";
clock_t before2=clock();
sort(q.arr,q.arr+(q.size/2));
fout<<"\nNearly sorted\n\n";
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";}
clock_t after2=clock();
fout1 << "NearBy Time is : " << ((after2-before2)*1.0)/CLOCKS_PER_SEC << " Second\n";
break ;
}
case 2:
{
fout1<<"\n \n \n \n \n \n \n \n ";
clock_t before=clock();
fout1<<"Time When pivot is random \n";
fout << "\nNormal sorting :\n";
q. quicksort2(q.arr,0,q.size-1);
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after=clock();
fout1 << "Normal Time is : " << ((after-before)*1.0)/CLOCKS_PER_SEC << " Second\n";
clock_t before1=clock();
reverse(q.arr,q.arr+q.size);
fout<<"\nReverse sorted\n\n";
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after1=clock();
fout1 << "reverse Time is : " << ((after1-before1)*1.0)/CLOCKS_PER_SEC << " Second\n";
clock_t before2=clock();
sort(q.arr,q.arr+(q.size/2));
fout<<"\nNearly sorted\n\n";
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after2=clock();
fout1 << "nearly Time is : " << ((after2-before2)*1.0)/CLOCKS_PER_SEC << " Second\n";
break;
}
case 3:
{
fout << "\nNormal sorting :\n";
clock_t before=clock();
q.quicksort3(q.arr,0,q.size-1);
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after=clock();
fout1 << "Normaly Time is : " << ((after-before)*1.0)/CLOCKS_PER_SEC << " Second\n";
clock_t before1=clock();
reverse(q.arr,q.arr+q.size);
fout<<"\nReverse sorted\n\n";
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after1=clock();
fout1 << "reverse Time is : " << ((after1-before1)*1.0)/CLOCKS_PER_SEC << " Second\n";
clock_t before2=clock();
sort(q.arr,q.arr+(q.size/2));
fout<<"\nNearly sorted\n\n";
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after2=clock();
fout1 << "nearly Time is : " << ((after2-before2)*1.0)/CLOCKS_PER_SEC << " Second\n";
break;
}
case 4:
{
fout << "\nNormal sorting :\n";
clock_t before=clock();
q.quicksort4(q.arr,0,q.size-1);
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after=clock();
fout1 << "nearly Time is : " << ((after-before)*1.0)/CLOCKS_PER_SEC << " Second\n";
clock_t before1=clock();
reverse(q.arr,q.arr+q.size);
fout<<"\nReverse sorted\n\n";
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after1=clock();
fout1 << "reverse Time is : " << ((after1-before1)*1.0)/CLOCKS_PER_SEC << " Second\n";
clock_t before2=clock();
sort(q.arr,q.arr+(q.size/2));
fout<<"\nNearly sorted\n\n";
for(int i=0;i<q.size;i++){
fout<<q.arr[i]<<" ";
}
clock_t after2=clock();
fout1 << "nearly Time is : " << ((after2-before2)*1.0)/CLOCKS_PER_SEC << " Second\n";
break;
}
}
system("pause");
return 0;
}
Apr 25, 2011 at 8:59pm UTC
Have you run this code? Does it do what you want it to? If yes then you're welcome. If not then how does it differ from what you are expecting?
Apr 25, 2011 at 9:02pm UTC
yes it run correctly but i dont want make new excel file every time it run i want data save and dont clear
Apr 25, 2011 at 9:03pm UTC
Then you need to use ios::app like Zeillinger suggested.
Apr 25, 2011 at 9:11pm UTC
wooooooooooow now it work correctly thanks every body for help
Apr 25, 2011 at 9:16pm UTC
but Sorry i have another quection
now it save case 1 and cas2 and case3 and case4 sequetional
if i run the programme again and enter to case 1 and have a new data so i want replace old data with new data :)
Apr 25, 2011 at 9:20pm UTC
If that was the case I would tear this down and start over. It would be too much of a mess to fit that kind of thing in here.
You would replace your 'ofstream' objects with 'fstream' objects which allow for both input and output on the same file.
You would have a function that loads the entries that are already in the file into memory, I highley recommend using either a class or a struct to hold the data.
You would need some way of displaying the data on the screen, use a member function for this.
Have you read the tutorial on this site yet?
Apr 25, 2011 at 9:28pm UTC
yes i know what you mean and sure me dont like tire my self :D but it is assignment and our Dr asked us to make this by this way
Apr 25, 2011 at 9:47pm UTC
You could start by trying to make some of the changes I suggested. Are you familiar with objects like structs and classes yet?