2015-01-15 25 views
0

我有一个程序,排序数组如何保存在文本文件?例如: :排序后的数组为:1,2,3,4,5。 我该如何保存名为的文本文件。排序元素”。 我试过很多方法,但数组排序不会在文本文件保存。 我是一个新手,所以我觉得很难。如何在文本文件中保存排序的数组?

这里是我的代码。

#include <iostream> 
using namespace std; 

int main() { 
    cout << "Enter number of element:"; 
    int n; cin >> n; 
    int a[n]; 
    for(int i=0;i<n;i++) 
    { 
     cout << "element number " << (i+1) << " : "; 
     cin >> a[i]; 
    } 
    int e=1, d=3; 
    int i, j, k, m, digit, row, col; 
    int length = sizeof(a)/sizeof(int); 
    int bmat[length][10]; 
    int c[10]; 
    for(m=1;m<=d;m++) 
    { 
     for(i=0;i<10;i++) 
     { 
      c[i]=-1; 
     } 
     for(i=0;i<length;i++) 
     { 
      digit=(a[i]/e)%10; 
      c[digit]++; 
      row=c[digit]; 
      col=digit; 
      bmat[row][col]=a[i]; 
     } 
     k=-1; 
     for(i=0;i<10;i++) 
     { 
      if(c[i]!=-1) 
      { 
       for(j=0;j<=c[i];j++) 
       { 
       k++; 
       a[k]=bmat[j][i]; 
       } 
      } 
     } 
     e=e*10; 
    } 

    cout << endl; 
    cout << "Sorted array:" << endl; 
    for(int i=0;i<n;i++) 
    { 
     cout << a[i] << " , "; 
    } 
    cout << endl; 
    system("pause"); 
    return 0; 
} 
+1

序列化是一种方法。 –

+0

打开文件并写入。您可以使用的排序功能进行排序。 – saadtaame

+2

'int a [n];'当n'不是常量表达式时* *不是*标准C++。相反,你应该使用'std :: vector'。 – crashmstr

回答

0
#include<iostream> 
#include<fstream> 
using namespace std; 

int compare(int, int); 
void sort(int[], const int); 
int compare(int x, int y){ 
    return(x > y); 
} 
void swap(int *x, int *y){ 
    int temp; 
    temp = *x; 
    *x = *y; 
    *y = temp; 
} 
void display(int array[], int n){ 
    for (int i = 0; i<n; i++) { 
     cout << array[i] << " "; 
    } 
    cout << endl; 
} 
void writeToFile(int array[], int n){ 
    ofstream myfile; 
    myfile.open("example.txt"); 
    for (int i = 0; i<n; i++) { 
     myfile << array[i]; 
     if (i != n - 1){ 
      myfile << ", "; 
     } 
    } 
    myfile.close(); 
} 
void sort(int table[], const int n) { 
    for (int i = 0; i < n; i++){ 
     for (int j = 0; j < n - 1; j++) { 
      if (compare(table[j], table[j + 1])) 
       swap(&table[j], &table[j + 1]); 
     } 
    } 
} 
int main(){ 
    int quantity; 
    int* tab; 
    ofstream outfile; 
    cout << "Enter number of element: "; 
    cin >> quantity; 
    tab = new int[quantity]; 
    cout << "Element:\n\n" << endl; 
    for (int i = 0; i < quantity; i++){ 
     int x = i; 
     cout << "#" << ++x << ":"; 
     cin >> tab[i]; 
    } 
    sort(tab, quantity); 
    cout << "The Sorted Elements are: "; 

    display(tab, quantity); 
    writeToFile(tab, quantity); 
    cout << endl; 
    getchar(); 
    getchar(); 
    //system("pause"); 
    return 0; 
} 
总之

,此块添加到您的代码:

ofstream myfile; 
myfile.open("example.txt"); 
for (int i = 0; i<n; i++) { 
    myfile << array[i]; 
    if (i != n - 1){ 
     myfile << ", "; 
    } 
} 
myfile.close(); 
0

您可以使用C++的fstream类,因为你想输出,你可以在这里使用的ofstream你应该只需更换一些“COUT” 与ofstream的实例:

在代码状态时,它的开头:

ofstream ofs("./sorted_elem.txt", ofstream::out); 

当想要输出:

ofs << "Sorted array:" << endl; 

    for(int i=0;i<n;i++) 
    { 
     ofs << a[i] << " , "; 
    } 
    ofs << endl; 
0

在C++中,你真的想用std::vector或其他一些用于存储数组数组的好容器。为了将数组写入文件,您需要打开文件并单独将每个元素写入文件(全部未经测试)。

#include <fstream> 

int main() 
{ 
    std::ofstream fp("output.txt"); 
    int data[5]; // todo: fill 

    for (unsitned i = 0; i < 5; ++i) 
    { 
    fp << data[i] << ' '; 
    } 
} 

并再次阅读:

#include <fstream> 

int main() 
{ 
    std::ifstream fp("output.txt"); 

    // todo: Determine the size of the array or guess it (don't guess it!) 
    unsigned array_size = 5; 
    int data[array_size]; 

    int n = 0; 
    while (fp.good() && n < array_size) fp >> data[n++]; 
} 

但是由于我们使用的是C++,我们可以使用std::vector

#include <fstream> 
#include <vector> 

int main() 
{ 
    std::vector<int> me(5); // todo: fill 

    std::ofstream fp("output.txt"); 

    for (size_t i = 0; i < me.size(); ++i) fp << me[i] << ' '; 

    // C++11: for (int d : me) fp << d << ' '; 
} 

而且,

#include <fstream> 
#include <vector> 

int main() 
{ 
    std::ifstream fp("output.txt"); 

    std::vector<int> data; 
    double buf; 
    while (fp >> buf) data.push_back(buf); // no longer need to guess 
} 
1

如果可以的话将排序后的数组写入std::cout,那么你可以把它写到一个文件中。在C++中,控制台与文件相同。

main末将这个:

cout << "Sorted array:" << endl; 
print_array(std::cout, a, n); // Show the results to the user. 

std::ofstream save("array.txt"); // Open a new file (or overwrite). 
print_array(save, a, n); // Save the results for later. 

system("pause"); 
return 0; 
} 

,并把打印的代码放在一个新的功能,它可以main之前定义:

void print_array(std::ostream & s, int * a, int n) { 
for(int i=0;i<n;i++) 
{ 
    s << a[i] << " , "; 
} 
s << endl; 
} 
2
//Use this code 

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <fstream> 
using namespace std; 



int main() 
{ 

    int n = 0; 
    cout << "Enter number of element:"; 
    cin >> n; 

    //Data Structure 
    std::vector<int> list; 
    //push back element in vector 
    for(register int i=0;i<n;++i) 
     list.push_back(rand()%10 + 1); 

    //do shuffling before sorting because rand() generates increasing order number 
    std::random_shuffle(list.begin(),list.end()); 

    std::sort(list.begin(),list.end()); 

    ofstream textfile; 
    textfile.open ("E:\\example.txt"); 
    for(size_t i= 0;i<list.size();++i) 
     textfile << list[i] <<" "; 

    textfile.close(); 
} 
+0

'rand'之前的'srand'是传统的,但'rand'的[天](http://www.open-std.org/jtc1/sc22/wg21/ docs/papers/2013/n3775.pdf)[编号](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3841.pdf)。 – Potatoswatter

+0

谢谢@Potatoswatter It's有用的文件。 –

0

我想,复制选项到目前为止还没有在这里展示。

请检查这个代码。 (假设你的矢量已经可以使用了,我已经跳过了它)。

该示例使用C数组和向量。尽可能在代码中使用后面的代码。但是,对于复制功能都可以工作:

#include <iostream>  
#include <iterator>  
#include <vector>  
#include <algorithm>  
#include <fstream>  

int main() { 

    int a[10]={0,1,2,3,4,5,6,7,8,9}; 
    std::vector<int> v; for (int i=0; i<10; ++i)v.push_back(i*10); //0, 10, 20,... 

    std::ofstream fs_a("c:/temp/out_a.txt"); 
    //store space separated 
    std::copy (a, a+sizeof(a)/sizeof(a[0]), std::ostream_iterator<int>(fs_a, " ")); 

    //store coma-separated, as one-liner 
    std::copy (v.begin(), v.end()), std::ostream_iterator<int>(std::ofstream("c:/temp/out_v.txt"), ",")); 
    return 0; 
} 
相关问题