首页 > 其他 > 详细

每次从文件 中读取一行数据并以指定格式输出

时间:2020-04-11 17:32:29      阅读:64      评论:0      收藏:0      [点我收藏+]

题目

每次从文件 input.txt 中读取一行数据,将其以以下格式输出,如“michyang;25;(215021)65214795;13405054444”,输出格式为“姓名|年龄|电话|邮编|手机号”。

考点

  1. 从文件中读取数据(成员函数getline
  2. 分割处理数据

代码

#include<iostream>
#include<fstream>
#include<string>
#pragma warning(disable:4996)
using namespace std;

const int MaxSize = 1000;

int main() {
	ifstream inf("inputFile.txt");
	char test[MaxSize] = {0, };
	string res[5];
	inf.getline(test, MaxSize);	//非成员函数可以传输到string中,成员函数getline只能到字符数组中
	//第一个参数是传输的字符数组,第二个是最多传输的size
	char *p;
	p = strtok(test, ";");		//第一个参数注意是字符数组
	for (int i = 0; p != NULL; i++) {
		res[i] = p;
		p = strtok(NULL, ";()");
	}
	
	cout << res[0] << "|" << res[1] << "|" << res[3] << "|" << res[2] << "|" << res[4] << endl;

}

每次从文件 中读取一行数据并以指定格式输出

原文:https://www.cnblogs.com/Za-Ya-Hoo/p/12680534.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!