如何在不显示十六进制的情况下读取dat文件中的文本?C++

How to read text from a dat file without it showing hexadecimals? C++

本文关键字:文件 dat 文本 C++ 读取 情况下 显示 十六进制      更新时间:2023-10-16

我正在编写一个程序,要求用户输入用户名密码,然后将其存储在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();
}