如何计算文本文件中的哪一行是C++
How to count in which line in text file the word is C++
我有一个练习,我需要在文本文件中查找以用户输入符号开头的单词。我还需要确定该单词在哪一行并将其输出到文本不同的文本文件中。
我设法编写代码来输出以符号开头的单词并计算单词在文本中的位置,但我不知道如何计算该单词在哪一行。我还需要找到那些带有 ?
!
等符号的单词,不仅 ' '
例如,如果我想找到以 c
开头的单词,那么我的程序只会从我的代码下方的示例输入中找到“cerebral, cortex, could, create”,而不是“construct, capable, computers”。
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream input;
fstream output;
string word, line;
char startOfWord;
cout << "I wanna find words starting with symbol: \n";
cin >> startOfWord;
int lineNumber = 0;
input.open("f.txt");
output.open("f1.txt");
while (!input.eof()) {
input >> word;
lineNumber++;
if (word.length() > 40) {
continue;
}
if (word[0] == startOfWord) {
output << word << ' ' << lineNumber << '\n';
}
}
input.close();
output.close();
return 0;
}
示例输入:用户想要查找以 a
.
开头的单词
f.txt:
A Stanford University project to?construct a model
of the cerebral cortex in silicon could help scientists
gain a better understanding of the brain, in order to
create more,capable.computers and advanced
neural prosthetics.
输出:f1.txt
a 1
a 3
and 4
advanced 4
您正在逐字阅读文件,计算每个字。你需要逐行读取文件,计算每一行,然后你可以从每一行读取单词。
试试这个:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main() {
ifstream input;
ofstream output;
string word, line;
char startOfWord;
cout << "I wanna find words starting with symbol: \n";
cin >> startOfWord;
int lineNumber = 0;
input.open("f.txt");
output.open("f1.txt");
while (getline(input, line)) {
++lineNumber;
istringstream iss(line);
while (iss >> word) {
if (word.length() > 40) {
continue;
}
if (word[0] == startOfWord) {
output << word << ' ' << lineNumber << '\n';
}
}
}
input.close();
output.close();
return 0;
}
我做到了!这有效!
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
void getWordsFromLine(string word, int index, vector<string> &memory) { //funkcija kas izvelk vardus no rindam
string newWord;
for (int i = index; i < word.length(); i++) {
if (word[i] == ')' || word[i] == '(' || word[i] == '.' || word[i] == ',' || word[i] == '=' || word[i] == '?' || word[i] == '!') { //parbauda visas iespejamas pieturzimes
i++;
getWordsFromLine(word, i, memory);
break;
}
else {
newWord += word[i];
}
}
memory.push_back(newWord);
}
int main() {
int ok;
do
{
ifstream input;
ofstream output;
string word, line;
char startOfWord;
char symbol;
cout << "I wanna find words starting with symbol: \n";
cin >> startOfWord;
int lineNumber = 0;
input.open("f.txt");
output.open("f1.txt");
while (getline(input, line)) {
++lineNumber; //skaita rindas
istringstream iss(line);
while (iss >> word) {
if (word.length() > 40) {
continue;
}
vector<string> words;
getWordsFromLine(word, 0, words);
for (int i = 0; i < words.size(); i++) {
if (words[i][0] == startOfWord) { //atrod vardus ar sakuma simbolu
output << words[i] << ' ' << lineNumber << '\n';
}
}
}
}
input.close();
output.close();
cout<<"Vai turpinat (1) vai beigt (0)?"<<endl;
cin>>ok;
} while (ok==1);
return 0;
}
我有一个练习,我需要在文本文件中查找以用户输入符号开头的单词。我还需要确定该单词在哪一行并将其输出到文本不同的文本文件中。
我设法编写代码来输出以符号开头的单词并计算单词在文本中的位置,但我不知道如何计算该单词在哪一行。我还需要找到那些带有 ?
!
等符号的单词,不仅 ' '
例如,如果我想找到以 c
开头的单词,那么我的程序只会从我的代码下方的示例输入中找到“cerebral, cortex, could, create”,而不是“construct, capable, computers”。
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream input;
fstream output;
string word, line;
char startOfWord;
cout << "I wanna find words starting with symbol: \n";
cin >> startOfWord;
int lineNumber = 0;
input.open("f.txt");
output.open("f1.txt");
while (!input.eof()) {
input >> word;
lineNumber++;
if (word.length() > 40) {
continue;
}
if (word[0] == startOfWord) {
output << word << ' ' << lineNumber << '\n';
}
}
input.close();
output.close();
return 0;
}
示例输入:用户想要查找以 a
.
f.txt:
A Stanford University project to?construct a model
of the cerebral cortex in silicon could help scientists
gain a better understanding of the brain, in order to
create more,capable.computers and advanced
neural prosthetics.
输出:f1.txt
a 1
a 3
and 4
advanced 4
您正在逐字阅读文件,计算每个字。你需要逐行读取文件,计算每一行,然后你可以从每一行读取单词。
试试这个:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main() {
ifstream input;
ofstream output;
string word, line;
char startOfWord;
cout << "I wanna find words starting with symbol: \n";
cin >> startOfWord;
int lineNumber = 0;
input.open("f.txt");
output.open("f1.txt");
while (getline(input, line)) {
++lineNumber;
istringstream iss(line);
while (iss >> word) {
if (word.length() > 40) {
continue;
}
if (word[0] == startOfWord) {
output << word << ' ' << lineNumber << '\n';
}
}
}
input.close();
output.close();
return 0;
}
我做到了!这有效!
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
void getWordsFromLine(string word, int index, vector<string> &memory) { //funkcija kas izvelk vardus no rindam
string newWord;
for (int i = index; i < word.length(); i++) {
if (word[i] == ')' || word[i] == '(' || word[i] == '.' || word[i] == ',' || word[i] == '=' || word[i] == '?' || word[i] == '!') { //parbauda visas iespejamas pieturzimes
i++;
getWordsFromLine(word, i, memory);
break;
}
else {
newWord += word[i];
}
}
memory.push_back(newWord);
}
int main() {
int ok;
do
{
ifstream input;
ofstream output;
string word, line;
char startOfWord;
char symbol;
cout << "I wanna find words starting with symbol: \n";
cin >> startOfWord;
int lineNumber = 0;
input.open("f.txt");
output.open("f1.txt");
while (getline(input, line)) {
++lineNumber; //skaita rindas
istringstream iss(line);
while (iss >> word) {
if (word.length() > 40) {
continue;
}
vector<string> words;
getWordsFromLine(word, 0, words);
for (int i = 0; i < words.size(); i++) {
if (words[i][0] == startOfWord) { //atrod vardus ar sakuma simbolu
output << words[i] << ' ' << lineNumber << '\n';
}
}
}
}
input.close();
output.close();
cout<<"Vai turpinat (1) vai beigt (0)?"<<endl;
cin>>ok;
} while (ok==1);
return 0;
}