在 C++ 中读取 .dat 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35820879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:33:33  来源:igfitidea点击:

read a .dat file in c++

c++file

提问by venkatesh gadde

I am unable to read '.dat' file. I have tired all the possible ways and tired googling it but I could not find the solution. All it gives me is a null value for integer and a junk value for a string variable or char. This what I have written

我无法读取“.dat”文件。我已经厌倦了所有可能的方法并厌倦了谷歌搜索,但我找不到解决方案。它给我的只是整数的空值和字符串变量或字符的垃圾值。这是我写的

ifstream file;
file.open("data1.dat"); // I have also tried this way too like file.open("data1.dat", ios::binary, ios::in);
int data=0;
file >> data;
cout << data << endl;
system("pause");
return 0;  

I am using visual studio to compile this code. I am pretty sure that the pointer is entering into the data file but I don't know for what reason the data is not being read.

我正在使用 Visual Studio 来编译这段代码。我很确定指针正在进入数据文件,但我不知道数据没有被读取的原因。

The .dat file consists of integer number per line ranging from 0, so I just need to read the file and get number from each line and should find the sum of all numbers in the file. The file contains number like 5, 468, 3200, 32, etc.,. Each number is in a new line. The file can contain any number of records. this how .dat file looks when opened using a notepad editor

.dat 文件由每行从 0 开始的整数组成,所以我只需要读取文件并从每一行获取数字,然后应该找到文件中所有数字的总和。该文件包含诸如 5、468、3200、32 等数字。每个数字都在一个新行中。该文件可以包含任意数量的记录。这是使用记事本编辑器打开 .dat 文件时的外观

回答by 2785528

Your code "works" on my system.

您的代码在我的系统上“有效”。

The following compiles (without "using namespace std;")
I changed the file name for my convenience. I created the 't391.dat' file in the same working directory of the code, and put in 10 lines, with 1 value per line, 1..9,0.

下面的编译(没有“使用命名空间 std;”)
为了方便我更改了文件名。我在代码的同一工作目录中创建了 't391.dat' 文件,并放入 10 行,每行 1 个值,1..9,0。

#include <fstream>
#include <iostream>

int t391a(void)
{
   std::ifstream file;
   file.open("t391.dat"); 
   int data=0;
   file >> data;
   std::cout << data << std::endl;  // echo of input / proof it works
   //system("pause");
   file.close();
   return 0;
}

This code outputs the first value (which is all it attempts to do), so it is working!

这段代码输出第一个值(这是它尝试做的所有事情),所以它正在工作!

The echo of input is good idea.

输入的回声是个好主意。

As an experiment, I temporarily renamed the 't391.dat' file to something else. The code ran to completion and printed a single 0, which is notthe first value in the file. Perhaps this suggests your file is not being found, I won't guess. To confirm, I restored the file, and the above 'works' again.

作为一项实验,我暂时将“t391.dat”文件重命名为其他文件。代码运行完成并打印一个 0,这不是文件中的第一个值。也许这表明您的文件没有被找到,我猜。为了确认,我恢复了文件,并且上面的“工作”再次出现。

Missing items in your code:

代码中缺少项目:

  • error check - file.open()

  • a loop to read to end of file

  • error check - formatted extract (i.e. read from stream) of data item

  • file.close - possibly not needed

  • 错误检查 - file.open()

  • 读取到文件末尾的循环

  • 错误检查 - 数据项的格式化提取(即从流中读取)

  • file.close - 可能不需要

If you are still working this issue, I have a minimally extended version of your code that addresses these issues. Let me know.

如果您仍在处理这个问题,我有您的代码的最小扩展版本来解决这些问题。让我知道。

回答by venkatesh gadde

class ValueGet {
public:
   int data;
   ValueGet() {
   data = 0;
   }
};
int main()
{
    ValueGet vg;
    ifstream file;
    file.open("data1.dat", fstream::binary | fstream::out); // Opens a file in binary mode for input operations i.e., getting data from file.
    if (!file)
        cout << "File Not Found." << endl;
    else {
        file.seekg(0); // To make sure that the data is read from the starting position of the file.
        while (file.read((char *)&vg, sizeof(vg))) // Iterates through the file till the pointer reads the last line of the file.
            cout<<vg.data<<endl;
    }
    //system("pause");
    return 0;
}

output of the data in the file

输出文件中的数据

回答by mitansh1398

Here is one way which I just found

这是我刚刚发现的一种方法

     #include <bits/stdc++.h>
    using namespace std;

    int main(){
        unsigned int a;
        unsigned char c;
        ifstream file;
        file.open("ou.bin", ios::binary);
        if(!file.is_open()){
            cout<<"error";
            return 0;
        }
        for(int i=0; i<8; i++){
            file>>c;
            a = c;
            a = a<<8;
            file>>c;
            a = a+ c;
            cout<<a<<endl;
        }
        file.close();
        return 0;
   }

This for storing two bytes in a number you can store as many bytes in a number or even one.

这用于在一个数字中存储两个字节,您可以在一个数字中存储尽可能多的字节,甚至是一个。

Hope this helps.

希望这可以帮助。

回答by Java Man Tea Man

You will not be able to read .dat files and understand them in your context-- they are general formats used for storing data. Unless you know the contents of it or how they are specified, you will always get junk.

您将无法阅读 .dat 文件并在您的上下文中理解它们——它们是用于存储数据的通用格式。除非你知道它的内容或它们是如何指定的,否则你总是会得到垃圾。