converting int to char
-
Hi there, I have a .txt file and characters was read from it and was put into character arrays. However, for example if the int 22 and 4 is read from the file and was put into an array of integers, how can I write it in a file using fstream.write(). As far as i know, to use this function, the output should be in character format. Therefore, may i know how to change the integer into character array of '22' and '4' and put into fstream.write(). Thanks for any suggestions!
-
Hi there, I have a .txt file and characters was read from it and was put into character arrays. However, for example if the int 22 and 4 is read from the file and was put into an array of integers, how can I write it in a file using fstream.write(). As far as i know, to use this function, the output should be in character format. Therefore, may i know how to change the integer into character array of '22' and '4' and put into fstream.write(). Thanks for any suggestions!
I hope this help Streams were originally designed for text, so the default output mode is text. In text mode, the newline character (hexadecimal 10) expands to a carriage return–linefeed (16-bit only). The expansion can cause problems, as shown here: #include int iarray[2] = { 99, 10 }; void main() { ofstream os( "test.dat" ); os.write( (char *) iarray, sizeof( iarray ) ); } You might expect this program to output the byte sequence { 99, 0, 10, 0 }; instead, it outputs { 99, 0, 13, 10, 0 }, which causes problems for a program expecting binary input. If you need true binary output, in which characters are written untranslated, you have several choices: Construct a stream as usual, then use the setmode member function, which changes the mode after the file is opened: ofstream ofs ( "test.dat" ); ofs.setmode( filebuf::binary ); ofs.write( char*iarray, 4 ); // Exactly 4 bytes written Specify binary output by using the ofstream constuctor mode argument: #include #include #include int iarray[2] = { 99, 10 }; void main() { ofstream os( "test.dat", ios::binary ); ofs.write( iarray, 4 ); // Exactly 4 bytes written } Use the binary manipulator instead of the setmode member function: ofs << binary; Use the text manipulator to switch the stream to text translation mode. Open the file using the run-time _open function with a binary mode flag: filedesc fd = _open( "test.dat", _O_BINARY | _O_CREAT | _O_WRONLY ); ofstream ofs( fd ); ofs.write( ( char* ) iarray, 4 ); // Exactly 4 bytes written
-
Hi there, I have a .txt file and characters was read from it and was put into character arrays. However, for example if the int 22 and 4 is read from the file and was put into an array of integers, how can I write it in a file using fstream.write(). As far as i know, to use this function, the output should be in character format. Therefore, may i know how to change the integer into character array of '22' and '4' and put into fstream.write(). Thanks for any suggestions!
I'm not exactly sure what you are asking, but
#include <fstream> using namespace std; ofstream out("x.txt"); int n = 22; int m = 4; out << n << endl; // line 1 out << m << endl; // line 2 out << "n = " << n << ", m = " << m << endl; // line 3
results in the following three lines of output:22 4 n = 22, m = 4
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."