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. how to do write to a file with a std::string

how to do write to a file with a std::string

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
4 Posts 4 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.
  • K Offline
    K Offline
    kfaday
    wrote on last edited by
    #1

    i've got this code FILE *tempf std::string s; arch=fopen(tempf,"r+"); cout <<"enter your name"; cin >>s; fprintf(arch,s); the last line doesn't compile. It says it can't convert from ...std::string.. to char * any ideas? thanks!

    M H D 3 Replies Last reply
    0
    • K kfaday

      i've got this code FILE *tempf std::string s; arch=fopen(tempf,"r+"); cout <<"enter your name"; cin >>s; fprintf(arch,s); the last line doesn't compile. It says it can't convert from ...std::string.. to char * any ideas? thanks!

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      s.c_str() Maxwell Chen

      1 Reply Last reply
      0
      • K kfaday

        i've got this code FILE *tempf std::string s; arch=fopen(tempf,"r+"); cout <<"enter your name"; cin >>s; fprintf(arch,s); the last line doesn't compile. It says it can't convert from ...std::string.. to char * any ideas? thanks!

        H Offline
        H Offline
        Henrik Stuart
        wrote on last edited by
        #3

        #include <iostream>
        #include <fstream>
        #include <string>

        std::string s;

        std::ofstream arch("filename");
        std::cout << "enter your name: ";
        std::getline(std::cin, s);

        arch << s;

        Reading the name by using "cin >> s;" will stop at the first whitespace character, so if you want to read the entire line std::getline is just perfect. std::getline strips the newline characters from the end (both \r and \n). Personally find mixing C and C++ I/O a tad on the ugly side. I'm fully aware that it can be a necessity when incorporating C++ code in older apps, but for new code you might as well stick to the iostream library. I haven't included any error checking in the above code (those are the things we love to leave as an exercise for the reader, no?). All it needs more, really, is a check on whether the file was actually opened and good for writing. Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])

        1 Reply Last reply
        0
        • K kfaday

          i've got this code FILE *tempf std::string s; arch=fopen(tempf,"r+"); cout <<"enter your name"; cin >>s; fprintf(arch,s); the last line doesn't compile. It says it can't convert from ...std::string.. to char * any ideas? thanks!

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          kfaday wrote: arch=fopen(tempf,"r+"); What is this? The first parameter to fopen() is a const char* filename, and the return value is a FILE*. Something like:

          tempf = fopen("myfile.txt", "r+");


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          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