如何在不显示十六进制的情况下读取dat文件中的文本?C++
How to read text from a dat file without it showing hexadecimals? C++
我正在编写一个程序,要求用户输入用户名和密码,然后将其存储在dat file
中。然后它应该输出用户名和密码,但它只给我十六进制,比如0x9ffd18
。这里的代码是
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open ("password.dat");
string username;
string password;
cout << "This is being saved to a file" << endl;
cout << "Please enter your username" << endl;
getline (cin, username);
myfile << username;
cout << "Please enter your password" << endl;
getline (cin, password);
myfile << password;
cout << myfile << endl;
}
cout << myfile << endl;
不会输出您在文件中写入的内容,而是myfile
地址的void*
解释,默认情况下打印为十六进制值。
您需要关闭文件(或seekg()
到起始位置),再次打开它,并在写入值时读取值。
我的解决问题的建议:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open ("password.dat", ios::in | ios::out | ios::trunc);
string username;
string password;
cout << "This is being saved to a file" << endl;
cout << "Please enter your username" << endl;
getline (cin, username);
myfile << username;
cout << "Please enter your password" << endl;
getline (cin, password);
myfile << password;
char data[100];
myfile.seekg(0,ios::beg);
myfile >> data;
cout << "Output: " << endl;
cout << data << endl;
myfile.close();
}
这里有几个问题。一种是简单的行为,倒带文件并读回内容。另一个(可能更重要,至少从长远来看)是以一种可以明确读回的方式编写文件。
现在的问题是,在编写用户名和密码时,没有定义用户名和密码的结尾和开头。最明显的候选者将是一条新线路。您使用getline
读取两者,它在新行处停止读取,因此我们知道两者都不能包含新行。
除此之外,我会选择#include <string>
,因为您使用的是std::string
,并去掉using namespace std;
和endl
(几乎总是错误)。结果可能看起来像这样:
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::fstream myfile("password.dat", std::ios::in | std::ios::out | std::ios::trunc);
std::string username;
std::string password;
std::cout << "Please enter your username: ";
std::getline(std::cin, username);
myfile << username << 'n';
std::cout << "nPlease enter your password: ";
std::getline(std::cin, password);
myfile << password;
myfile.seekg(0);
std::string read_back_user_name;
std::string read_back_password;
std::getline(myfile, read_back_user_name);
std::getline(myfile, read_back_password);
std::cout << "The user name is: " << read_back_user_name << "n";
std::cout << "The password is: " << read_back_password << "n";
}
如果你真的想把文件的全部内容复制到cout,你可以这样做:
myfile.seekg(0);
cout << myfile.rdbuf();
您打印的不是输入到文件中的数据,而是转换为地址的std::fstream
对象。要解决此问题,请使用方法rdbuf()
。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open ("password.dat"); //open for input
string username;
string password;
cout << "This is being saved to a file" << endl;
getline (cin, username);
myfile << username; //input first string
cout << "Please enter your password" << endl;
getline (cin, password);
myfile << password; //input second string
myfile.close(); //close so data is sent from buffer
myfile.open("password.dat"); //open for output
cout << "Your file: " << myfile.rdbuf() << endl; //print all characterf from file
myfile.close();
}
相关文章:
- 文件中.dat Dlib 选项
- 在 c++ 中读取和写入.dat文件
- 在 c++ 中错误地读取.dat文件
- 使用ifstream从DAT文件加载数据
- 从函数中的dat文件创建一个2维数组
- (C++) 如何从要存储在结构数组中的二进制文件中读取.dat信息?
- 无法读取一个.dat文件以创建第二个.dat文件
- 读取.DAT文件并创建一个数组C
- 无法读取 Ubuntu "Segmentation fault (core dumped)"中的.dat或.bin文件
- 读取和写入.dat文件
- 如何返回文件中项目的索引.dat
- 使用 std::write 和 std::read C++ 读取包含 std::string 到.dat文件的结构
- C++:如何将数据输出到多个.dat文件
- 一次读取两个字节的.dat文件
- C++ 如何从.dat文件构建字符串的 2D 矩阵?5 列 x 行
- 为什么我的程序写入.dat文件中的同一行
- 正在从.dat文件中读取2个字节
- 创建.dat文件 (fstream) c++
- 正在从dat文件读取到数组中
- C++ 读取文件.dat中保存的对象