在C ++中读取.dat文件 [英] read a .dat file in c++

查看:342
本文介绍了在C ++中读取.dat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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;  

我正在使用Visual Studio编译此代码.我很确定指针正在进入数据文件,但是我不知道由于什么原因而无法读取数据.

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.

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

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

推荐答案

您的代码在我的系统上有效".

Your code "works" on my system.

以下编译(不带"using namespace std;")
为了方便起见,我更改了文件名. 我在代码的相同工作目录中创建了"t391.dat"文件,并放入10行,每行1个值,即1..9,0.

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.

#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!

输入回声是个好主意.

作为一个实验,我暂时将"t391.dat"文件重命名为其他名称.代码运行完毕并打印了一个0,这不是文件中的第一个值.也许这表明未找到您的文件,我想不会.为了确认,我恢复了文件,然后再次执行上述工作".

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 not the 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.

代码中缺少项目:

  • 错误检查-file.open()

  • error check - file.open()

读取文件末尾的循环

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

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

file.close-可能不需要

file.close - possibly not needed

如果您仍在解决此问题,我将为您提供代码的最低扩展版本,以解决这些问题.让我知道.

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

这篇关于在C ++中读取.dat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆