Thread: Output numbers in C++

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Chappell Hill, Texas
    Posts
    2,332
    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, 03: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, 06:38 PM