2015-12-29 73 views
1
#include<iostream> 
#include<fstream> 
#include<string> 
#include<vector> 

using namespace std; 

int main(){ 

    ofstream out; 
    ifstream in; 

    out.open("The Necessary Death of Charlie Countryman2.srt"); 
     if (out.fail()) { 
      perror("The Necessary Death of Charlie Countryman2.srt"); 
     } 

    in.open("The Necessary Death of Charlie Countryman.srt"); 
     if (in.fail()) { 
      perror("The Necessary Death of Charlie Countryman.srt"); 
     } 

    vector<string> input; 
    string inc; 

    while (getline(in, inc)) { 
     input.push_back(inc); 
    } 

    for (int k = 0; k < input.size(); k++) { 
     out << input[k] << endl; 
    }  
    return 0; 
} 

當我使用ifstream的和寫入使用ofstream的anotherfile.txt,一切工作正常,但假設我註釋掉out.open("anotherfile.txt")並執行從somefile.txt閱讀代碼,沒有錯誤,當我打開anotherfile.txt它是空的。當ofstream object.open()被註釋掉時會發生什麼?

我的問題是在

for (int k = 0; k < input.size(); k++) { 
      out << input[k] << endl; 
     } 

會發生什麼?

哪裏或什麼是input[k]發送給?如果是cout它會轉到命令提示符,如果out.open("anotherfile.txt")未被註釋,則轉到anotherfile.txt。

+1

好像它被髮送到從未使用的緩衝區。但是我找不到關於它的更多信息 – I3ck

+1

當輸出文件沒有被打開時,文本被簡單地加載到緩衝區中並在程序終止時被銷燬。除非您打開文件進行閱讀,否則您嘗試發送至輸出文件的文本將被中繼回堆棧。 – Poriferous

+0

@ I3ck和@ Poriferous謝謝你們,這就是我想知道我的'輸入[k]'發送到哪裏。 – nSv23

回答

2

每個<<操作失敗,就是這樣(即(1)發生的一切)。


(1)流可以在其上拋出異常的故障的模式來設定,但是這不是默認。默認情況下,失敗僅僅是被忽略。除了設置了失敗狀態位之外,您可以使用fail()或通過轉換爲bool來查看失敗狀態位。

+0

我不認爲'getline'失敗,因爲'getline(in,inc)'從'in'中讀取'ifstream'對象,它與'outstream'對象'out'無關。 – nSv23

+0

@ nSv23:哦,你是對的:這是另一個未被註釋的流。好,修復。 –

+0

yes'input'是一個'std :: vector',但這幾乎是整個代碼,只是從'in'讀入並寫入'out'。 – nSv23

2

我假定out被聲明爲一個ostream(或子類如ofstream)。當您註釋掉行out.open(...)時,out被初始化爲封閉文件。因此,當您嘗試寫入它時,沒有任何反應,並且注入失敗...但未能測試它是否成功!

如果你測試了它:

for (int k = 0; k < input.size(); k++) { 
     out << input[k] << endl; 
     if out.fail() { 
      // process error condition... 
     } 
    } 

,你會傳遞錯誤條件分支,將能夠知道,並沒有出現注射。

1

outstd::ostream類型的對象,同樣std::coutstd::ostream類型住在全局命名空間中的對象(它可以在你的程序的開頭)。

類型std::ostream(輸出流,包括擴展它的子類,如std::ofstream)的對象的運算符已重載<<

cout<<"hello";這種方式是同樣的事情,std::cout.operator<<("Hello World");

注意,重載<<操作返回ostream對象的引用,因此cout<<"hi" << " there";基本上

std::cout.operator<<("hi").operator<<(" there")); 

你需要打開任何輸出文件流然後寫入它。通過這樣做,您可以調用操作系統的系統調用,使您可以訪問這些文件,以便寫入它們。否則,您正在嘗試寫入一個已關閉的文件,表示您的進程無法訪問寫入。

您不必爲std::cout明確地執行此操作,但您應該打開自己的輸出文件流。

不要忘了關閉你打開的文件!

+0

這不回答這個問題。 – wallyk

+0

不確定我是否遵循,他問爲什麼他需要在寫入輸出流之前打開輸出流,並且想知道爲什麼cout的行爲不同。 – ForeverStudent

+1

他或她在寫入* not * open的流時會發生什麼情況。 OP不詢問「如何寫入文件?」。 – wallyk

2
ofstream

使用此operator<<簽名

basic_ostream& operator<<(std::basic_streambuf<CharT, Traits>* sb); 

cpppreferenceoperator<<,8)

構造和檢查對象崗哨,檢查是否SB是一個空指針之後。如果是,則執行setstate(badbit)並退出。

您可以通過執行

#include <iostream> 
#include <fstream> 

int main() 
{ 
    std::ofstream sink; 
    //sink.open("/dev/null"); 

    sink << "check state"; 

    switch(sink.rdstate()) 
    { 
     case std::ios_base::failbit: 
      std::cout << "stream state is failbit\n"; 
      break; 
     case std::ios_base::goodbit: 
      std::cout << "stream state is goodbit\n"; 
      break; 
     case std::ios_base::eofbit: 
      std::cout << "stream state is eofbit\n"; 
      break; 
     case std::ios_base::badbit: 
      std::cout << "stream state is badbit\n"; 
      break; 
     default: 
      break; 
    } 

    return 0; 
} 

Prints容易檢查:流狀態在badbit

相關問題