Dudearoo
Useing Code::Blocks

Hey you guys!
ive got a problem with a 'test' program i have made for saveing strings.
heres the code:

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip>
#include <windows.h>
#include <fstream>
using namespace std;

int main()
{

    ofstream myfile ("savefile.txt", ios::ate | ios::in | ios::app | ios::trunc);

    string stringsaveT;
    string question;

    if (myfile.is_open())
    {
        cout << "This is a .txt file save test.\n please type in a string of characters, and close the program\n once you open the program\n please type in open. " << endl;
        cout << "" << endl;
        cin >> question;
        if (question == "open")
        {

        }
        else if (question == "print")
        {
            cout << "  Please Type the string you want to save." << endl;
            cin >> stringsaveT;
            myfile << "  "<< stringsaveT <<" ";
        }



        myfile.close();
    }

    cout << "Last Cout! program reverts here first? and closes!" << endl;
    return 0;
}

weird! output is just the last cout. ok, so can you guys help me figure out why the file is not opening?
Thanks!
this is only part of it i just want to see if i can open it first.
:)
(Oh by the way all my programs have as default those #include files. i only use half mostly.)

Recommended Answers

All 3 Replies

Line 15. Your ios flags don't make a lot of sense. In particular "ios::app" and "ios::trunc" being used together seems to cause the problem. Delete one or the other. I can't think of any reason you'd want them both.

Look at these flags and the descriptions, figure out what you want to do, then pick the appropriate flags. My guess is you want to get rid of "ios::trunc".

http://www.cplusplus.com/reference/iostream/fstream/open/

is better to put as your ofstrema flag the ios::out
and you could do it simply:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

int main(){
    ofstream out("text12.txt", ios::out);
    cout<<"Insert string: ";
    string a;
    cin>>a;
    out<<a;
    return (0);
}

Thanks!!!!!!!! it works.... for all my question's ill start putting the links i got the info from.
Thanks Lucaci Andrew and VernonDozier, up a Reputation point for the both of you!
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.