By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

How to write to a file without deleting information in it

P: 4
using c++ in files:
when using ios::out mode for an old file,which is to be changed once again,(not only at the end),the old information in the file is getting deleted.what operation should we use to open and write into any old file without deleting the old information...please reply.
Sep 23 '10 #1
Share this Question
Share on Google+
7 Replies


Banfa
Expert Mod 5K+
P: 8,916
either move the output pointer to the end of the file before writing or use ios::ate to do it as you open.
Sep 23 '10 #2

P: 4
thanks for that...but even on using ios::ate operation,i am unable to add my data without getting the old data deleted..can u please give me a sample progrm for that??
Sep 24 '10 #3

Banfa
Expert Mod 5K+
P: 8,916
No but you could post the code you are using.
Sep 24 '10 #4

P: 4
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. int main()
  5. {
  6. fstream f;
  7. f.open("sai",ios::out|ios::ate);
  8. f<<"shskkhdljd;kdkd";
  9. f.close();
  10. fstream f1;
  11. f1.open("sai",ios::out|ios::ate);
  12. f1.seekp(4);
  13. f1<<"sai";
  14. f1.close();
  15. return 0;
  16. }

here actually the old record should remain,but even on using ios::ate,it is getting deleted..
Sep 26 '10 #5

Banfa
Expert Mod 5K+
P: 8,916
Instead of "ios::out|ios::ate" try "ios_base::out"
Sep 26 '10 #6

P: 4
i am using ubuntu os(terminal) for compiling my programs.....when i am using ios::nocreate to open my file...." error: ‘nocreate’ is not a member of ‘std::ios’" ...this error is getting displayed .is there any problem with my compiler??
Sep 28 '10 #7

Banfa
Expert Mod 5K+
P: 8,916
That is because ios::nocreate and for that matter ios_base::nocreate are not part of the standard.
Sep 28 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.