如何在 .txt 文件中对数值进行流式处理,并在同一程序中通过 ifstream 读取相同的数据
How to ofstream a numeric value in a .txt file and read same data through ifstream in same program
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string mobile;
int N[10];
ofstream file;
file.open("my.txt");
cout<<"enter mobile ";
getline(cin, mobile);
file<<mobile<<endl;
cout<<"enter number ";
cin>>N[10];
file<<N;
file.close();
int a;
cin.ignore();
cout<<"press 1 to know details ";
cin>>a;
if(a==1)
{
ifstream file1;
file1.open("my.txt");
string str;
int num;
file1>>str;
file1>>num;
cout<<"company "<<str<<endl<<"number "<<num;
file1.close();
}
return 0;
}
字符串值被完美存储和读取,但输入的整数值以 0x61fdf0 的形式存储,当它应该再次打印时,它总是打印 0。我正在尝试存储手机号码。
我正在代码块中编码。
#include <iostream>
#include <fstream>
using namespace std;
const int NUM = 10;
int main()
{
string mobile;
int n[NUM];
ofstream file;
file.open("my.txt");
cout << "enter mobile: ";
getline(cin, mobile);
file << mobile << endl;
cout << "enter number ";
file << n[0];
file.close();
int a;
cin.ignore();
cout << "press 1 to know details ";
cin >> a;
if(a==1)
{
ifstream file1;
file1.open("my.txt");
string str;
int num;
(file1, str);
file1 >> num;
cout << "company: " << str <<"\nnumber: " << num << endl;
file1.close();
}
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string mobile;
int N[10];
ofstream file;
file.open("my.txt");
cout<<"enter mobile ";
getline(cin, mobile);
file<<mobile<<endl;
cout<<"enter number ";
cin>>N[10];
file<<N;
file.close();
int a;
cin.ignore();
cout<<"press 1 to know details ";
cin>>a;
if(a==1)
{
ifstream file1;
file1.open("my.txt");
string str;
int num;
file1>>str;
file1>>num;
cout<<"company "<<str<<endl<<"number "<<num;
file1.close();
}
return 0;
}
字符串值被完美存储和读取,但输入的整数值以 0x61fdf0 的形式存储,当它应该再次打印时,它总是打印 0。我正在尝试存储手机号码。
我正在代码块中编码。
#include <iostream>
#include <fstream>
using namespace std;
const int NUM = 10;
int main()
{
string mobile;
int n[NUM];
ofstream file;
file.open("my.txt");
cout << "enter mobile: ";
getline(cin, mobile);
file << mobile << endl;
cout << "enter number ";
file << n[0];
file.close();
int a;
cin.ignore();
cout << "press 1 to know details ";
cin >> a;
if(a==1)
{
ifstream file1;
file1.open("my.txt");
string str;
int num;
(file1, str);
file1 >> num;
cout << "company: " << str <<"\nnumber: " << num << endl;
file1.close();
}
return 0;
}