Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8555387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:06:41+00:00 2026-06-11T15:06:41+00:00

The following code outputs a string literal to a file with both anonymous and

  • 0

The following code outputs a string literal to a file with both anonymous and named streams:

#include <fstream>

using namespace std;

int main()
{
    ofstream("testfile") << "test" << endl;

    ofstream ofs ("testfile2");
    ofs << "test2" << endl;

    return 0;
}

As you can see from strace’s output, only the named stream works:

open("testfile", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
write(3, "0x400a91\n", 9)               = 9
close(3)                                = 0
open("testfile2", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
write(3, "test2\n", 6)                  = 6
close(3)                                = 0

Also, if you use std::string instead of a literal, it fails to compile.

Why is this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:06:42+00:00Added an answer on June 11, 2026 at 3:06 pm

    In C++03, the non-member operators that output character pointers and strings cannot be called with an rvalue, as they require an lvalue reference, so the member operator<<(const void *) is called. In C++11 this is solved by writing overloads that take an rvalue reference, but in C++03 you can work around it by calling a member function or member operator that returns an lvalue reference (note that non-const member functions can be called on rvalues):

    ofstream("testfile").write("", 0) << "test" << endl;
    

    You can easily write a manipulator to do this:

    std::ios_base& (*do_nothing)(std::ios_base&) ) {}
    
    ofstream("testfile") << do_nothing << "test" << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the following code #include <iostream> using namespace std; int main(){ char greeting[50] = goodmorning
The following code outputs Illegal seek: #include <stdio.h> #include <errno.h> #include <string.h> int main()
I have the following code which outputs an object to an XML file: using
I was surprised to find that the following code System.out.println("Character size:"+Character.SIZE/8); System.out.println("String size:"+"a".getBytes().length); outputs
I build a CodeCompileUnit and the following code outputs a C# source code file,
I am using Jena 2.6.4. The following code String v = Parnell Square East;
Rather than outputting the expected string of Bar , the following code outputs what
I have the following code: <%= Html.Encode(String.Format({0:g}, item.startDate)) %> It outputs something like 01/01/2011
I have the following code: class TestClass { public: TestClass(){}; std::string GetTestString() { return
The following code outputs an empty string. The cause is the ó in $text,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.