Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. converting int to char

converting int to char

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialdata-structuresquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cyn8
    wrote on last edited by
    #1

    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!

    D C 2 Replies Last reply
    0
    • C cyn8

      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!

      D Offline
      D Offline
      doctorpi
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C cyn8

        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!

        C Offline
        C Offline
        cp9876
        wrote on last edited by
        #3

        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."

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups