Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to clean contents of a file

How to clean contents of a file

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PankajB
    wrote on last edited by
    #1

    Hi there. Lets say I have created a file as below...

    #include <stdio.h>

    int main ()
    {
    FILE * pFile;
    int n;
    char name [100];

    pFile = fopen ("myfile.txt","w");
    for (n=0 ; n<3 ; n++)
    {
    puts ("please, enter a name: ");
    gets (name);
    fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
    }
    fclose (pFile);

    return 0;
    }

    Now, my requirement is, each and every time I write a name into the file, i want to delete the previous name. So, at a time I want to have just one name in the file. Please suggest. Thanks & Regards PanB

    _ E D 3 Replies Last reply
    0
    • P PankajB

      Hi there. Lets say I have created a file as below...

      #include <stdio.h>

      int main ()
      {
      FILE * pFile;
      int n;
      char name [100];

      pFile = fopen ("myfile.txt","w");
      for (n=0 ; n<3 ; n++)
      {
      puts ("please, enter a name: ");
      gets (name);
      fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
      }
      fclose (pFile);

      return 0;
      }

      Now, my requirement is, each and every time I write a name into the file, i want to delete the previous name. So, at a time I want to have just one name in the file. Please suggest. Thanks & Regards PanB

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      It seems you are doing it the right way. Opening a file using fopen with "w" as the parameter creates an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. but since you are writing in the for loop the contents are written such that the file contains all the three names or names equal to the number of iterations

      You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

      P 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        It seems you are doing it the right way. Opening a file using fopen with "w" as the parameter creates an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. but since you are writing in the for loop the contents are written such that the file contains all the three names or names equal to the number of iterations

        You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

        P Offline
        P Offline
        PankajB
        wrote on last edited by
        #3

        What i want to do is, at one point I just want to have one entry in the file. If you run attached program, you will find three entries in the same.

        C _ 2 Replies Last reply
        0
        • P PankajB

          What i want to do is, at one point I just want to have one entry in the file. If you run attached program, you will find three entries in the same.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Instead of opening the file once at the begining, you could simply open it in your loop. Thus, each time you open it, the previous contents are deleted. Don't forget to close it afterwards.

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • P PankajB

            What i want to do is, at one point I just want to have one entry in the file. If you run attached program, you will find three entries in the same.

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            Why do you want the loop, if you want only 1 name in the file. And if you really want the loop, put the fopen and fclose inside the loop.

            «_Superman_» I love work. It gives me something to do between weekends.

            P 1 Reply Last reply
            0
            • _ _Superman_

              Why do you want the loop, if you want only 1 name in the file. And if you really want the loop, put the fopen and fclose inside the loop.

              «_Superman_» I love work. It gives me something to do between weekends.

              P Offline
              P Offline
              PankajB
              wrote on last edited by
              #6

              The code was just a sample code... I want to update some information lets say for more than 1000 times, so dont you think that will an overhead? I think better option will be deleting contents amd updating the same with the updated information. Please suggest.

              _ 1 Reply Last reply
              0
              • P PankajB

                The code was just a sample code... I want to update some information lets say for more than 1000 times, so dont you think that will an overhead? I think better option will be deleting contents amd updating the same with the updated information. Please suggest.

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #7

                I'm not very sure of the performance issues here. For overwriting the file you will need to first bring the pointer back to the begining. You can use rewind() for that. After writing the new data, if the newer data is of a smaller size than the earlier, you will need to make the file smaller. You can use _chsize() for that. You could probably try to either use fopen/fclose inside the loop or rewind/_chsize inside the loop, loop for 1000 or more times and compare the performance.

                «_Superman_» I love work. It gives me something to do between weekends.

                1 Reply Last reply
                0
                • P PankajB

                  Hi there. Lets say I have created a file as below...

                  #include <stdio.h>

                  int main ()
                  {
                  FILE * pFile;
                  int n;
                  char name [100];

                  pFile = fopen ("myfile.txt","w");
                  for (n=0 ; n<3 ; n++)
                  {
                  puts ("please, enter a name: ");
                  gets (name);
                  fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
                  }
                  fclose (pFile);

                  return 0;
                  }

                  Now, my requirement is, each and every time I write a name into the file, i want to delete the previous name. So, at a time I want to have just one name in the file. Please suggest. Thanks & Regards PanB

                  E Offline
                  E Offline
                  Eytukan
                  wrote on last edited by
                  #8

                  PankajB wrote:

                  Now, my requirement is, each and every time I write a name into the file, i want to delete the previous name.

                  Little weird. So what do achieve by writing them into the file immediately? If you want to keep the last-entered text to be written, you can keep them in a temp buffer and write it into the file at the end. Otherwise if you want to keep up the requirement, I'd suggest the same as everybody there, something like

                  ofstream ofs;

                  while(your_input_loop)
                  {
                  ofs.open("txt",ios::out);
                  //Get input// write//
                  ofs.close();
                  }

                  As you show in your code, the control already waits for user inputs. So it doesn't make sense to count performance there.

                  He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

                  1 Reply Last reply
                  0
                  • P PankajB

                    Hi there. Lets say I have created a file as below...

                    #include <stdio.h>

                    int main ()
                    {
                    FILE * pFile;
                    int n;
                    char name [100];

                    pFile = fopen ("myfile.txt","w");
                    for (n=0 ; n<3 ; n++)
                    {
                    puts ("please, enter a name: ");
                    gets (name);
                    fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
                    }
                    fclose (pFile);

                    return 0;
                    }

                    Now, my requirement is, each and every time I write a name into the file, i want to delete the previous name. So, at a time I want to have just one name in the file. Please suggest. Thanks & Regards PanB

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    What about:

                    #include <stdio.h>

                    void main( void )
                    {
                    char name[100];

                    for (int n = 0; n < 3; n++)
                    {
                    puts("Please, enter a name: ");
                    gets(name);
                    }

                    FILE *pFile = fopen("myfile.txt", "w");
                    fprintf(pFile, "Name %d [%-10.10s]\n", n, name);
                    fclose(pFile);
                    }

                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups