【问题标题】:opening a txt file twice c++两次打开一个txt文件c ++
【发布时间】:2017-02-15 02:18:39
【问题描述】:

在我的程序中,我试图读取一个数字文件。我想取第一个数字并使该数字成为我数组的大小。关闭文件后,我想重新打开同一个文件并将其余数字存储到数组中。在数组中,我想使用冒泡排序算法对数字进行排序,并按升序显示数组。

int readData(int * arr);
void bsort(int * arr, int last);
void writeToConsole(int * arr, int last);
void swap(int, int);
int main()
{
   ifstream inFile("data.txt");
   int arrSize;
   inFile >> arrSize;
   int * arr = new int[arrSize];
   inFile.close();
   readData(arr);
   writeToConsole(arr, arrSize);
   system("pause");
   return 0;
}
int readData(int * arr)
{
   ifstream read("data.txt");
   int index = 0;
   for(int i = 0; i < 10; i++)
   {
       read >> *(arr + index);
       index++;
   }
   read.close();
   return *arr;
}
void bsort(int * arr, int last)
{
    for (int i = last - 1; i >= 0; i--)
    {
        for (int j = 1; j <= i; j++)
        {
            //this block is constant with respect to array size
            if (*(arr + (j - 1)) > *(arr + j))
            {
                int temp = *(arr + (j - 1));
                *(arr + (j - 1)) = *(arr + j);
                *(arr + j) = temp;
            }
        }
    }
}
void writeToConsole(int * arr, int last)
{
    bsort(arr, last);
    for (int i = 0; i < last; i++)
    {
        cout << *(arr + i) << " ";
    }
}
void swap(int x, int  y)
{
    int temp = x;
    x = y;
    y = temp;
}

换句话说,我第一次读取文件时,存储的数字将代表我的数组的大小。第二次阅读文件时,我想跳过第一个数字并继续下一个数字。读取文件时,如何跳过第一个数字?任何帮助将不胜感激。

这是我想阅读的文件中的信息:

9

8

4

7

2

9

5

6

1

3

【问题讨论】:

    标签: sorting c++11 io


    【解决方案1】:

    它可以读取一个文件,读取一个大小,然后关闭它,然后再次打开它,跳过大小,然后读取内容。但这是一件很奇怪的事情。大多数人会打开文件,读取大小,为数据分配空间,读取数据,然后关闭文件。

    在 C 中,在读取文件数据块之前知道它的大小通常很方便。在 C++ 中,它远没那么重要,因为 std::vector 类为您扩展,在幕后完成所有繁琐的内存重新分配。

    如果一切正常,C++ 中的提取和插入运算符 > 可以说比 C 版本更容易使用。当他们出错时,就更难理解出了什么问题。一般来说,我建议新手使用 fopen 和 fgets() 逐行读取文件。这样您就不会因丢失或跳过的字符而感到困惑。函数 sscanf() 然后会将 ascii 字符串转换为数字,但您也可以在调试时打印出该行以了解发生了什么。

    【讨论】:

    • 关于如何分配空间然后读取数据的任何提示?我正在考虑创建另一个指针变量,但我认为这不会非常有效。非常感谢您的建议。谢谢!
    【解决方案2】:

    更建议使用 C++ 语言和标准库的所有潜力,毕竟您使用的是 C++ 而不是 C 语言。有很多方法可以做你想做的事。取决于存储在文件中的数据的格式。但在所有情况下,标准库都为您提供了大量的功能和实用程序,无需重新发明轮子并专注于您的实际问题。就我而言,我为这个特定的 I/O 案例提出了这个解决方案。最后,我强烈的建议是:转到文档并尝试找到可以使您的工作更简单的东西

    http://www.cplusplus.com/reference/

    http://en.cppreference.com/w/cpp

    #include <bits/stdc++.h>
    using namespace std;
    
    vector<int> read_data( const string file_name){
    
      ifstream inf(file_name);
    
      istream_iterator<int> eoi, input(inf);
    
      vector<int> arr(*input++);//take the first integer, the size, and advance the iterator
    
      copy(input, eoi, arr.begin());
    
      return arr;
    }
    
    void bsort( vector<int>& data ){
      //sort data like you want
      sort( data.begin(), data.end());
    }
    
    void write_to_console( vector<int>& data){
      //do your stuff
      copy(data.begin(), data.end(), ostream_iterator<int>(cout," "));
    }
    
    int main(void) {
      string file_name = "data.txt";
    
      auto data = read_data( file_name );
    
      bsort( data );
      write_to_console( data );
    
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 2017-04-24
      • 1970-01-01
      • 2018-11-04
      相关资源
      最近更新 更多