writing to a file?

writing to a file?

Post by chrisx16x2008 on Sun Apr 12, 2009 11:16 pm
([msg=21668]see writing to a file?[/msg])

i am trying to write test to a file im using ofstream outfile
("thefile.txt", ios::app && ios::out | ios::in);
outfile.open("thefile.txt");
outfile << "a";
outfile.close();

i get no compile errors at all when i type cout << it works fine but i can never get it to put "a'" into a file. i have been trying at this for 2 days someone please help me out.
chrisx16x2008
New User
New User
 
Posts: 36
Joined: Tue Jan 13, 2009 9:53 pm
Blog: View Blog (0)


Re: writing to a file?

Post by mischief on Mon Apr 13, 2009 2:59 am
([msg=21683]see Re: writing to a file?[/msg])

you're opening the file twice, once in the constructor and once with the ofstream::open member. try fixing that :)
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)


Re: writing to a file?

Post by chrisx16x2008 on Mon Apr 13, 2009 11:25 pm
([msg=21746]see Re: writing to a file?[/msg])

ok so my new code. it writes to the file but everytime it opens it dumps the files. I was told a while back that ios::app was supposed to stop that by appended the contents to the new contents. Although as of right now its dumping the contents upon open.
Code: Select all
      
ofstream file;
file.open("test.txt", ios::app & ios::in | ios:: out);
if (file.is_open())
{
file << "a";
cout << "wrote to the file";
}
else
{
cout << "error reading file";
}
file.close();
chrisx16x2008
New User
New User
 
Posts: 36
Joined: Tue Jan 13, 2009 9:53 pm
Blog: View Blog (0)


Re: writing to a file?

Post by mischief on Tue Apr 14, 2009 12:53 am
([msg=21749]see Re: writing to a file?[/msg])

i think you have a problem with your flags. i don't think using bitwise AND is the proper way to set the flags, as far as i know only bitwise OR is supposed to work.

also, why are you using ios::in on an output stream?
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)


Re: writing to a file?

Post by chrisx16x2008 on Tue Apr 14, 2009 1:47 am
([msg=21750]see Re: writing to a file?[/msg])

yea i just realized i didnt need the ios::in but can u give me a website to learn about flags cuz i have no idea what they are.
chrisx16x2008
New User
New User
 
Posts: 36
Joined: Tue Jan 13, 2009 9:53 pm
Blog: View Blog (0)


Re: writing to a file?

Post by mischief on Tue Apr 14, 2009 5:45 am
([msg=21758]see Re: writing to a file?[/msg])

The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)


Re: writing to a file?

Post by chrisx16x2008 on Tue Apr 14, 2009 2:55 pm
([msg=21786]see Re: writing to a file?[/msg])

alright thanks alot for all your help mischief i got it working. All i had to do was use the | statement instead of the & and it worked.
chrisx16x2008
New User
New User
 
Posts: 36
Joined: Tue Jan 13, 2009 9:53 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron