确定c++中的getline()起始点

Determine a getline() starting point in C++

本文关键字:c++ 中的 getline 确定      更新时间:2023-10-16

这是我的代码的一部分

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;
}