2017-03-03 9 views
2

4行の入力テキストファイルがあり、各行は固定長80文字です。すべてのカンマをスペースで置き換えたい私は以下のコードを書いて、Code :: Blocks IDEでコンパイルして実行します。問題は、出力ファイルに余分な行が含まれていることです。間違いを修正するのを手伝ってください。私はC++の初心者です。C++を使用してファイル内の文字を置き換えます

inputFile

outputFile

#include <iostream> 
#include <fstream> 
#include <string> 


using namespace std; 

int main() 
{ 
ifstream in("circArc.txt", ios::in | ios::binary); 


if(!in) 
{ 
    cout << "Cannot open file"; 
    return 1; 
} 

ofstream out("readInt.txt", ios::out | ios::binary); 
if(!out) 
{ 
    cout << "Cannot open file"; 
    return 1; 
} 

string str; 
char rep[80]; //replace array 

while(in) 
{ 
    getline(in,str); 
    for(int i=0; i<80; i++) 
    { 
     if(str[i] == ',') 
      rep[i] = ' '; 
     else 
      rep[i] = str[i]; 
     out.put(rep[i]); 
    } 
    out << endl; 

} 
in.close(); 
out.close(); 
return 0; 
} 

答えて

1

使用上の問題

while(in) 
{ 
    getline(in,str); 

は、getlineが成功したかどうかを確認していないということです。無関係にstrを使用しています。

私はここでの問題は、あなたのwhileループで終了条件だと思います

while(getline(in,str)) 
{ 
    ... 
} 
+0

@Antony、このSOの質問への答えを見てみましょう。 http://stackoverflow.com/questions/42571529/how-to-count-the-number-of-lines-in-a-file-using-c。あなたが同じ状況に遭遇したように見えます。 –

+0

@Antony:getline()を2回呼び出すので – androidFan

0

しばらく()ループを保つが、最後の行のwhileループ内で、その後一度/ whileループの外の前にそれを行うと:

#include <iostream> 
#include <fstream> 
#include <string> 


using namespace std; 

int main() 
{ 
ifstream in("circArc.txt", ios::in | ios::binary); 


if(!in) 
{ 
    cout << "Cannot open file"; 
    return 1; 
} 

ofstream out("readInt.txt", ios::out | ios::binary); 
if(!out) 
{ 
    cout << "Cannot open file"; 
    return 1; 
} 

string str; 
char rep[80]; //replace array 

getline(in,str); 

while(in) 
{ 
    for(int i=0; i<80; i++) 
    { 
     if(str[i] == ',') 
      rep[i] = ' '; 
     else 
      rep[i] = str[i]; 
     out.put(rep[i]); 
    } 
    out << endl; 

    getline(in,str); 

} 

in.close(); 
out.close(); 
return 0; 
} 
+0

問題あまりにも。 – Antony

0

while(in) 
{ 
    getline(in,str); 
    ... 
} 

を交換してください。あなたは使用することができます。

while(getline(in, str)) { 
    if (in.eof()) { 
     break; 
    } 
    /** This will get you out if you are reading past the end of file 
    * which seems to be the problem. 
    */ 
    ... 
0

問題は、あなたが(簡単に)最後の文字が行末のかいないのであれば言うことができないので、それが存在する場合std::getlineは行末文字を削除していることです。

あなただけ一度に一つの文字、それを処理することができるようにあなたは、このタスクのためのあなたのデータの形式についてworyする必要はありません。私には思える:

ifstream in("circArc.txt", ios::in | ios::binary); 

ofstream out("readInt.txt", ios::out | ios::binary); 

for(char c; in.get(c); out.put(c)) 
    if(c == ',') 
     c = ' '; 

あなたが本当に行を処理する場合ラインであなたが読んでラインは行末文字が含まれているかどうかを確認する必要があり、入力の1があった場合のみ、エンド・オブ・ライン出力では、次のとおりです。このことによって解決

ifstream in("circArc.txt", ios::in | ios::binary); 

ofstream out("readInt.txt", ios::out | ios::binary); 

for(std::string line, eol; std::getline(in, line); out << line << eol) 
{ 
    // only add an eol to output if there was an eol in the input 
    eol = in.eof() ? "":"\n"; 

    // replace ',' with ' ' 
    std::transform(std::begin(line), std::end(line), std::begin(line), 
     [](char c){ if(c == ',') return ' '; return c; }); 
} 
関連する問題