1

这是我的代码的一部分

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(第一个大写字母)时遇到了问题。

我感谢您的帮助!!

4

2 回答 2

2
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),然后解析行。

于 2013-04-07T17:55:22.607 回答
1

像这样的东西应该工作:

#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;
}
于 2013-04-07T18:09:11.227 回答