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

题目

每次从文件 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;

}
posted @ 2020-04-11 16:08  游芒。  阅读(476)  评论(0)    收藏  举报