1.7writing and reading files

本文通过一个C++程序示例介绍了如何使用文件流(ifstream和ofstream)进行文件的读写操作。程序创建了一个名为seq_data.txt的文件,用于存储一系列用户名和密码数据,并随后读取这些数据并显示出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// Ess1.7.cpp : 定义控制台应用程序的入口点。

//

 

#include "stdafx.h"

#include <fstream>

#include <iostream>

#include <string>

#include <sstream>

 

using namespace std; 

 

int _tmain(int argc, _TCHAR* argv[])

{

//ofstream outfile("seq_data.txt", ios_base::app);

ofstream outfile("seq_data.txt");

for( int i = 0; i < 10; i ++ ){

stringstream i_tos;

i_tos<<i;

string user_name = "wind" + i_tos.str();

string user_pwd = "wind";

 

if( !outfile){

cerr<<"oops, Unable to save session data!";

}else{

outfile << user_name << ' '

<< user_pwd << endl;

}

}

 

ifstream infile("seq_data.txt");

 

if( !infile ){

cerr<<"can't open file!";

}else{

string name = "", pass = "";

while( (infile >> name) && (infile >> pass) ){

cout<<name<<"------"<<pass<<endl;

}

}

 

 

return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值