确定c++中的getline()起始点
Determine a getline() starting point in C++
这是我的代码的一部分
string line;
ifstream file ("Names.txt");
int i;
for (i = 0; i < line.length(); ++i) {
if ('A' <= line[i] && line[i] <= 'Z') break;
}
string start = line.substr(i);
getline(file, start, '.');
cout << start;
我需要从第一个大写字母开始阅读一行,直到文本文件中的第一个句号。目前,它成功地从文件的开头读取到第一个周期。所以我有一个问题,确定起点I(第一个大写字母)。
我很感激你的帮助!string line; // line is empty
ifstream file ("Names.txt"); // line is still empty
int i; // still empty
for (i = 0; i < line.length(); ++i) { // still empty, line.length() == 0
有帮助吗?您需要从文件中读入一行(使用getline),然后解析该行。
应该这样做:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
ifstream file ("file.txt");
char temp;
while(file>>temp)
{
if(isupper(temp)) break;//First capital letter
}
file.seekg(-1,file.cur);//rewind one char so you can read it in the string
getline(file,line,'.');//read until the first .
cout << line << endl;
system("pause");
return 0;
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- .cpp和.h文件中的模板专用化声明
- 反向给定链表中的K节点
- 正在查找文档以获得PS4平台的C++中的设备信息
- enum是C++中的宏变量还是整数变量
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 将字符串存储在c++中的稳定内存中
- 文本文件中的单词链表
- 递归函数计算序列中的平方和(并输出过程)
- 如何从C++中的依赖类型中获得它所依赖的类型
- C++中的"inline"关键字
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 用C++中的一个变量定义一个常量
- vector.resize()中的分配错误
- 使用指针从C++中的数组中获取最大值
- arr[-1]在c++中的奇怪行为
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 将值指定给向量(2D)的向量中的某个位置
- 内置函数可查看CPP中的成员变量