标题:从键盘输入一个字符串、一个整数和一个实数,将其写入文件f1.txt中,再用读 ...
只看楼主
jojo170
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-5-17
结帖率:0
已结贴  问题点数:20 回复次数:1 
从键盘输入一个字符串、一个整数和一个实数,将其写入文件f1.txt中,再用读字符方式从此文本文件中逐个读出并显示在屏幕上
从键盘输入一个字符串、一个整数和一个实数,将其写入文件f1.txt中,再用读字符方式从此文本文件中逐个读出并显示在屏幕上
搜索更多相关主题的帖子: 屏幕 读出 文本文件 显示 txt 
2020-05-17 17:38
吕孟伟
Rank: 8Rank: 8
等 级:贵宾
威 望:27
帖 子:200
专家分:870
注 册:2018-10-4
得分:20 
程序代码:
#include<iostream>
#include<fstream>
using namespace std;

int main(void)
{
    char buf[1024];
    int value;
    double number;
    ofstream ofs;
    ofs.open("test.txt");
    if(ofs.is_open()){
        cout << "opening file success!" << endl;
    }
    cout << "Enter your string :";
    cin.getline(buf, 100);
    ofs << buf << endl;
    cout << "Enter your int :";
    cin >> value;
    ofs << value << endl;
    cout << "Enter your double :";
    cin >> number;
    ofs << number << endl;

    ifstream ifs;
    ifs.open("test.txt", ios::in);
    if(ifs.is_open()){
        cout << "opening file success!" << endl;
    }
    cout << "Reading the file:" << endl;
    
    ifs >> buf;  
    cout << buf << endl;
    ifs >> value;
    cout << value <<  endl;
    ifs >> number;
    cout << number << endl;

    return 0;
}


[此贴子已经被作者于2020-5-18 08:14编辑过]


借用一下侯捷的话:勿在浮沙筑高台。
2020-05-18 08:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-501651-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.218317 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved