How to write to a file without deleting information in it

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sai manoj bandi
    New Member
    • Sep 2010
    • 4

    #1

    How to write to a file without deleting information in it

    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...p lease reply.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    either move the output pointer to the end of the file before writing or use ios::ate to do it as you open.

    Comment

    • sai manoj bandi
      New Member
      • Sep 2010
      • 4

      #3
      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??

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        No but you could post the code you are using.

        Comment

        • sai manoj bandi
          New Member
          • Sep 2010
          • 4

          #5
          Code:
          #include<iostream>
          #include<fstream>
          using namespace std;
          int main()
          {
          fstream f;
          f.open("sai",ios::out|ios::ate);
          f<<"shskkhdljd;kdkd";
          f.close();
          fstream f1;
          f1.open("sai",ios::out|ios::ate);
          f1.seekp(4);
          f1<<"sai";
          f1.close();
          return 0;
          }

          here actually the old record should remain,but even on using ios::ate,it is getting deleted..
          Last edited by Banfa; Sep 26 '10, 10:42 AM. Reason: Added [code] ... [/code] tags round the code, please use them in future.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Instead of "ios::out|ios:: ate" try "ios_base:: out"

            Comment

            • sai manoj bandi
              New Member
              • Sep 2010
              • 4

              #7
              i am using ubuntu os(terminal) for compiling my programs.....wh en 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??

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                That is because ios::nocreate and for that matter ios_base::nocre ate are not part of the standard.

                Comment

                Working...