0
파일을 읽고 줄로 출력하고 문자, 줄 및 비 공백 문자 및 단어 수를 계산하십시오. 임씨는 끝내기에 가깝지만 나는 단어의 수를 세는 방법을 생각할 수 없다. 또한 그것을 실행할 때 첫 줄을 제외한 모든 줄의 첫 글자를 자릅니다.파일 및 개수 단어 읽기
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
int line_number = 0;
char words;
int number_of_spaces = 0;
int number_of_lines = 0;
int number_of_characters = 0;
int number_of_words = 0;
int count = 0;
char character;
string word;
int n = 0;
string line;
ifstream myFile;
string file;
cout <<"Enter file name: ";
getline(cin,file);
myFile.open(file.c_str());
char output[100];
if(myFile.is_open())
{
cout << " " << endl;
while(getline (myFile, line))
{
line_number += 1;
cout << "Line: " << line_number << " ";
cout << line << endl;
number_of_characters += line.length();
for (int i = 0; i < line.length(); i++)
{
if(line[i] == ' ' || line[i] == '/t')
{
number_of_spaces++;
}
}
myFile.get(character);
if (character != ' ' || character != '/n')
{
number_of_lines += 1;
}
}
cout << " " << endl;
cout << number_of_characters << " characters, " << endl;
cout << number_of_characters - number_of_spaces << " non whitespace characters, " << endl;
cout << number_of_words << " words, " << endl;
cout << number_of_lines << " lines." << endl;
}
myFile.close();
system("Pause");
return 0;
}
'getline'은 전체 행을 사용합니다. 따라서'myFile.get()'을 호출하면 이후의 모든 라인의 첫 문자를 잃게됩니다. 또한''/ n ''이 아니라 ''\ n ''입니다. – kfsone
다음 행을 삭제할 수 있습니다 :'myFile.get (character); if (문자! = ''|| 문자! = '/ n') { number_of_lines + = 1; }' – BlackMamba