2013-03-20 238 views
1

我的循環有些東西肯定是錯誤的,因爲在讀取並執行第一行程序結束之後。ifstream getline問題(它只能讀取第一行)

if (infile.is_open()) 
{ 
    cout << "Input filename: "; 
    cin>>filename; 
    infile.open(filename.c_str()); 

    cout<< "Output filename: "; 
    cin>>filename; 
    outfile.open(filename.c_str()); 


    while(getline(infile,input)) 
    { 
     string output = ""; 
     for(int x = 0; x < input.length(); x++) 
      output += cipher(input[x]); 

     cout<<output<<endl; 
     outfile<<output; 
    } 
} 

有關如何使這項工作的任何建議?

編輯

其次的建議,並得到這個:

if (infile.is_open()) { 

     cout << "Input filename: "; 
     cin>>filename; 
     infile.open(filename.c_str()); 
     if (!infile.is_open()) 
     { 
     std::cout << "Failed to open the input file." << std::endl; 
     return -1; 
     } 


     cout<< "Output filename: "; 
     cin>>filename; 
     outfile.open(ofilename.c_str()); 
     if (!outfile.is_open()) 
     { 
     std::cout << "Failed to open the output file." << std::endl; 
     return -1; 
     } 


     while(getline(infile,line)){ 

     string output = ""; 
     for(int x = 0; x < input.length(); x++) { 

     output += cipher(input[x]); 


     } 

     } 

,但它仍然只讀取第一線......一切工作完全沒有問題....只是不能讀取超出第一行的任何內容..

+1

'如果(infile.is_open()){... infile.open(filename.c_str()); ......} - 對我沒有任何意義。 – LihO 2013-03-20 01:52:12

+0

@LihO你並不孤單。 – WhozCraig 2013-03-20 01:52:50

+0

你能解釋爲什麼你在打開'infile'之前測試'in file.is_open()',然後及時***不***檢查它,當你*應該*? – WhozCraig 2013-03-20 02:01:42

回答

0

所以我建議這一點。

  1. char cipher(char ch)返回加密的輸入什麼。如果你不想加密空白,那麼不要。但總是返回加密字符或未修改字符。

  2. 使用std::transform,std::istream_iteratorstd::ostream_iterator來轉換您的輸入和輸出文件。

  3. 在正確的時間檢查您的文件狀態。

一個例子如下所示:

#include <iostream> 
#include <fstream> 
#include <iteraor> 
#include <string> 
using namespace std; 

char cipher(char ch) 
{ 
    if (std::isalpha(ch)) 
    { 
     // TODO: change ch to whatever you want here. 
    } 

    // but always return it, whether you changed it or not. 
    return ch; 
} 

int main() 
{ 
    int res = EXIT_SUCCESS; 

    string in_filename, out_filename; 
    cout << "Input filename: "; 
    cin >> in_filename; 
    cout << "Output filename: "; 
    cin >> out_filename; 

    // don't skip whitespace 
    ifstream infile(in_filename); 
    ofstream outfile(out_filename); 
    if ((infile >> noskipws) && outfile) 
    { 
     std::transform(istream_iterator<char>(infile), 
         istream_iterator<char>(), 
         ostream_iterator<char>(outfile), 
         cipher); 
    } 
    else 
    { 
     perror("Failed to open files."); 
     res = EXIT_FAILURE; 
    } 
    return res; 
} 
1

看來你誤解了fstream的方法is_open()方法的一點,因爲這個代碼:

if (infile.is_open()) 
{ 
    cout << "Input filename: "; 
    cin>>filename; 
    infile.open(filename.c_str()); 
    ... 
} 

檢查infile是否已成功打開(即, 如果要麼成員open之前調用成功,或者如果對象是使用參數的構造函數構建成功,
和關閉後還沒有被
調用),並在情況下,它是開放從cin並檢索輸入文件名打開文件。

良好的開端將是由線路輸入文件行讀取和不做處理直接寫入這些線路到輸出文件的程序:

// retrieve the name of the input file and open it: 
cout << "Input filename: "; 
cin>>filename; 
infile.open(filename.c_str()); 
if (!infile.is_open()) 
{ 
    std::cout << "Failed to open the input file." << std::endl; 
    return -1; 
} 

// retrieve the name of the output file and open it: 
cout << "Output filename: "; 
cin >> filename; 
outfile.open(filename.c_str()); 
if (!outfile.is_open()) 
{ 
    std::cout << "Failed to open the output file." << std::endl; 
    return -1; 
} 

std::string line; 
while(getline(infile,line)) 
{ 
    std::cout << line << std::endl; 
    outfile << line; 
} 
+0

我遵循你的建議並編輯了上面的帖子 – user2183126 2013-03-20 02:15:16