खोज…


परिचय

C ++ फ़ाइल I / O स्ट्रीम के माध्यम से किया जाता है । प्रमुख सार हैं:

std::istream पाठ पढ़ने के लिए std::istream

std::ostream टेक्स्ट लिखने के लिए std::ostream

std::streambuf पढ़ने या लिखने के लिए अक्षर।

प्रारूपित इनपुट operator>> का उपयोग करता है।

स्वरूपित आउटपुट operator<< का उपयोग करता है।

धाराएँ std::locale , जैसे, स्वरूपण के विवरण और बाहरी एन्कोडिंग और आंतरिक एन्कोडिंग के बीच अनुवाद के लिए उपयोग करती हैं।

धाराओं पर अधिक: <iostream> पुस्तकालय

एक फ़ाइल खोलना

एक फाइल को खोलना सभी 3 फाइल स्ट्रीम ( ifstream , ofstream , and fstream ) के लिए उसी तरह से किया जाता है।

आप फ़ाइल को सीधे कंस्ट्रक्टर में खोल सकते हैं:

std::ifstream ifs("foo.txt");  // ifstream: Opens file "foo.txt" for reading only.

std::ofstream ofs("foo.txt");  // ofstream: Opens file "foo.txt" for writing only.

std::fstream iofs("foo.txt");  // fstream:  Opens file "foo.txt" for reading and writing.

वैकल्पिक रूप से, आप फ़ाइल स्ट्रीम के सदस्य फ़ंक्शन को open() का उपयोग कर सकते हैं:

std::ifstream ifs;
ifs.open("bar.txt");           // ifstream: Opens file "bar.txt" for reading only.

std::ofstream ofs;
ofs.open("bar.txt");           // ofstream: Opens file "bar.txt" for writing only.

std::fstream iofs;
iofs.open("bar.txt");          // fstream:  Opens file "bar.txt" for reading and writing.

आपको हमेशा यह देखना चाहिए कि क्या कोई फ़ाइल सफलतापूर्वक खोली गई है (लिखते समय भी)। विफलताओं में शामिल हो सकते हैं: फ़ाइल मौजूद नहीं है, फ़ाइल का अधिकार अधिकार नहीं है, फ़ाइल पहले से ही उपयोग में है, डिस्क त्रुटियां हुई हैं, डिस्कनेक्ट हो गई है ... जाँच निम्नानुसार की जा सकती है:

// Try to read the file 'foo.txt'.
std::ifstream ifs("fooo.txt");  // Note the typo; the file can't be opened.

// Check if the file has been opened successfully.
if (!ifs.is_open()) {
    // The file hasn't been opened; take appropriate actions here.
    throw CustomException(ifs, "File could not be opened");
}

जब फ़ाइल पथ में बैकस्लैश होते हैं (उदाहरण के लिए, विंडोज सिस्टम पर) तो आपको उन्हें ठीक से बचना चाहिए:

// Open the file 'c:\folder\foo.txt' on Windows.
std::ifstream ifs("c:\\folder\\foo.txt"); // using escaped backslashes
सी ++ 11

या कच्चे शाब्दिक उपयोग करें:

// Open the file 'c:\folder\foo.txt' on Windows.
std::ifstream ifs(R"(c:\folder\foo.txt)"); // using raw literal

या इसके बजाय फ़ॉरवर्ड स्लैश का उपयोग करें:

// Open the file 'c:\folder\foo.txt' on Windows.
std::ifstream ifs("c:/folder/foo.txt");
सी ++ 11

यदि आप Windows में गैर-ASCII वर्ण पथ पर फ़ाइल को खोलना चाहते हैं, तो वर्तमान में आप गैर-मानक विस्तृत वर्ण पथ तर्क का उपयोग कर सकते हैं:

// Open the file 'пример\foo.txt' on Windows.
std::ifstream ifs(LR"(пример\foo.txt)"); // using wide characters with raw literal

एक फ़ाइल से पढ़ना

फ़ाइल से डेटा पढ़ने के कई तरीके हैं।

यदि आप जानते हैं कि डेटा को कैसे स्वरूपित किया जाता है, तो आप स्ट्रीम निष्कर्षण ऑपरेटर ( >> ) का उपयोग कर सकते हैं। मान लेते हैं कि आपके पास foo.txt नाम की एक फाइल है जिसमें निम्नलिखित डेटा है:

John Doe 25 4 6 1987
Jane Doe 15 5 24 1976

फिर आप फ़ाइल से उस डेटा को पढ़ने के लिए निम्न कोड का उपयोग कर सकते हैं:

// Define variables.
std::ifstream is("foo.txt");
std::string firstname, lastname;
int age, bmonth, bday, byear;

// Extract firstname, lastname, age, bday month, bday day, and bday year in that order.
// Note: '>>' returns false if it reached EOF (end of file) or if the input data doesn't
// correspond to the type of the input variable (for example, the string "foo" can't be
// extracted into an 'int' variable).
while (is >> firstname >> lastname >> age >> bmonth >> bday >> byear)
    // Process the data that has been read.

धारा निष्कर्षण ऑपरेटर >> प्रत्येक वर्ण को निकालता है और अगर यह एक ऐसा चरित्र पाता है जो संग्रहीत नहीं किया जा सकता है या यदि यह एक विशेष चरित्र है:

  • स्ट्रिंग प्रकारों के लिए, ऑपरेटर एक व्हाट्सएप पर रुकता है ( ) या एक नई रेखा ( \n ) पर।
  • संख्याओं के लिए, ऑपरेटर गैर-संख्या वर्ण पर रुक जाता है।

इसका अर्थ है कि फ़ाइल का निम्न संस्करण foo.txt भी पिछले कोड द्वारा सफलतापूर्वक पढ़ा जाएगा:

John 
Doe 25
4 6 1987


Jane
Doe 
15 5
24
1976

स्ट्रीम निष्कर्षण ऑपरेटर >> हमेशा दिए गए स्ट्रीम को लौटाता है। इसलिए, डेटा को लगातार पढ़ने के लिए कई ऑपरेटरों को एक साथ जंजीर में डाला जा सकता है। हालांकि, एक धारा भी एक बूलियन अभिव्यक्ति के रूप में (के रूप में में दिखाया गया है इस्तेमाल किया जा सकता while पिछले कोड में पाश)। इसका कारण यह है कि स्ट्रीम कक्षाओं में टाइप bool लिए रूपांतरण ऑपरेटर होता है। यह bool() ऑपरेटर तब तक true रहेगा जब तक स्ट्रीम में कोई त्रुटि नहीं है। यदि कोई स्ट्रीम त्रुटि स्थिति में जाती है (उदाहरण के लिए, क्योंकि कोई अधिक डेटा नहीं निकाला जा सकता है), तो bool() ऑपरेटर false वापस आ जाएगा। इसलिए, while पिछले कोड में पाश इनपुट फ़ाइल के बाद अपने अंत करने के लिए पढ़ने के लिए कर दिया गया है से बाहर निकल गया हो जाएगा।

यदि आप एक स्ट्रिंग के रूप में पूरी फ़ाइल पढ़ना चाहते हैं, तो आप निम्नलिखित कोड का उपयोग कर सकते हैं:

// Opens 'foo.txt'.
std::ifstream is("foo.txt");
std::string whole_file;

// Sets position to the end of the file.
is.seekg(0, std::ios::end);

// Reserves memory for the file.
whole_file.reserve(is.tellg());

// Sets position to the start of the file.
is.seekg(0, std::ios::beg);

// Sets contents of 'whole_file' to all characters in the file.
whole_file.assign(std::istreambuf_iterator<char>(is),
  std::istreambuf_iterator<char>());

यह कोड अनावश्यक स्मृति आवंटन पर कटौती करने के लिए string लिए स्थान रखता है।

यदि आप एक फ़ाइल लाइन लाइन द्वारा पढ़ना चाहते हैं, तो आप फ़ंक्शन getline() उपयोग कर सकते हैं:

std::ifstream is("foo.txt");   

// The function getline returns false if there are no more lines.
for (std::string str; std::getline(is, str);) {
    // Process the line that has been read.
}

यदि आप एक निश्चित संख्या में वर्ण पढ़ना चाहते हैं, तो आप स्ट्रीम के सदस्य फ़ंक्शन को read() :

std::ifstream is("foo.txt");
char str[4];

// Read 4 characters from the file.
is.read(str, 4);

रीड कमांड को निष्पादित करने के बाद, आपको हमेशा जांचना चाहिए कि क्या त्रुटि राज्य ध्वज failbit सेट किया गया है, क्योंकि यह इंगित करता है कि ऑपरेशन विफल हुआ या नहीं। यह फ़ाइल स्ट्रीम की सदस्य फ़ंक्शन fail() को कॉल करके किया जा सकता है:

is.read(str, 4); // This operation might fail for any reason.

if (is.fail())
    // Failed to read!

एक फ़ाइल के लिए लेखन

फ़ाइल में लिखने के कई तरीके हैं। सबसे आसान तरीका है कि आउटपुट इंसर्शन ऑपरेटर ( << ) के साथ मिलकर आउटपुट फाइल स्ट्रीम ( ofstream ) का उपयोग करें:

std::ofstream os("foo.txt");
if(os.is_open()){
    os << "Hello World!";
}

<< बजाय, आप आउटपुट फाइल स्ट्रीम के मेंबर फंक्शन write() भी उपयोग कर सकते हैं:

std::ofstream os("foo.txt");
if(os.is_open()){
    char data[] = "Foo";

    // Writes 3 characters from data -> "Foo".
    os.write(data, 3);
}

एक स्ट्रीम में लिखने के बाद, आपको हमेशा यह देखना चाहिए कि क्या त्रुटि स्टेट फ्लैग badbit सेट किया गया है, क्योंकि यह इंगित करता है कि ऑपरेशन विफल हुआ या नहीं। यह आउटपुट फाइल स्ट्रीम के मेंबर फंक्शन को bad() कहकर किया जा सकता है bad() :

os << "Hello Badbit!"; // This operation might fail for any reason.
if (os.bad())
    // Failed to write!

खोलने के तरीके

फ़ाइल स्ट्रीम बनाते समय, आप एक ओपनिंग मोड निर्दिष्ट कर सकते हैं। एक प्रारंभिक मोड मूल रूप से यह नियंत्रित करने के लिए एक सेटिंग है कि स्ट्रीम फ़ाइल को कैसे खोलता है।

(सभी मोड std::ios में पाया जा सकता है std::ios नाम स्थान।)

फ़ाइल मोड के निर्माण या इसके open() सदस्य फ़ंक्शन के लिए एक ओपनिंग मोड दूसरे पैरामीटर के रूप में प्रदान किया जा सकता है:

std::ofstream os("foo.txt", std::ios::out | std::ios::trunc);

std::ifstream is;
is.open("foo.txt", std::ios::in | std::ios::binary);

यह ध्यान दिया जाना है कि आपको ios::in या ios::out सेट करना है यदि आप अन्य झंडे सेट करना चाहते हैं क्योंकि वे iostream सदस्यों द्वारा स्पष्ट रूप से सेट नहीं किए गए हैं, हालांकि उनका सही डिफ़ॉल्ट मान है।

यदि आप एक उद्घाटन मोड निर्दिष्ट नहीं करते हैं, तो निम्न डिफ़ॉल्ट मोड का उपयोग किया जाता है:

  • ifstream - in
  • ofstream - out
  • fstream - in और out

फ़ाइल खोलने के तरीके जो आप डिज़ाइन द्वारा निर्दिष्ट कर सकते हैं वे हैं:

मोड अर्थ के लिये विवरण
app संलग्न उत्पादन फ़ाइल के अंत में डेटा को लागू करता है।
binary बाइनरी इनपुट आउटपुट इनपुट और आउटपुट बाइनरी में किया जाता है।
in इनपुट इनपुट पढ़ने के लिए फ़ाइल खोलता है।
out उत्पादन उत्पादन लिखने के लिए फ़ाइल खोलता है।
trunc truncate इनपुट आउटपुट खोलते समय फ़ाइल की सामग्री को निकालता है।
ate अंत में इनपुट खुलने पर फ़ाइल के अंत में जाता है।

नोट: binary मोड सेट करने से डेटा को ठीक-ठीक पढ़ा / लिखा जा सकता है; इसे स्थापित नहीं करने से लाइन अनुक्रम के प्लेटफ़ॉर्म विशिष्ट छोर से / के लिए newline '\n' वर्ण का अनुवाद सक्षम हो जाता है।

उदाहरण के लिए विंडोज पर लाइन सीक्वेंस का अंत CRLF ( "\r\n" ) है।
लिखें: "\n" => "\r\n"
पढ़ें: "\r\n" => "\n"

एक फ़ाइल बंद करना

स्पष्ट रूप से एक फ़ाइल को बंद करना C ++ में शायद ही कभी आवश्यक है, क्योंकि एक फ़ाइल स्ट्रीम स्वचालित रूप से अपने विध्वंसक में अपनी संबद्ध फ़ाइल को बंद कर देगी। हालाँकि, आपको फ़ाइल स्ट्रीम ऑब्जेक्ट के जीवनकाल को सीमित करने का प्रयास करना चाहिए, ताकि यह फ़ाइल हैंडल को आवश्यकता से अधिक समय तक खुला न रखे। उदाहरण के लिए, यह सभी फ़ाइल परिचालनों को एक ही दायरे ( {} ) में डालकर किया जा सकता है:

std::string const prepared_data = prepare_data();
{
    // Open a file for writing.
    std::ofstream output("foo.txt");

    // Write data.
    output << prepared_data;
}  // The ofstream will go out of scope here.
   // Its destructor will take care of closing the file properly.

close() स्पष्ट रूप से कॉल करना केवल तभी आवश्यक है जब आप बाद में उसी fstream ऑब्जेक्ट का पुन: उपयोग करना चाहते हैं, लेकिन फ़ाइल को बीच में खुला नहीं रखना चाहते:

// Open the file "foo.txt" for the first time.
std::ofstream output("foo.txt");

// Get some data to write from somewhere.
std::string const prepared_data = prepare_data();

// Write data to the file "foo.txt".
output << prepared_data;

// Close the file "foo.txt".
output.close();

// Preparing data might take a long time. Therefore, we don't open the output file stream
// before we actually can write some data to it.
std::string const more_prepared_data = prepare_complex_data();

// Open the file "foo.txt" for the second time once we are ready for writing.
output.open("foo.txt");

// Write the data to the file "foo.txt".
output << more_prepared_data;

// Close the file "foo.txt" once again.
output.close();

एक प्रवाह निस्तब्धता

फ़ाइल स्ट्रीम डिफ़ॉल्ट रूप से बफ़र की जाती हैं, क्योंकि कई अन्य प्रकार की स्ट्रीम हैं। इसका मतलब है कि स्ट्रीम को लिखने से अंतर्निहित फ़ाइल को तुरंत बदलने का कारण नहीं हो सकता है। सभी बफरों को तुरंत जगह लेने के लिए लिखने के लिए ओडर में, आप स्ट्रीम को फ्लश कर सकते हैं। आप इसे सीधे flush() विधि द्वारा या std::flush माध्यम से कर सकते हैं std::flush स्ट्रीम मैनिपुलेटर:

std::ofstream os("foo.txt");
os << "Hello World!" << std::flush;

char data[3] = "Foo";
os.write(data, 3);
os.flush();

एक स्ट्रीम मैनिपुलेटर std::endl है जो स्ट्रीम को फ्लश करने के साथ एक नई लाइन लिखने को जोड़ती है:

// Both following lines do the same thing
os << "Hello World!\n" << std::flush;
os << "Hello world!" << std::endl;

बफरिंग एक स्ट्रीम में लेखन के प्रदर्शन में सुधार कर सकता है। इसलिए, जो अनुप्रयोग बहुत अधिक लेखन करते हैं, उन्हें अनावश्यक रूप से निस्तब्धता से बचना चाहिए। इसके विपरीत, यदि I / O बार-बार किया जाता है, तो स्ट्रीम ऑब्जेक्ट में डेटा के अटकने से बचने के लिए अनुप्रयोगों को बार-बार फ्लशिंग करने पर विचार करना चाहिए।

एक std :: string में ASCII फाइल पढ़ना

std::ifstream f("file.txt");

if (f)
{
  std::stringstream buffer;
  buffer << f.rdbuf();
  f.close();

  // The content of "file.txt" is available in the string `buffer.str()`
}

rdbuf() विधि एक करने के लिए एक सूचक रिटर्न streambuf कि में धकेल दिया जा सकता है buffer के माध्यम से stringstream::operator<< सदस्य कार्य करते हैं।


एक और संभावना ( स्कॉट मेयर्स द्वारा प्रभावी STL में लोकप्रिय है):

std::ifstream f("file.txt");

if (f)
{
  std::string str((std::istreambuf_iterator<char>(f)),
                  std::istreambuf_iterator<char>());

  // Operations on `str`...
}

यह अच्छा है क्योंकि छोटे कोड की आवश्यकता होती है (और किसी भी एसटीएल कंटेनर में सीधे एक फ़ाइल को पढ़ने की अनुमति देता है, न केवल तार) बल्कि बड़ी फ़ाइलों के लिए धीमा हो सकता है।

नोट : स्ट्रिंग कंस्ट्रक्टर के लिए पहले तर्क के आसपास अतिरिक्त कोष्ठक सबसे विकट पार्स समस्या को रोकने के लिए आवश्यक हैं।


अंतिम पर कम नहीं:

std::ifstream f("file.txt");

if (f)
{
  f.seekg(0, std::ios::end);
  const auto size = f.tellg();

  std::string str(size, ' ');
  f.seekg(0);
  f.read(&str[0], size); 
  f.close();

  // Operations on `str`...
}

जो शायद सबसे तेज़ विकल्प है (तीन प्रस्तावित के बीच)।

एक कंटेनर में एक फ़ाइल पढ़ना

नीचे दिए गए उदाहरण में हम फ़ाइल से आइटम पढ़ने के लिए std::string और operator>> का उपयोग करते हैं।

    std::ifstream file("file3.txt");

    std::vector<std::string>  v;

    std::string s;
    while(file >> s) // keep reading until we run out
    {
        v.push_back(s);
    }

उपरोक्त उदाहरण में, हम बस operator>> उपयोग से एक समय में एक "आइटम" पढ़ने वाली फ़ाइल के माध्यम से पुनरावृत्ति कर रहे हैं। इसी प्रभाव को std::istream_iterator का उपयोग करके प्राप्त किया जा सकता है, जो एक इनपुट std::istream_iterator जो स्ट्रीम से एक बार में एक "आइटम" पढ़ता है। इसके अलावा अधिकांश कंटेनरों का निर्माण दो पुनरावृत्तियों का उपयोग करके किया जा सकता है ताकि हम उपरोक्त कोड को सरल बना सकें:

    std::ifstream file("file3.txt");

    std::vector<std::string>  v(std::istream_iterator<std::string>{file},
                                std::istream_iterator<std::string>{});

हम उस वस्तु को पढ़ने के लिए बढ़ा सकते हैं जिसे हम पसंद करते हैं, बस उस वस्तु को निर्दिष्ट करके जिसे हम टेम्पलेट पैरामीटर के रूप में पढ़ना चाहते हैं std::istream_iterator । इस प्रकार हम बस ऊपर लाइनों को पढ़ सकते हैं (शब्दों के बजाय) इस तरह से:

// Unfortunately there is  no built in type that reads line using >>
// So here we build a simple helper class to do it. That will convert
// back to a string when used in string context.
struct Line
{
    // Store data here
    std::string data;
    // Convert object to string
    operator std::string const&() const {return data;}
    // Read a line from a stream.
    friend std::istream& operator>>(std::istream& stream, Line& line)
    {
        return std::getline(stream, line.data);
    }
};


    std::ifstream file("file3.txt");

    // Read the lines of a file into a container.
    std::vector<std::string>  v(std::istream_iterator<Line>{file},
                                std::istream_iterator<Line>{});

एक स्वरूपित पाठ फ़ाइल से एक `संरचना` पढ़ना।

सी ++ 11
struct info_type
{
    std::string name;
    int age;
    float height;
    
    // we define an overload of operator>> as a friend function which
    // gives in privileged access to private data members 
    friend std::istream& operator>>(std::istream& is, info_type& info)
    {
        // skip whitespace
        is >> std::ws;
        std::getline(is, info.name);
        is >> info.age;
        is >> info.height;
        return is;
    }
};

void func4()
{
    auto file = std::ifstream("file4.txt");

    std::vector<info_type> v;

    for(info_type info; file >> info;) // keep reading until we run out
    {
        // we only get here if the read succeeded
        v.push_back(info);
    }

    for(auto const& info: v)
    {
        std::cout << "  name: " << info.name << '\n';
        std::cout << "   age: " << info.age << " years" << '\n';
        std::cout << "height: " << info.height << "lbs" << '\n';
        std::cout << '\n';
    }
}

file4.txt

Wogger Wabbit
2
6.2
Bilbo Baggins
111
81.3
Mary Poppins
29
154.8

आउटपुट:

name: Wogger Wabbit
 age: 2 years
height: 6.2lbs

name: Bilbo Baggins
 age: 111 years
height: 81.3lbs

name: Mary Poppins
 age: 29 years
height: 154.8lbs

एक फाइल कॉपी करना

std::ifstream  src("source_filename", std::ios::binary);
std::ofstream  dst("dest_filename",   std::ios::binary);
dst << src.rdbuf();
सी ++ 17

C ++ 17 के साथ किसी फ़ाइल को कॉपी करने का मानक तरीका <filesystem> हैडर और copy_file का उपयोग copy_file :

std::fileystem::copy_file("source_filename", "dest_filename");

फाइलसिस्टम लाइब्रेरी को मूल रूप से boost.filesystem रूप में विकसित किया गया था और अंत में सी ++ 17 के रूप में आईएसओ सी ++ में विलय कर दिया गया था।

लूप स्थिति, खराब अभ्यास के अंदर फ़ाइल के अंत की जांच?

फाइल के अंत को पढ़ने के बाद ही eof true होता है। यह इंगित नहीं करता है कि अगली रीडिंग स्ट्रीम का अंत होगी।

while (!f.eof())
{
  // Everything is OK

  f >> buffer;

  // What if *only* now the eof / fail bit is set?

  /* Use `buffer` */
}

आप सही तरीके से लिख सकते हैं:

while (!f.eof()) 
{  
  f >> buffer >> std::ws;

  if (f.fail())
    break;

  /* Use `buffer` */
}

परंतु

while (f >> buffer)
{
  /* Use `buffer` */
}

सरल और कम त्रुटि प्रवण है।

आगे के संदर्भ:

  • std::ws : एक इनपुट स्ट्रीम से प्रमुख व्हाट्सएप को जोड़ता है
  • std::basic_ios::fail : यदि संबंधित स्ट्रीम पर कोई त्रुटि हुई true तो यह true है

गैर-मानक लोकेल सेटिंग्स के साथ फाइल लिखना

यदि आपको डिफॉल्ट में विभिन्न लोकेल सेटिंग्स का उपयोग करके फाइल लिखने की आवश्यकता है, तो आप एक विशिष्ट फाइल स्ट्रीम के लिए std::locale और std::basic_ios::imbue() का उपयोग कर सकते हैं:

उपयोग के लिए मार्गदर्शन:

  • फ़ाइल खोलने से पहले आपको हमेशा एक स्ट्रीम पर एक स्थानीय लागू करना चाहिए।
  • एक बार स्ट्रीम को imbued कर देने के बाद आपको लोकल को नहीं बदलना चाहिए।

प्रतिबंधों के कारण: एक लोकल के साथ फाइल स्ट्रीम को इम्प्रूव करना अपरिभाषित व्यवहार होता है यदि वर्तमान लोकेल स्वतंत्र नहीं है या फाइल की शुरुआत में इंगित नहीं है।

UTF-8 स्ट्रीम (और अन्य) स्वतंत्र नहीं हैं। UTF-8 लोकेल के साथ एक फ़ाइल स्ट्रीम भी खोला जा सकता है और फ़ाइल से BOM मार्कर पढ़ सकता है; इसलिए फ़ाइल खोलने से फ़ाइल में से वर्ण पढ़ सकते हैं और यह शुरुआत में नहीं होगा।

#include <iostream>
#include <fstream>
#include <locale>

int main()
{
  std::cout << "User-preferred locale setting is "
            << std::locale("").name().c_str() << std::endl;

  // Write a floating-point value using the user's preferred locale.
  std::ofstream ofs1;
  ofs1.imbue(std::locale(""));
  ofs1.open("file1.txt");
  ofs1 << 78123.456 << std::endl;

  // Use a specific locale (names are system-dependent)
  std::ofstream ofs2;
  ofs2.imbue(std::locale("en_US.UTF-8"));
  ofs2.open("file2.txt");
  ofs2 << 78123.456 << std::endl;

  // Switch to the classic "C" locale
  std::ofstream ofs3;
  ofs3.imbue(std::locale::classic());
  ofs3.open("file3.txt");
  ofs3 << 78123.456 << std::endl;
}

यदि आपका प्रोग्राम किसी भिन्न डिफ़ॉल्ट लोकेल का उपयोग करता है और आप फ़ाइलों को पढ़ने और लिखने के लिए एक निश्चित मानक सुनिश्चित करना चाहते हैं, तो स्पष्ट रूप से क्लासिक "C" लोकेल पर स्विच करना उपयोगी है। एक "सी" पसंदीदा स्थान के साथ, उदाहरण लिखते हैं

78,123.456
78,123.456
78123.456

यदि, उदाहरण के लिए, पसंदीदा स्थान जर्मन है और इसलिए एक अलग संख्या प्रारूप का उपयोग करता है, तो उदाहरण लिखते हैं

78 123,456
78,123.456
78123.456

(पहली पंक्ति में दशमलव कॉमा पर ध्यान दें)।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow