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. Always write to top of file

Always write to top of file

Scheduled Pinned Locked Moved C / C++ / MFC
help
12 Posts 7 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.
  • G Offline
    G Offline
    Grahamfff
    wrote on last edited by
    #1

    Its too slow when I open -> write -> close a file (see code below). Are they a way of opening the file and always writing at the top of the file; e.g. I do not want to Append text, just write the same 100 lines of text each time at the top of the file.

    #include <stdio.h>
    FILE * pFile;
    /* open file and check if was successful */
    if ((pFile = fopen("myfile.txt", "w")) == NULL)
    {
    /* couldn't open file; do some error handling if u want */
    }
    else
    {
    /* do s.th. */
    /* close file */
    fclose(pFile);
    }

    Waiting in hope!

    grahamfff

    L E D A 4 Replies Last reply
    0
    • G Grahamfff

      Its too slow when I open -> write -> close a file (see code below). Are they a way of opening the file and always writing at the top of the file; e.g. I do not want to Append text, just write the same 100 lines of text each time at the top of the file.

      #include <stdio.h>
      FILE * pFile;
      /* open file and check if was successful */
      if ((pFile = fopen("myfile.txt", "w")) == NULL)
      {
      /* couldn't open file; do some error handling if u want */
      }
      else
      {
      /* do s.th. */
      /* close file */
      fclose(pFile);
      }

      Waiting in hope!

      grahamfff

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      No, there is no way, since all the data that you write would overwrite whatever is already there. You would need to hold the data in memory, and rewrite all of it every time.

      Veni, vidi, abiit domum

      G 1 Reply Last reply
      0
      • G Grahamfff

        Its too slow when I open -> write -> close a file (see code below). Are they a way of opening the file and always writing at the top of the file; e.g. I do not want to Append text, just write the same 100 lines of text each time at the top of the file.

        #include <stdio.h>
        FILE * pFile;
        /* open file and check if was successful */
        if ((pFile = fopen("myfile.txt", "w")) == NULL)
        {
        /* couldn't open file; do some error handling if u want */
        }
        else
        {
        /* do s.th. */
        /* close file */
        fclose(pFile);
        }

        Waiting in hope!

        grahamfff

        E Offline
        E Offline
        Eugen Podsypalnikov
        wrote on last edited by
        #3

        // I do not want to Append text, just write the same 100 lines of text each time at the top of the file. Any appending in the file context is more rapid than an inserting. Of course, you could try to append the reversed content to be read later as reversed as well... :)

        They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

        1 Reply Last reply
        0
        • L Lost User

          No, there is no way, since all the data that you write would overwrite whatever is already there. You would need to hold the data in memory, and rewrite all of it every time.

          Veni, vidi, abiit domum

          G Offline
          G Offline
          Grahamfff
          wrote on last edited by
          #4

          I just want to append to the TOP of the file each time. Its too slow doing an open -> write -> close. In a 20 msec loop and sometimes two files need to hav data written to them. No timing problems with the append, but this could result in large files if I do not overwirie.

          grahamfff

          L 1 Reply Last reply
          0
          • G Grahamfff

            I just want to append to the TOP of the file each time. Its too slow doing an open -> write -> close. In a 20 msec loop and sometimes two files need to hav data written to them. No timing problems with the append, but this could result in large files if I do not overwirie.

            grahamfff

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Grahamfff wrote:

            I just want to append to the TOP of the file each time.

            As I said before, you cannot do it; the system just does not work that way. Perhaps it would be better if you explained why you want to do this.

            Veni, vidi, abiit domum

            G 1 Reply Last reply
            0
            • L Lost User

              Grahamfff wrote:

              I just want to append to the TOP of the file each time.

              As I said before, you cannot do it; the system just does not work that way. Perhaps it would be better if you explained why you want to do this.

              Veni, vidi, abiit domum

              G Offline
              G Offline
              Grahamfff
              wrote on last edited by
              #6

              In a 20 msec loop and sometimes two files need to have data written to them and the open -> write -> close takes about 8-10 msec each and sometimes get an overrun.

              grahamfff

              L E 2 Replies Last reply
              0
              • G Grahamfff

                In a 20 msec loop and sometimes two files need to have data written to them and the open -> write -> close takes about 8-10 msec each and sometimes get an overrun.

                grahamfff

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Then you need to work on your system design; you cannot change the laws of physics.

                Veni, vidi, abiit domum

                1 Reply Last reply
                0
                • G Grahamfff

                  In a 20 msec loop and sometimes two files need to have data written to them and the open -> write -> close takes about 8-10 msec each and sometimes get an overrun.

                  grahamfff

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

                  As Eugen alludes to, a time-critical loop is not the place to muck about. Write the data to the end of the file then deal with the order later on. Furthermore, I assume that you're dealing with different files each time through the loop. If not, then again, don't muck about. Open the file before the loop and close it again afterwards.

                  1 Reply Last reply
                  0
                  • G Grahamfff

                    Its too slow when I open -> write -> close a file (see code below). Are they a way of opening the file and always writing at the top of the file; e.g. I do not want to Append text, just write the same 100 lines of text each time at the top of the file.

                    #include <stdio.h>
                    FILE * pFile;
                    /* open file and check if was successful */
                    if ((pFile = fopen("myfile.txt", "w")) == NULL)
                    {
                    /* couldn't open file; do some error handling if u want */
                    }
                    else
                    {
                    /* do s.th. */
                    /* close file */
                    fclose(pFile);
                    }

                    Waiting in hope!

                    grahamfff

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

                    Grahamfff wrote:

                    Are they a way of opening the file and always writing at the top of the file;

                    Do you mean having the newest data at the beginning of the file (rather than at the end)?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "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

                    "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                    G 1 Reply Last reply
                    0
                    • D David Crow

                      Grahamfff wrote:

                      Are they a way of opening the file and always writing at the top of the file;

                      Do you mean having the newest data at the beginning of the file (rather than at the end)?

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "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

                      "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                      G Offline
                      G Offline
                      Grahamfff
                      wrote on last edited by
                      #10

                      Yes that would do! They are recoverery data files, so periodically writing them. Just interested in the newest data.

                      grahamfff

                      S 1 Reply Last reply
                      0
                      • G Grahamfff

                        Its too slow when I open -> write -> close a file (see code below). Are they a way of opening the file and always writing at the top of the file; e.g. I do not want to Append text, just write the same 100 lines of text each time at the top of the file.

                        #include <stdio.h>
                        FILE * pFile;
                        /* open file and check if was successful */
                        if ((pFile = fopen("myfile.txt", "w")) == NULL)
                        {
                        /* couldn't open file; do some error handling if u want */
                        }
                        else
                        {
                        /* do s.th. */
                        /* close file */
                        fclose(pFile);
                        }

                        Waiting in hope!

                        grahamfff

                        A Offline
                        A Offline
                        Alan Balkany
                        wrote on last edited by
                        #11

                        Open the file as "read/update": fopen ("myfile.txt", "r+"), link: http://www.cplusplus.com/reference/cstdio/fopen/[^] The use fseek (SEEK_SET) to position the file pointer to the beginning of the file. Link: http://www.cplusplus.com/reference/cstdio/fseek/[^] You can then just overwrite 100 lines of text, without having to write the WHOLE file.

                        1 Reply Last reply
                        0
                        • G Grahamfff

                          Yes that would do! They are recoverery data files, so periodically writing them. Just interested in the newest data.

                          grahamfff

                          S Offline
                          S Offline
                          Stefan_Lang
                          wrote on last edited by
                          #12

                          Grahamfff wrote:

                          They are recoverery data files,

                          Now, finally, you are providing some useful info. First off, the file system is a bottleneck. The only way to speed up file access is use a different file system. But you probably don't have that choice. Second, repeatedly writing to hard disk in a fixed time loop every 20ms is an incredibly stupid idea, for various reasons - this is simply not the intended purpose of a file system! You should consider different storage media, depending an what is available to you, or you should consider to just wait longer between writes. I'm not an expert on this, but Flash drives, SRAM, or even USB may be more suitable. (Although writing to flash drives 50 times per second is a sure way to destroy them in short order...) Third, I can't think of any reason to write recovery info more often then once every couple of seconds: a recovery feature usually means the ability to restore a state that is difficult to reproduce, either because it is the result of interacting with a non-deterministic system (such as a human), or because there is no other way to reproduce the data (e. g. in very long range communication such as talking to mars rovers and the like). Either way, losing the data from as little a timespan as one second is meaningless! OTOH, if for whatever incomprehensible reason you really, really need that info stored every 0.02s flawlessly, then the file system simply is too slow to reliably do this - not to mention that trying it will slow your entire application to a crawl. As suggested above, use a different storage medium!

                          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                          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