c++学习之旅

标题:c++入门篇

今天学习C++上机题,小白分享一下感受。题目是:写一个程序,他读入一个源文件,并写出其中用#include包含的所有文件的名字。采用缩排形式 显示出被包含的文件里用#include包含的文件。简单来说就是在一个主文件中读出file1中的#include“file2”然后显示file2文件中的内容。

代码也相对较简单如下:
1.主文件:

#include<iostream>
#include<fstream>
#include<cstring>
#include<sstream>
using namespace std;

void Read(string s){
	//s = "4.000.cpp";//file1
	//cout <<s;
	const char *filename = s.c_str();
    //cout << filename<<endl;
	char *content;
	long size;
	ifstream file(filename, ios::in|ios::ate);
	size = file.tellg();
	//cout <<size<<endl;
	file.seekg(0, ios::beg);
	content = new char [size];
	//cout << "as"<<endl;
	if (file.is_open()){
		//cout << "asd"<<endl;
		file.read(content,size);
	}
	file.close();
	cout << content ;
	delete[] content;
    //return 0;
}

int main(){
	
	//ofstream file("3.cpp");
    //cout << file;
    const char * filename = "4.000.cpp";
    string strs;
    string str;
    int count=0;
    long size;
    //in 文件以输入方式打开(只读)ate 打开文件后指针转到文件末尾 
    ifstream file(filename, ios::in|ios::ate);
    size = file.tellg() ;//返回文件结尾所在位置(读取文件长度 ) 
    //cout << size;
    file.seekg(0, ios::beg);//指针返回文件开头 
    
    if (file.is_open())
		while(!file.eof())
		{//eof 到达文件末尾返回TRUE 
			getline(file ,str);
			int i =str.find("#include"); 
			//cout <<i<<endl;
			if(i>=0){
				string str1 ;
				for(int j = i+9;j<str.find_last_of('"');j++){
					str1+= str[j] ;
					strs = str1;
					
				
				}
				//cout << str1;
				Read(str1);	
				
			}
			
    		//cout << str <<endl;
		} 
	//cout << *strs[0] <<endl;
			
    	
    file.close();

    return 0;
	
}

file1

#include"4001.cpp"//file2
using namespace std;

int main(){
	
	print(1);
}

为了简单,file只有一个#include

接下来就是file2的代码:

#include<iostream>
#include<sstream>
#include<fstream>
#include<typeinfo>

using namespace std;

void print(int i){
	
	cout << i <<endl;
}

运行主代码时,运行结果应该是file2的内容

关于文件的操作,没有什么坑,网上也有很多介绍,这里不再陈述,我想说的是在主函数的Read(string s)中

void Read(string s){
	//s = "4.000.cpp";//file1
	//cout <<s;
	const char *filename = s.c_str();
    //cout << filename<<endl;
	char *content;
	long size;
	ifstream file(filename, ios::in|ios::ate);
	size = file.tellg();
	//cout <<size<<endl;
	file.seekg(0, ios::beg);
	content = new char [size];
	//cout << "as"<<endl;
	if (file.is_open()){
		//cout << "asd"<<endl;
		file.read(content,size);
	}
	file.close();
	cout << content ;
	delete[] content;
    //return 0;
}
```由于ifstream file第一个参数接收类型为char*,故牵涉到string转换为char*的问题,
刚开始采用流转换,未发生报错,但程序一直运行不了,原因在于开始时定义为char *filename;
而C++中是在const内存中给其分配空间,所以之后不能给其赋值,故一开始就应该定义为
const char*,然后用const char *filename = s.c_str();可以直接实现char*与string之间的转换,非常方便。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三喂树屋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值