getline怎么用+小例程

getline函数

getline不是C库函数,而是C++库函数。它会生成一个包含一串从输入流读入的字符的字符串,直到以下情况发生会导致生成的此字符串结束。1)到文件结束,2)遇到函数的定界符,3)输入达到最大限度

应用范围:

用于读取一行字符直到换行符,包括换行符,并存到string变量中;直到出现一下情况时就可以结束:

读入了文件结束标志

读到一个新行

达到字符串的最大长度

如果getline没有读入字符,将返回false,可用于判断文件是否结束;

 

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
	string buff;
	ifstream infile;//ifstream读操作的文件类(由istream引申而来)
	ofstream outfile;//写操作(输出)的文件类 (由ostream引申而来)
	cout<<"Input file name: "<<endl;
	cin>>buff;
	infile.open(buff.c_str());//打开文件名存在buff.c_str()变量中的文件

	if(!infile)//检查打开文件是否成功
		cout<<"error"<<buff<<endl;//如果打开失败,则输出error

	cout<<"Input outfile name: "<<endl;
	cin>>buff;
	outfile.open(buff.c_str());

	while(getline(infile, buff))//读取一行字符直到换行符
		outfile<<buff<<endl;

	infile.close();
	outfile.close();
	return 0;

}

DOS界面显示:第一个红框中的是我输入的读取文件(已经存在在我的电脑里,txt中有一句话“we are friends!”)

 


执行程序后结果显示:

 

 文件“1.txt”和“2.txt”都是已经存在在我电脑中的文件(frinends中的“s”掉了,不好意思尴尬)!

 

#include<iostream>
#include<fstream>
#include<string>

using namespace std;
int main()
{
    ifstream fin("d:\\Projects_Face_Detection\\Datasets\\afw\\Path_Images_1.txt"); 
    string s; 
    while( getline(fin,s) )
    {   
        cout << "Read from file: " << s << endl;
    }
	getchar();
	return 0;
}

上面这段代码主要是读取一个“*.txt”的文档; (注:txt文档中不要出现带有空行)

 

输出所有文档中的内容:

以下是使用VC++解析EIP中的EDS文件的示例代码,包括读取EDS文件,解析EDS文件并输出其内容的过程。请注意,这只是一个简单的示例代码,实际应用中可能需要更多的错误处理和逻辑判断。 ```c++ #include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; // 定义EIP-2470中的EDS数据结构 struct EIP_EDS_Item { string name; // 名称 string type; // 类型 string defaultVal; // 默认值 string desc; // 描述 }; // 定义EDS文件解析器 class EIP_EDS_Parser { public: // 解析EDS文件 vector<EIP_EDS_Item> parse(string filename) { vector<EIP_EDS_Item> res; ifstream ifs(filename); if (!ifs) { cerr << "Can't open file " << filename << endl; return res; } string line; EIP_EDS_Item item; while (getline(ifs, line)) { // 去除空格和注释 line = trim(line); if (line.empty() || line[0] == '#') { continue; } // 解析EDS项 if (line[0] == '-') { // 将上一个EDS项添加到结果中 if (!item.name.empty()) { res.push_back(item); } // 初始化新的EDS项 item.name = ""; item.type = ""; item.defaultVal = ""; item.desc = ""; // 解析EDS项的名称 int pos = line.find(":"); if (pos != string::npos) { item.name = trim(line.substr(1, pos - 1)); line = trim(line.substr(pos + 1)); } // 解析EDS项的类型和默认值 pos = line.find("="); if (pos != string::npos) { item.type = trim(line.substr(0, pos)); item.defaultVal = trim(line.substr(pos + 1)); } else { item.type = trim(line); } } // 解析EDS项的描述 else { item.desc += line + "\n"; } } // 添加最后一个EDS项 if (!item.name.empty()) { res.push_back(item); } return res; } private: // 去除字符串两端的空格 string trim(string s) { int pos = s.find_first_not_of(" \t\r\n"); if (pos == string::npos) { return ""; } s.erase(0, pos); pos = s.find_last_not_of(" \t\r\n"); if (pos != string::npos) { s.erase(pos + 1); } return s; } }; int main() { EIP_EDS_Parser parser; vector<EIP_EDS_Item> items = parser.parse("test.eds"); for (const auto& item : items) { cout << "Name: " << item.name << endl; cout << "Type: " << item.type << endl; cout << "Default Value: " << item.defaultVal << endl; cout << "Description: " << item.desc << endl; cout << "-----------------" << endl; } return 0; } ``` 在上面的示例代码中,我们首先定义了一个EIP_EDS_Item结构体,用于表示一个EDS项的各个属性。然后,我们定义了一个EIP_EDS_Parser类,用于解析EDS文件。EIP_EDS_Parser类中的parse方法接受一个EDS文件名作为参数,并返回一个EIP_EDS_Item类型的vector,其中包含了EDS文件中的所有EDS项。 在parse方法中,我们使用ifstream读取EDS文件,然后逐行解析文件内容。对于每一个EDS项,我们使用EIP_EDS_Item结构体来存储其名称、类型、默认值和描述。具体来说,我们首先解析EDS项的名称,然后根据等号判断是否有默认值,最后解析EDS项的描述。 最后,我们在主函数中调用EIP_EDS_Parser的parse方法,并遍历所返回的EDS项vector,输出其各个属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

猫猫与橙子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值