C++文件读写 实现文件每行数据齐长输出

来源:互联网 发布:java web开发实战入门 编辑:程序博客网 时间:2024/04/28 04:40
#include<iostream>#include<fstream>#include<cstring>using namespace std;int main(int argc ,char *argv[]){cout << "input parameters numbers :" <<  argc << endl;if(argv > 0){/*for(auto s : argv){传入参数数组不能使用foreachcout << "Input parameter:" << s << endl;}*/for(int i = 0;i < argc ;i++){cout << "Input parameter: " << argv[i] << endl;}if(argc >= 2 && (strcmp(argv[1],"/help") == 0 || strcmp(argv[1],"--help") == 0 || strcmp(argv[1],"/?") == 0)){cout << "\t--filename  \t \'filename\'\t:-f\tabbr" << endl;cout << "\t--filename \t \'filepath\'\t:-f\tabbr" << endl;}}if(argc >=2 && (strcmp(argv[1],"--filename") == 0 || strcmp(argv[1],"-f") == 0)){if(argc >= 3){int maxLength = 0;char *filename = argv[2];cout << "filename:\t" << filename << endl;ifstream in(filename);if(!in.is_open()){cout << "Not found file!Please input correct filename!" << endl;       return 0;}char str[100];int length;while(!in.eof()){in >> str;//会忽略回车的length = strlen(str);if(maxLength < length)maxLength = length;cout << str << "\tsize:" << length << endl;//所以加个endl}cout << "maxLength :" << maxLength << endl;                        in.clear();//最好还是加上吧                       in.seekg(0,ios::beg);//重回首cursorofstream out(strcat(filename,"__output.txt"));//因为filename长度未知,有隐藏风险!while(!in.eof()){in >> str;length = strlen(str);//int rest = maxLength + 1 - length;for(int i = length;i < maxLength + 1; i++){str[i] = ' ';}str[maxLength+1] = '\0';cout << str << "|size: "  << strlen(str) << endl;out << str << endl;}in.close();out.close();}}return 0;}
C:\Users\jackz\Desktop>g++ outputColumnSpaceTxt.cppC:\Users\jackz\Desktop>a -f columns.txtinput parameters numbers :3Input parameter: aInput parameter: -fInput parameter: columns.txtfilename:       columns.txtuserid  size:6u_version       size:9loginid size:7lastname        size:8firstname       size:9mi      size:2title   size:5phone   size:5deptid  size:6managerid       size:9hiredate        size:8termdate        size:8levelid size:7def_loc size:7term_id size:7last_login      size:10classid size:7struid1 size:7struid2 size:7struid3 size:7struid4 size:7struid5 size:7struid6 size:7struid7 size:7struid8 size:7struid9 size:7struid10        size:8struid11        size:8struid12        size:8struid13        size:8struid14        size:8struid15        size:8company_no      size:10tc_exempt       size:9cost_center     size:11user_loc        size:8payrollname     size:11available_status        size:16absence_start_date      size:18first_day_back_date     size:19blackout        size:8nickname        size:8job_code        size:8global_id       size:9support_company size:15fusion_id       size:9你好    size:4maxLength :19userid              |size: 20u_version           |size: 20loginid             |size: 20lastname            |size: 20firstname           |size: 20mi                  |size: 20title               |size: 20phone               |size: 20deptid              |size: 20managerid           |size: 20hiredate            |size: 20termdate            |size: 20levelid             |size: 20def_loc             |size: 20term_id             |size: 20last_login          |size: 20classid             |size: 20struid1             |size: 20struid2             |size: 20struid3             |size: 20struid4             |size: 20struid5             |size: 20struid6             |size: 20struid7             |size: 20struid8             |size: 20struid9             |size: 20struid10            |size: 20struid11            |size: 20struid12            |size: 20struid13            |size: 20struid14            |size: 20struid15            |size: 20company_no          |size: 20tc_exempt           |size: 20cost_center         |size: 20user_loc            |size: 20payrollname         |size: 20available_status    |size: 20absence_start_date  |size: 20first_day_back_date |size: 20blackout            |size: 20nickname            |size: 20job_code            |size: 20global_id           |size: 20support_company     |size: 20fusion_id           |size: 20你好                |size: 20


0 0