1

I have searched through a lot of questions on this website which are pretty much the same, but nothing works for me. In the first place, let me tell you that I am using Code::Blocks and I am using Ubuntu. This is my code:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream file("code.out");

    string write;
    getline(cin, write);
    file << write << "\n";
    file.close();
}

Tried \n, tried \r\n (\r doesn't seem to do anything for me really). Oh and by the way, if you could also make it work with word-by-word reading that would be great. Thank you very much!

EDIT: Hey guys, I solved it. Thanks for the answers tho! I needed to add a ios::app after code.out!

  • Use [std::endl](http://en.cppreference.com/w/cpp/io/manip/endl). – Marc Claesen Feb 12 '14 at 21:42
  • Try adding a flush at the end `file << std::flush();` and remove `file.close()` – andre Feb 12 '14 at 21:43
  • @MarcClaesen, it doesn't work. Should I try to remove using namespace std; and put everything in std::? – ijustwantedtosayhi Feb 12 '14 at 21:44
  • @ijustwantedtosayhi No, that won't help anything. – BWG Feb 12 '14 at 21:45
  • @andre, no matching function to call to flush? – ijustwantedtosayhi Feb 12 '14 at 21:49
  • For me the code does generate a newline. However, I must change `fstream` to `ofstream`. Compiled on visual studio windows. – BWG Feb 12 '14 at 21:52
  • @ijustwantedtosayhi - Get to know your tools. 1) Ubuntu is a flavor of the Linux OS. It is not a C++ compiler. 2) Codeblocks is an IDE, it is not a C++ compiler. The C++ compiler you're using is more than likely g++. – PaulMcKenzie Feb 12 '14 at 21:53
  • @ijustwantedtosayhi - You forgot to #include . That code is not guaranteed to compile due to that missing header. – PaulMcKenzie Feb 12 '14 at 21:53
  • @BWG, changed my code to ofstream, still the same. Maybe it's a problem I have with ubuntu? – ijustwantedtosayhi Feb 12 '14 at 21:55
  • Are you sure you are looking at a correct file? I once spent 2 hours trying to make bmp writer, then realized that I was not even in the right directory where the file was being generated. – BWG Feb 12 '14 at 21:56
  • @PaulMcKenzie, it did compile without the header. I included it, nothing happened. – ijustwantedtosayhi Feb 12 '14 at 21:59
  • @BWG, I am looking at the correct file. The text in the file always replaces the old text I wrote, it doesn't generate a new line. – ijustwantedtosayhi Feb 12 '14 at 21:59
  • @ijustwantedtosayhi - The issue is not whether it compiled. The issue is that there is no guarantee it will compile on another compiler or another version of the compiler. You must #include all of the necessary headers, regardless, and one of those headers is – PaulMcKenzie Feb 12 '14 at 22:01
  • @ijustwantedtosayhi Perhaps you could do something like `file << write << "\n" << "test\n"`? To see if the test after `write` writes? – BWG Feb 12 '14 at 22:01
  • It does. Wrote it 1 time: it replaced the first line and added a "test". wrote it second time: it replaced the first line i added last time and added that "test". – ijustwantedtosayhi Feb 12 '14 at 22:03
  • @ijustwantedtosayhi - can you write 2 lines of text? Maybe your editor or whatever you're using to view the file is at fault? – PaulMcKenzie Feb 12 '14 at 22:06

3 Answers3

2

Should you be using ofstream. http://www.cplusplus.com/reference/fstream/ofstream/

Then check that it has opened.

Then check you have read some data - debugger is handy for that

EDIT

You need

 ofstream file("code.out", ios::out | ios::app)
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

Try this code:

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

using namespace std;

int main()
{
    fstream file("code.out", std::fstream::out);

    string write;
    getline(cin, write);
    file << write << '\n';
    file.close();
}

Explicitly passing the std::fstream::out through the constructor got it to behave correctly for me and produced the newline.

Edit:

Note for future reference, my solution produces the newline but this will overwrite data currently found in the file. Ed Heal has code for appending to a file in his answer.

Adding

std::fstream::app

to my code would then mimic Ed Helms solution. Please mark his answer if appending functionality is actually what you needed. This answer will be for others who have a similar newline issue who want to overwrite the file.

Dean Knight
  • 660
  • 6
  • 17
  • The OP has already specified in the comments that this solution doesn't work for his needs. What he needed to do was *append* data to the end of the file and therefore needed the openmode `std::ios_base::app`. – David G Feb 12 '14 at 22:29
  • The question did not mention appending data whatsoever. He had trouble getting the newline to show up and this solution specifically solves that problem. I see how the ios::app helped him in comments above, but that does warrant this type of comment on all other answers in the question. This could help someone else down the road who is having a similar issue and may want this type of functionality. And that, is the purpose of StackExchange--future reference. – Dean Knight Feb 12 '14 at 22:45
  • 1
    I agree, the question was ambiguous as to the OP's real problem. And I appreciate that you defended your post. In order for me to revoke my downvote, you have to make an edit to your post (please do that). BTW, `std::endl` doesn't manage newline translation issues, all it does is insert the `'\n'` character and flush the stream. – David G Feb 12 '14 at 22:50
-1

I also had that problem once.

Try using ofstream instead of fstream. Maby that'll help, because I used that too.

MazzMan
  • 815
  • 1
  • 9
  • 15
  • The OP has already specified in the comments that this solution doesn't work for his needs. What he needed to do was *append* data to the end of the file and therefore needed the openmode `std::ios_base::app`. – David G Feb 12 '14 at 22:33