writing strings chars from ints

This is a discussion on writing strings chars from ints within the C++ Programming forums, part of the General Programming Boards category; bennyandthejets is right. The integer (created by a random number generator) is "50", to be written to a text file ...

  1. #16
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    bennyandthejets is right. The integer (created by a random number generator) is "50", to be written to a text file as
    "50"--in readable form.
    Deum solum fidentia est

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why all the confusion?
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    int main(void)
    {
      ofstream f;
      int i;
      
      srand (10);  // Amend as appropriate
      
      f.open ("out.txt");
      
      if (f)
      {
        i = rand() % 10;
        f <<i <<flush;
        f.close();
      }
      else cerr <<"open error" <<endl;
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    Hammer, is the file "out.txt" an ASCII file that one can read, or is it a binary?
    The file that I need must be ascii, like an ini file.

    THX
    Deum solum fidentia est

  4. #19
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    ripper079 is very correct. You can also use
    Code:
    int main()
    {
       int number = 50;
       char buffer[10];
    /*
    itoa(the number, 
         the buffer for the return string, 
         the base of the number);
    */
       itoa(number, buffer, 10);
      //do stuff with buffer
       return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #20
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by deleeuw
    Hammer, is the file "out.txt" an ASCII file that one can read, or is it a binary?
    The file that I need must be ascii, like an ini file.

    THX
    The file out.txt is in text format (ascii char's), so you should be fine.

  6. #21
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    Thanx all!
    the itoa ([int], [char], 10) worked!
    Deum solum fidentia est

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Question about strings n chars
    By cszym001 in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2008, 05:09 PM
  4. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21