[英]How to check if a line is all whitespaces in c++?
我的任務是通讀用戶輸入的文件,然后檢查每一行以查看該行是否有有效的命令。 例如,如果文件包含以下內容:
run prog1
%
delete
% execute next command
delete myfile
copy file1 file2
print myfile
% terminate script
我的代碼的預期 output 是這樣的:
Enter the name of a file to read from:
Total lines: 13
Commented lines: 3
Valid Command lines: 5
Invalid Command lines: 0
Run commands: 1
Print commands: 1
Copy commands: 1
Delete commands: 2
我的代碼是這樣的:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
ifstream inClientFile;
string filename;
cout << "Enter the name of a file to read from:" << endl;
cin >> filename;
inClientFile.open(filename.c_str());
if( !inClientFile)
{
cerr << endl;
cerr << "File cannot be opened" << " " << filename << endl;
exit (1);
}
string line;
string commands;
int total = 0, comment = 0, valid_lines = 0, invalid_lines = 0, run_lines = 0, print_lines = 0, copy_lines = 0, delete_lines = 0;
while (getline(inClientFile,line)){
total++;
if(line.size()==0){
continue;
}
else if(line[0]=='%'){
comment++;
continue;
}
else{
int i=0;
while (i<line.size() and line[i]!=' '){
i++;
}
commands = line.substr(0,i);
if(commands == "run"){
run_lines++;
valid_lines++;
}
else if(commands == "print"){
print_lines++;
valid_lines++;
}
else if(commands == "copy"){
copy_lines++;
valid_lines++;
}
else if(commands == "delete"){
delete_lines++;
valid_lines++;
}
else if(line.size()==0){
continue;
}
else{
cout<<"\nError: Unrecognizable command in line " <<total<<endl;
invalid_lines++;
}
}
}
cout<<endl;
cout << "Total lines: " << total << endl;
cout << "Commented lines: " << comment << endl;
cout << "Valid Command lines: " << valid_lines<< endl;
cout << "Invalid Command lines: " << invalid_lines << endl;
cout << "Run commands: " << run_lines << endl;
cout << "Print commands: " << print_lines << endl;
cout << "Copy commands: " << copy_lines << endl;
cout << "Delete commands: " << delete_lines << endl;
return 0;
}
問題是我的代碼正在讀取所有空格作為無效命令的行。 所以 output 最終是:
Enter the name of a file to read from:
Error: Unrecognizable command in line 7
Error: Unrecognizable command in line 10
Error: Unrecognizable command in line 12
Total lines: 13
Commented lines: 3
Valid Command lines: 5
Invalid Command lines: 3
Run commands: 1
Print commands: 1
Copy commands: 1
Delete commands: 2
請教我如何閱讀包含所有空格的行,然后忽略該行或繼續。 我想到了這條線:
if(line.size()==0){
continue;
}
可以解決這個問題,但它不適用於兩個連續的空行,因為包含所有空格的行的值大於空行的值,即 0。請幫助我!
最簡單的做法是使用std::istringstream
和>>
來拉開這條線。 如果該行只是空格, >>
將無法找到令牌,並且 stream 將被放入失敗的 state。 測試 stream state,您會知道該行是否除了空白之外什么都沒有。
不利的是,這將讀取
run
作為run
並接受它作為有效令牌。 如果您不接受以空格開頭的行
std::isspace(static_cast<unsigned char>(line[0]));
可以用來檢測這種情況。 請參閱std::isspace
文檔中的注釋,以了解看起來很傻的演員表的解釋。
為了簡單起見,我仍然會使用istringstream
來標記該行。 它將完全標記一個命令,如
copy file1 file2
容易死。
更新代碼
#include <iostream>
#include <string>
#include <fstream>
#include <sstream> // for istringstream
using namespace std;
int main(){
ifstream inClientFile;
string filename;
cout << "Enter the name of a file to read from:" << endl;
cin >> filename;
inClientFile.open(filename.c_str());
if( !inClientFile)
{
cerr << endl;
cerr << "File cannot be opened" << " " << filename << endl;
exit (1);
}
string line;
string commands;
int total = 0,
comment = 0,
valid_lines = 0,
invalid_lines = 0,
run_lines = 0,
print_lines = 0,
copy_lines = 0,
delete_lines = 0;
while (getline(inClientFile,line)){
total++;
if(line.size()==0){
continue;
}
else if(line[0]=='%'){
comment++;
continue;
}
else{
// test std::isspace(static_cast<unsigned char>(line[0])); here if
// necessary
std::istringstream strm(line); //use stream to split up the line
if (strm >>commands) // skip all whitespace leading to first
// whitepace-delimited token. If the line is
// nothing but whitespace, the read will fail.
// downside:
// run
// is now a valid command. May need extra checks
{
if(commands == "run"){
run_lines++;
valid_lines++;
}
else if(commands == "print"){
print_lines++;
valid_lines++;
}
else if(commands == "copy"){
copy_lines++;
valid_lines++;
}
else if(commands == "delete"){
delete_lines++;
valid_lines++;
}
else if(line.size()==0){
continue;
}
else{
cout<<"\nError: Unrecognizable command in line " <<total<<endl;
invalid_lines++;
}
}
else // no token at all
{
continue; // bit of a waste now, but if someone adds more cases
// after this one... One less possible bug.
}
}
}
cout<<endl;
cout << "Total lines: " << total << endl;
cout << "Commented lines: " << comment << endl;
cout << "Valid Command lines: " << valid_lines<< endl;
cout << "Invalid Command lines: " << invalid_lines << endl;
cout << "Run commands: " << run_lines << endl;
cout << "Print commands: " << print_lines << endl;
cout << "Copy commands: " << copy_lines << endl;
cout << "Delete commands: " << delete_lines << endl;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.