Prev: C++ How to Program, 7/E Deitel & Deitel instructors solution manual ch 1-27 is available at affordable prices. Email at allsolutionmanuals11[at]gmail.com if you need to buy this. All emails will be answered ASAP.
Next: oops! oddly result! help me? (about MemoryMap class)?
From: John H. on 14 Dec 2009 19:21 On Dec 13, 11:01 am, "scooper" <robert...(a)internode.on.net> wrote: > Would anyone please be able to tell me how to read and write a float or > double to a text file. A simple C++ example: #include <fstream> int main() { std::ifstream in("C:\\file_in.txt"); // open the input file double num; in >> num; // read in a double std::ofstream out("C:\\file_out.txt"); // open the output file out << num; // write out the double return 0; } The same example, with some error checking in place might look like: #include <fstream> int main() { std::ifstream in("C:\\file_in.txt"); // open the input file if(!in.good()) // make sure it opened ok { return 1; } double num = 0.0; in >> num; // read in a double if(in.fail()) // make sure we read in ok { return 2; } in.close(); // done reading so we can close the input std::ofstream out("C:\\file_out.txt"); // open the output file if(!out.good()) // make sure it opened ok { return 3; } out << num; // write out the double if(out.fail()) // make sure it wrote ok { return 4; } out.close(); // done writing so close the output return 0; }
From: scooper on 15 Dec 2009 03:25 Great answers everybody!!!! Thank you all. It was C++ too. scooper "scooper" <robertc47(a)internode.on.net> wrote in message news:00aea297$0$15603$c3e8da3(a)news.astraweb.com... > Would anyone please be able to tell me how to read and write a float or > double to a text file. > I am stuck on reading a line from the file into a string variable but > cannot use the atof or strtod functions because it isn't a char[] array > variable and a char array variable can't read a line from a file(?). > What do I do? > scooper >
From: Francis Glassborow on 15 Dec 2009 04:22 John H. wrote: > On Dec 13, 11:01 am, "scooper" <robert...(a)internode.on.net> wrote: >> Would anyone please be able to tell me how to read and write a float or >> double to a text file. > > A simple C++ example: > #include <fstream> > int main() > { > std::ifstream in("C:\\file_in.txt"); // open the input file > double num; > in >> num; // read in a double > std::ofstream out("C:\\file_out.txt"); // open the output file > out << num; // write out the double > return 0; > } > > The same example, with some error checking in place might look like: > #include <fstream> > int main() > { > std::ifstream in("C:\\file_in.txt"); // open the input file > if(!in.good()) // make sure it opened ok > { > return 1; > } > double num = 0.0; > in >> num; // read in a double > if(in.fail()) // make sure we read in ok > { > return 2; > } > in.close(); // done reading so we can close the input > std::ofstream out("C:\\file_out.txt"); // open the output file > if(!out.good()) // make sure it opened ok > { > return 3; > } > out << num; // write out the double > if(out.fail()) // make sure it wrote ok > { > return 4; > } > out.close(); // done writing so close the output > return 0; > } Those return values are not guaranteed to help. Yes, DOS based OS's can report them but there are OS's that won't be able to. You would be much better of outputting a message to clog (which you could set to be something other than the console. Better still throw an exception (which could be of type int if you want it that way and catch it at the end of main() and output an error message (preferably to clog)
From: Francis Glassborow on 15 Dec 2009 18:15 Stefan Ram wrote: > Francis Glassborow <francis.glassborow(a)btinternet.com> writes: >> Those return values are not guaranteed to help. Yes, DOS based OS's can >> report them but there are OS's that won't be able to. You would be much >> better of outputting a message to clog (which you could set to be >> something other than the console. Better still throw an exception (which >> could be of type int if you want it that way and catch it at the end of >> main() and output an error message (preferably to clog) > > Runtime error handling really is a difficult part of programming! > > One idea I have to accomplish �idiomatic� programing in any language > is to look at how the standard library of that language does it. > > So, does any C++ standard library function write to �clog� > in certain circumstances? > > If not, then, why might the designers of the C++ standard > library have decided to refrain from this? > > And which examples are the in the C++ standard library of > functions that might throw exceptions? (I really do not know.) > But there are nor examples in the Standard library of catching. And it is the catch that handles the error. clog is an unbuffered output stream and was originally designed (in C IIRC) to handle logging (errors in particular). The Standard Library gives examples of exceptions but not what to do with them when you catch one.
First
|
Prev
|
Pages: 1 2 3 Prev: C++ How to Program, 7/E Deitel & Deitel instructors solution manual ch 1-27 is available at affordable prices. Email at allsolutionmanuals11[at]gmail.com if you need to buy this. All emails will be answered ASAP. Next: oops! oddly result! help me? (about MemoryMap class)? |