我正在做 ATM 练习。在我的数据库或“.txt”文件中,我有基本信息。
0123456789 John Doe 0123 9000
我在互联网上唯一能找到的,用 C++ 读取文件的方法是使用 getline();
。它读取文件并将其存储在变量字符串中。我有必须使用的值,如整数和 float 。
- 如何将数据库中的值存储为不同的数据类型而不是 就在一根绳子里?
- 或者有没有一种方法可以切割字符串并将不同的值存储在 float 或整数中?
- 还有其他阅读方式吗?我只是 C++ 编程的新手。
最佳答案
在C++中,可以通过“文件流”读取文件:
#include <iostream>
#include <string>
using namespace std;
int main(){
string filename="test.txt";
ifstream fin(filename);
while (!fin.fail()){//read until end of file
int a,d,e;
string b,c;
fin>>a>>b>>c>>d>>e;//That's a line
cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" \n";//show it on command line
}
fin.close();
return 0;
}
关于c++ - 读取数据库/文件中的不同数据类型(C++中的文件处理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32899961/