处理C++源代码的程序(3)
2016-06-16 09:28
295 查看
问题及描述;
运行结果;
/* 02. *Copyright (c) 2016,烟台大学计算机学院 03. *All rights reserved. 04. *文件名称;test.cpp 05. *作者;邱凯 06. *完成日期;2016年6月16号 07. *版本号;v6.0 08. *问题描述; 处理C++源代码的程序(3) 09. *输入描述; 输入答案 10. *输出描述; 输出答案 11.*/ #include <fstream> #include<iostream> #include<cstring> #include<cstdlib> using namespace std; void outprogram(const char *filename); int main( ) { char line[256]; int m,n; //将文件中的数据读入到字符数组中 ifstream sourceFile("source.cpp",ios::in); //以输入的方式打开文件 if(!sourceFile) //测试是否成功打开 { cerr<<"source code read error!"<<endl; exit(1); } ofstream outFile("newsource.cpp",ios::out); //以输出的方式打开文件 if(!outFile) //测试是否成功打开 { cerr<<"new source code write error!"<<endl; exit(1); } cout<<"您要将第m行开始的n行代码作为注释,请输入m和n:"; cin>>m>>n; int n1=0; while(!sourceFile.eof()) { sourceFile.getline(line,255,'\n'); n1++; if(n1>=m&&n1<m+n) outFile.put('/').put('/'); outFile.write(line,strlen(line)); outFile.write("\n",1); } outFile.close(); sourceFile.close(); cout<<"经过处理后的源程序是:"<<endl; outprogram("newsource.cpp"); return 0; } void outprogram(const char *filename) { char line[256]; int n = 1; ifstream inFile(filename, ios::in); //以输入的方式打开文件 if(!inFile) //测试是否成功打开 { cerr<<"file open error!"<<endl; exit(1); } while (!inFile.eof()) { inFile.getline(line,255,'\n'); cout<<n<<'\t'<<line<<endl; n++; } inFile.close(); return; }
运行结果;
相关文章推荐
- 处理C++源代码的程序(2)
- 处理C++源代码的程序
- c++类与对象,构造函数
- 浅析过滤敏感词过滤算法(C++)
- 文件——字节统计
- 如何在非托管C++中调用托管C#中的回调函数
- C++.NET编程体验
- C# 调用 C++
- C++ const总结
- 小玩文件3
- 哈夫曼算法(haffman)实现压缩和解压缩-C语言实现
- MFC多线程的创建
- Qt/C++ qss一些记录
- 第9周课后实践1-①
- C++关于指针----参考图
- 求最大公约数
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- c语言中strtok的使用
- C++中map操作
- *leetcode #99 in cpp