2013-01-24 24 views
5

निकोलाई Josuttis अपनी पुस्तक "सी ++ स्टैंडर्ड लाइब्रेरी" के पेज 547 में नीचे दिए गए कोड के संबंध में निम्नलिखित कहते हैं:निकोलाई जोसुटिस अपनी पुस्तक में कहते हैं कि खुले सदस्य समारोह राज्य झंडे को साफ़ नहीं करता है। यह वही नहीं है जो मैंने वीएस -2010 में पाया था। क्या यह एक एमएस मुद्दा है?

Note that after the processing of a file, clear() must be called to clear the state flags that are set at end-of-file. This is required because the stream object is used for multiple files. The member function open() does not clear the state flags. open() never clears any state flags. Thus, if a stream was not in a good state, after closing and reopening it you still have to call clear() to get to a good state. This is also the case, if you open a different file.

// header files for file I/O 
#include <fstream> 
#include <iostream> 
using namespace std; 
/* for all file names passed as command-line arguments 
* - open, print contents, and close file 
*/ 
int main (int argc, char* argv[]) 
{ 
    ifstream file; 
    // for all command-line arguments 
    for (int i=1; i<argc; ++i) { 
     // open file 
     file.open(argv[i]); 
     // write file contents to cout 
     char c; 
     while (file.get(c)) { 
      cout.put(c); 
     } 
     // clear eofbit and failbit set due to end-of-file 
     file.clear(); 
     // close file 
     file.close(); 
    } 
} 

मेरे नीचे कोड VS2010 में एक समस्या के बिना काम करता है। ध्यान दें कि फ़ाइल "data.txt" बनाई जाने के बाद, यह इनपुट स्ट्रीम झंडे को साफ़ किए बिना दो बार पढ़ा जाता है।

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

int main() 
{ 
    // Create file "data.txt" for writing, write 4 lines into the file and close the file. 

    std::ofstream out("data.txt"); 
    out << "Line 1" << '\n' << "Line 2" << '\n' << "Line 3" << '\n' << "Line 4" << '\n'; 
    out.close(); 

    // Open the file "data.txt" for reading and write file contents to cout 

    std::ifstream in("data.txt"); 
    std::string s; 
    while(std::getline(in, s)) std::cout << s << '\n'; 
    std::cout << '\n'; 
    std::cout << std::boolalpha << "ifstream.eof() before close - " << in.eof() << '\n'; 

    // Close the file without clearing its flags 

    in.close(); 
    std::cout << std::boolalpha << "ifstream.eof() after close - " << in.eof() << '\n'; 

    // Open the file "data.txt" again for reading 

    in.open("data.txt"); 
    std::cout << std::boolalpha << "ifstream.good() after open - " << in.good() << '\n'; 
    std::cout << '\n'; 

    // Read and print the file contents 

    while(std::getline(in, s)) std::cout << s << '\n'; 
    std::cout << '\n'; 
} 

ouput

enter image description here

उत्तर

4

के लिए सी ++ 11 बदल गया था कि। सी ++ 98 नियम (जैसा कि जोसुटिस द्वारा सही ढंग से वर्णित है) स्पष्ट रूप से गलत था, इसलिए अगर मुझे कार्यान्वयन का सम्मान नहीं हुआ तो मुझे आश्चर्य नहीं होगा।

+0

कुछ डेटा बिंदु: एमएसवीसी के लिए यह व्यवहार वीसी ++ 2010 के साथ नया है; 2008 और इससे पहले 'खुला() 'पर स्ट्रीम झंडे को साफ़ नहीं करते हैं। लिनक्स पर जीसीसी 4.6.1 वीसी ++ 2010 (यानी, सी ++ 11 अर्थशास्त्र के समान व्यवहार करता है भले ही आप '--std = C++ 98' निर्दिष्ट करते हैं)। –

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^