Output numbers in C++

This is a discussion on Output numbers in C++ within the C++ Programming forums, part of the General Programming Boards category; I want to ask a question about output numbers to a file in C++. I know how to output characters ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    Output numbers in C++

    I want to ask a question about output numbers to a file in C++.
    I know how to output characters to a file using putc() but what to do if what I want to output are numbers?
    I want to input some int values to a file and I dunno what function to use.
    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    One way is just to use formatted output:
    Code:
    std::ofstream out("test.txt");
    out << 123;
    It would be no different from printing numbers to std::cout.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    I've tried ofstream out but I get these errors:

    error C2514: 'std::basic_fstream<char,struct std::char_traits<char> >' : class has no constructors

    error C2079: 'out' uses undefined class 'basic_ofstream<char,struct std::char_traits<char> >'

    error C2440: 'initializing' : cannot convert from 'char [11]' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast

    And this warning:

    warning C4552: '<<' : operator has no effect; expected operator with side-effect
    Error executing cl.exe.

    Ho to solve this issue?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    How did you use ofstream?
    Did you #include <fstream> ?

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Please post your code. Sometimes it's easier to SEE the code than to try and reverse interpret the compiler errors.

    Todd

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    Wink

    How did you use ofstream?
    Did you #include <fstream> ?
    Indeed I forget include fstream library
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. Outputting numbers in arrays
    By rachael033 in forum C++ Programming
    Replies: 10
    Last Post: 05-29-2007, 02:56 AM
  3. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  4. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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