user3150762
user3150762

Reputation: 57

Writing to a file in C++

I'm trying to make a program that will get the user input of a new file name, create the file, and write to it. It works but it will only write the first word of the string to the file. How can i get it to write the full string? thanks.

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
for (;;)
{
     char  *myFile = " "; 
     string f = " ";
     string w = " ";
     cout <<"What is the name of the file you would like to write to? " <<endl;
     cin >>f;
     ofstream myStream(f,ios_base::ate|ios_base::out);

     cout <<"What would you like to write to " <<f <<" ? ";
     cin >>w;

     myStream <<w;

  if (myStream.bad())
  {
     myStream <<"A serious error has occured.";
     myStream.close();
     break;
  }
}

}

Upvotes: 1

Views: 104

Answers (3)

DamnnnSure
DamnnnSure

Reputation: 21

cin<<w; cin would stop consuming input character when it encounter whitespace tab and other unseeable characters.

you should probably use std::getline() instead. take a look at this page for ref. http://en.cppreference.com/w/cpp/string/basic_string/getline

Or you can use manipulator to not skip whitespace.

Upvotes: 0

M. K. Hunter
M. K. Hunter

Reputation: 1828

According to this post, you should consult this reference to use a method like getline().

Also, when you are writing out I recommend that you flush the output (cout.flush()) before ending the program, especially in this case, since I presume you are ending the program with a ctrl-C break.

In formulating a suggestion, I will read data into char*, and convert them to "string" in case you will use them elsewhere in your program.

I tested this code in MS Visual C++ Express.

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    for (;;)
    {
        char  *myFile = new char[200]; // modified this line

        //added this line
        char *myInput = new char[200];

        string f = " ";
        string w = " ";
        cout << "What is the name of the file you would like to write to? " << endl;
        cin.getline(myFile, 200);//modified this line
        f = (myFile);//added this line
        cin.clear(); //added this line
        ofstream myStream(f, ios_base::ate | ios_base::out);

        cout << "What would you like to write to " << f << " ? ";

        cin.getline(myInput, 200);//edited this line

        w = string(myInput);//added this line

        myStream << w;
        myStream.flush();//added this line

        if (myStream.bad())
        {
            myStream << "A serious error has occured.";
            myStream.close();
            break;
        }

        delete myFile;
        delete myInput;
    }

}

Upvotes: 1

David G
David G

Reputation: 96810

You have to use std::getline() to read the entire line:

std::getline(std::cin >> std::ws, w);

The >> std::ws part gets rid of leading whitespace which is needed because the newline left in the stream from the previous extraction will prevent std::getline() from fully consuming the input.

When inserting data into the stream you need to make sure it gets flushed (because, as the other answer said, you're probably using Ctrl+C to terminate the program and you may not see output during the program run). You can use the std::flush manipulator to flush the output:

myStream << w << std::flush;

Upvotes: 0

Related Questions