Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6594215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:46:54+00:00 2026-05-25T17:46:54+00:00

I want to read and write NaN values from/into text files using iostream and

  • 0

I want to read and write NaN values from/into text files using iostream and Visual C++. When writing a NaN value, i get 1.#QNAN. But, reading it back outputs 1.0 .

float nan = std::numeric_limits<float>::quiet_NaN ();
std::ofstream os("output.txt");

os << nan ;
os.close();

The output is 1.#QNAN .

std::ifstream is("output.txt");
is >> nan ;
is.close();

nan equals 1.0.

Solution

Finally, as suggested by awoodland, I’ve come up with this solution. I chose “nan” as a string representation of a NaN. Both << and >> operators are overridden.

using namespace ::std;

class NaNStream 
{
public:
  NaNStream(ostream& _out, istream& _in):out(_out), in(_in){}
  template<typename T>
  const NaNStream& operator<<(const T& v) const {out << v;return *this;}
  template<typename T>
  const NaNStream& operator>>(T& v) const {in >> v;return *this;}
protected:
  ostream& out;
  istream& in;
};

// override << operator for float type
template <> const NaNStream& NaNStream::operator<<(const float& v) const 
{
  // test whether v is NaN 
  if( v == v )
    out << v;
  else
    out << "nan";
  return *this;
}

// override >> operator for float type
template <> const NaNStream& NaNStream::operator>>(float& v) const 
{
  if (in >> v)
    return *this;

  in.clear();
  std::string str;
  if (!(in >> str))
    return *this;

  if (str == "nan")
    v = std::numeric_limits<float>::quiet_NaN();
  else
    in.setstate(std::ios::badbit); // Whoops, we've still "stolen" the string

  return *this;
}

A minimal working example: a finite float and a NaN are written into a stringstream and then read back.

int main(int,char**) 
{
  std::stringstream ss;
  NaNStream nis(ss, ss);
  nis << 1.5f << std::numeric_limits<float>::quiet_NaN ();
  std::cout << ss.str() << std::endl; // OUTPUT : "1.5nan"

  float a, b;
  nis >> a;  nis >> b;
  std::cout << a << b << std::endl;  // OUTPUT : "1.51.#QNAN"
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:46:54+00:00Added an answer on May 25, 2026 at 5:46 pm

    With C++03 you can fairly easily work around the issue with the aid of a helper class and your own operator:

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <limits>
    
    struct FloatNaNHelper {
      float value;
      operator const float&() const { return value; }
    };
    
    std::istream& operator>>(std::istream& in, FloatNaNHelper& f) {
      if (in >> f.value)
        return in;
    
      in.clear();
      std::string str;
      if (!(in >> str))
        return in;
    
      // use std::transform for lowercaseness?
      // NaN on my platform is written like this.
      if (str == "NaN")
        f.value = std::numeric_limits<float>::quiet_NaN();
      else
        in.setstate(std::ios::badbit); // Whoops, we've still "stolen" the string
    
      return in;
    }
    

    This works for NaN on my platform quite sensibly, but also illustrates the portability problem inherent there too – your library seems to represent it differently, which could complicate the issue somewhat if you wanted to support both.
    I used this test with it:

    int main() {
      std::istringstream in("1.0 555 NaN foo");
      FloatNaNHelper f1,f2,f3;
      in >> f1 >> f2 >> f3;
      std::cout << static_cast<float>(f1) << ", " << static_cast<float>(f2) << ", " << static_cast<float>(f3) << std::endl;
    
      if (in >> f1)
        std::cout << "OOPS!" << std::endl;
    }
    

    You can also change the semantics of this to something possibly a little cleaner:

    int main() {
      std::istringstream in("1.0 555 NaN foo");
      float f1,f2,f3;
      in >> FloatNaNHelper(f1) >> FloatNaNHelper(f2) >> FloatNaNHelper(f3);
      std::cout << f1 << ", " << f2 << ", " << f3 << std::endl;
    }
    

    Requires the changing FloatNaNNHelper:

    struct FloatNaNHelper {
      float& value;
      explicit FloatNaNHelper(float& f) : value(f) { }
    };
    

    And the operator:

    std::istream& operator>>(std::istream& in, const FloatNaNHelper& f);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read and write from serial using events/interrupts. Currently, I have it
I am using PHPExcel library to read/write excel files. Now I want to generate
I am using Java 6 and want to process (read and write) xml files
I want to know is it possible to read/write google spreadsheets using GET/POST methods
I have servlet deployed in JBoss. I want to read/write data into a text
I want to read and write python-source-files from the file system in a thread-safe
i want simultaneously read and write data into file. Can i use StreamReader and
I want to read multiple RSS feeds using jQuery. I'm trying to write a
I am writing to file in android and read from the same file using
I know how to read/write to local files on iOS using file handles. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.