テキストファイル内の異なる文字の位置を数えようとしていますが、問題があります。目的はファイルを読み込み、各大文字と小文字の最初のインスタンスの位置と、文字の位置に影響を与えるように空白がカウントされる数字の最初のインスタンス(0〜9)。私は関数でそうしていますが、壁に当たっています。 私は主にテスト/参照目的でいくつかのことをハードコードしました。 以下のコードを使用すると、 "S"のみが出力され、位置量は出力されません(文字列として出力されます)。 私は、主なエラーがファイルを間違って読み込んでいると仮定しています。私は一度にそれをすべて読んでから、それを解析して、私が試合のためにテストしているキャラクターかどうかを確かめようとしています。以下はテキストファイル内の文字の位置を特定する方法(C++)
コード:
//function
string position_upper_alpha(char upper_letter){
string line;
int location=0;
string val;
string iname;
iname = "testing.txt";
ifstream ist {iname}; // ist is an input stream for the file named name
if (!ist) error("can't open input file ",iname);
getline (ist, line);
for (int i=1;i<line.length();i++){
if (line.at(i)==upper_letter){
location=i;
break;
}
}
if(location ==0){
val="Not Found";
}
return val;
}
int main(){
//arbitrary character for testing
char test = 'S';
cout<<test<<position_upper_alpha(test);
}
空の文字列を返すとどうなりますか? – mpiatek