[英]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;
}
string value is stored and read perfectly but the integer value entered is stored in the form of 0x61fdf0 and when it is supposed to again print, it always prints 0. I am trying to store a mobile number.字符串值被完美地存储和读取,但输入的 integer 值以 0x61fdf0 的形式存储,当它应该再次打印时,它总是打印 0。我正在尝试存储手机号码。
I am coding in code blocks.我在代码块中编码。
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.