简体   繁体   English

如何在 a.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;
}

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.

相关问题 我的代码不会使用 ifstream 和 ofstream 从文件中读取和显示数据 - My code will not read and display data from a file using ifstream and ofstream 如何在 c++ 的同一程序中对 2D 字符串数组进行流式处理,然后对其进行流式处理。 这是我写的代码。 我是首发 - How to ofstream and then ifstream a 2D string array in same program in c++. Here is the code which I wrote. I am a starter 写入ofstream,然后用ifstream读取不会读取整个文件 - Write to ofstream followed by read with ifstream doesn't read entire file 如何将 ifstream 和 ofstream 传递给 C++ 中的相同 function 对象? - how can i pass ifstream and ofstream to same function objects in C++? 从文件读取(ifstream)和写入文件(ofstream)-编码问题 - Read from file (ifstream) and write to file (ofstream) - problems with encoding 如何使我的 ifstream“输入”在程序中看起来像在文件中一样 - How to make my ifstream "input" look the same in program like it does in file ifstream读取和读取未返回相同数据,C ++ - ifstream read and fread not returning same data, C++ 使用ofstream将数据写入文件后更新ifstream对象 - Update ifstream object after data is written to its file using ofstream 使用 ifstream 读取.txt 并使用 ofstream 写入 new.txt 文件,但只有第一行有效 - Reading .txt using ifstream and writing to new .txt file with ofstream, but only first line works 无法使用 ifstream 读取 txt 文件 - Cannot read in a txt file using ifstream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM