【问题标题】:C++ file handlingC++ 文件处理
【发布时间】:2015-05-11 18:48:08
【问题描述】:

我有两个文本文件“source1”和“source2”分别包含整数3 4 1 2 562 45 34 23 45

在屏幕上显示它们。

即使没有给出任何错误,但我确信这些数组没有从文件 source1 和 source2 中获取正确的数据。

输出应该是文件中的整数,但这不是我所期望的。

我猜我的 READ 和 WRITE 有问题。

 #include<stdio.h>
 #include<fstream>
 #include<iostream>
 using namespace std;


 int main()
{
     fstream file1;
     fstream file2;
     file1.open("source1.txt");
     file2.open("source2.txt");
     int source1[20];
     int source2[20];
     file1.read((char*)&source1,sizeof(source1));
     cout<<source1<<"\n";
     file2.read((char*)&source2,sizeof(source2));
     cout<<source2<<"\n";
}

【问题讨论】:

  • 尝试打开您的文件,然后以int data; file1&gt;&gt;data的身份读取数据并将其放入以file结尾的循环中。
  • 我想只读写@AbhinavGauniyal

标签: c++ file-handling


【解决方案1】:

以下是我在您的程序中发现的一些问题。

读取数组

您的代码读取 20 个整数,无论​​是否有 20 个。如果文件包含 4 个二进制整数,如何处理错误?

您的文件以.txt 扩展名结尾,因此我假设数据是人类可读(文本)格式。 read 方法不会将“123”转换为数字123read 方法将按原样保存数据的镜像

数组和cout

C++ 编程语言的cout 没有打印整数数组的功能。您必须使用循环来打印它们。此外,您应该在整数之间使用分隔符,例如制表符、换行符、逗号或空格。

二进制和文本编写

如果要编写数字的内部表示,请使用ostream::write。如果您想使用格式化或文本表示,请使用operator&gt;&gt;
如果整数超过一个字节宽,您将需要知道平台是先输出最高字节(Big Endian)还是最后输出(Little Endian)。当您读回这些值时,这会产生很大的不同。

使用向量而不是数组

向量更容易传递,它们负责动态增长。数组根据定义是固定容量的,传递时需要 3 个参数:数组、容量和数组中项目的数量。使用std::vector,只需要传递向量即可,因为所有其他信息都可以通过调用vector方法获得,例如vector::size()

【讨论】:

    【解决方案2】:

    在 C 或 C++ 中打开文件以写入二进制数据时,您必须以二进制模式打开它们。使用:

     file1.open("source1.txt", ios::in | ios::binary);
     file2.open("source2.txt", ios::out | ios::binary);
    

    或类似的标志。

    【讨论】:

    • 我试过了,但仍然没有任何反应。正在生成一些随机数字。
    • 您确定target 在您完成排序后包含正确的数据吗?您的问题尚不清楚问题是文件写入还是您应该写入的实际数据。
    • 我猜 OP 有 txt 文件:O "integers 3 4 1 2 56 and 2 45 34 23 45 "其中包含整数
    • @PaulMcKenzie 我正在将两个源文件的数据合并到目标文件中。问题是我没有得到正确的输出。
    • @Tilakraj 再说一遍,是数组包含错误数据的问题,还是文件写入的问题?你还没有澄清这一点。
    【解决方案3】:

    这里是描述你想要达到的目标的工作代码:

    #include <string>
    #include <vector>
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    
    bool readIntsFromFile( const std::string& strFilename, std::vector<int>& v ) {
        std::fstream inFile;
        inFile.open( strFilename.c_str(), std::ios_base::in );
        if ( !inFile.is_open() ) {
            return false;
        } else {
            int val;
            while ( !inFile.eof() ) {
                inFile >> val;
                v.push_back( val );
            }
        }
        inFile.close();
    
        return true;
    }
    
    void sortVectors( const std::vector<int>& v1, const std::vector<int>& v2, std::vector<int>& out ) {
        unsigned index = 0;
        for ( ; index < v1.size(); ++index ) {
            out.push_back( v1[index] );
        }
    
        index = 0;
        for ( ; index < v2.size(); ++index ) {
            out.push_back( v2[index] );
        }
    
        std::sort( out.begin(), out.end() );
    }
    
    int main() {
        const std::string strFile1( "source1.txt" );
        const std::string strFile2( "source2.txt" );
        const std::string strOutput( "output.txt" );
    
        std::vector<int> a;
        std::vector<int> b;
        std::vector<int> res;
    
        if ( !readIntsFromFile( strFile1, a ) ) {
            return -1;
        }
    
        if ( !readIntsFromFile( strFile2, b ) ) {
            return -1;
        }
    
        sortVectors( a, b, res );
    
        unsigned index = 0;
        for ( ; index < res.size(); ++index ) {
            std::cout << res[index] << " ";
        }
        std::cout << std::endl;
    
        std::fstream out;
        out.open( strOutput.c_str(), std::ios_base::out );
        if ( !out.is_open() ) {
            return -1;
        } else {
            index = 0;
            for ( ; index < res.size(); ++index ) {
                out << res[index] << " ";
            }
        }
        out.close();
    
        return 0; 
    }
    

    只需确保您的 source1 和 source2 文本文件位于同一目录中 作为您生成的可执行文件。

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多