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. is this the correct/best way to set file size? [modified]

is this the correct/best way to set file size? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
linuxquestion
15 Posts 3 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 George_George

    Thank you DavidCrow! What do you mean SetEndOfFile()? Is there a method called SetEndOfFile() in C? regards, George

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

    George_George wrote:

    Is there a method called SetEndOfFile() in C?

    There's a function called SetEndOfFile().


    "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

    "Judge not by the eye but by the heart." - Native American Proverb

    G 1 Reply Last reply
    0
    • D David Crow

      George_George wrote:

      Is there a method called SetEndOfFile() in C?

      There's a function called SetEndOfFile().


      "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

      "Judge not by the eye but by the heart." - Native American Proverb

      G Offline
      G Offline
      George_George
      wrote on last edited by
      #5

      Thank you DavidCrow! Why my method is better than SetEndOfFile()? regards, George

      D 1 Reply Last reply
      0
      • G George_George

        Thank you DavidCrow! Why my method is better than SetEndOfFile()? regards, George

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

        George_George wrote:

        Why my method is better than SetEndOfFile()?

        You wanted portability, didn't you?


        "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

        "Judge not by the eye but by the heart." - Native American Proverb

        G 1 Reply Last reply
        0
        • G George_George

          Hello everyone, I have verified that the following approach works to set the size of a file (newly created file) to be 100 bytes, but I am not sure whether it is the correct/best way to have a maximum portability (I need to write code on both Windows and Linux). Could anyone give me any comments? #include "fcntl.h" #include "sys/types.h" #include "sys/stat.h" #include "io.h" #include "stdio.h" int main() { FILE* file = fopen ("foo123", "w+"); fseek (file, 99, SEEK_SET); fprintf (file, "x"); fclose (file); return 0; } thanks in advance, George -- modified at 6:11 Monday 17th July, 2006

          E Offline
          E Offline
          earl
          wrote on last edited by
          #7

          There's two ways to get portability; the first is to use a common API and test it enough to make sure it works the same way on both systems; the second is to do such:

          int main(int argc, char** argv)
          {
          #if defined(WINDOWS)
          //put windows code here

          #elif defined(LINUX)
          //code that does the same thing in linux
          #endif

          return 0;
          }

          The former tends to make for much more readable code... at least IMO. earl

          G 1 Reply Last reply
          0
          • D David Crow

            George_George wrote:

            Why my method is better than SetEndOfFile()?

            You wanted portability, didn't you?


            "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

            "Judge not by the eye but by the heart." - Native American Proverb

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #8

            Thank you DavidCrow! Yes, I mean why SetEndOfFile() has worse portability? regards, George

            D 1 Reply Last reply
            0
            • E earl

              There's two ways to get portability; the first is to use a common API and test it enough to make sure it works the same way on both systems; the second is to do such:

              int main(int argc, char** argv)
              {
              #if defined(WINDOWS)
              //put windows code here

              #elif defined(LINUX)
              //code that does the same thing in linux
              #endif

              return 0;
              }

              The former tends to make for much more readable code... at least IMO. earl

              G Offline
              G Offline
              George_George
              wrote on last edited by
              #9

              Thank you earl! Your sample is only the general principle to develop portable code. In my case, to set the length of a file, is my method showed above has good portability? Do you have any better ideas or any comments? regards, George

              E 1 Reply Last reply
              0
              • G George_George

                Thank you DavidCrow! Yes, I mean why SetEndOfFile() has worse portability? regards, George

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

                George_George wrote:

                ...why SetEndOfFile() has worse portability?

                It's only for a Windows platform.


                "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                "Judge not by the eye but by the heart." - Native American Proverb

                G 1 Reply Last reply
                0
                • G George_George

                  Thank you earl! Your sample is only the general principle to develop portable code. In my case, to set the length of a file, is my method showed above has good portability? Do you have any better ideas or any comments? regards, George

                  E Offline
                  E Offline
                  earl
                  wrote on last edited by
                  #11

                  Why do you need the file to be 100b? I imagine your example should work, unless there are examples of sparse filesystems in use? I doubt it, but I don't keep up with the various file systems in use under linux. Alternatively, you could just fwrite 100b of zeroes...

                  unsigned char buff[100];
                  memset(buff, 0x0, 100 * sizeof(unsigned char));

                  fwrite(buff, sizeof(unsigned char), 100, fp);

                  earl

                  G 1 Reply Last reply
                  0
                  • D David Crow

                    George_George wrote:

                    ...why SetEndOfFile() has worse portability?

                    It's only for a Windows platform.


                    "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                    "Judge not by the eye but by the heart." - Native American Proverb

                    G Offline
                    G Offline
                    George_George
                    wrote on last edited by
                    #12

                    Thank you DavidCrow! regards, George

                    1 Reply Last reply
                    0
                    • E earl

                      Why do you need the file to be 100b? I imagine your example should work, unless there are examples of sparse filesystems in use? I doubt it, but I don't keep up with the various file systems in use under linux. Alternatively, you could just fwrite 100b of zeroes...

                      unsigned char buff[100];
                      memset(buff, 0x0, 100 * sizeof(unsigned char));

                      fwrite(buff, sizeof(unsigned char), 100, fp);

                      earl

                      G Offline
                      G Offline
                      George_George
                      wrote on last edited by
                      #13

                      Thank you earl! Your method is using more memory compared with my method -- 100 more bytes for the local char array. Agree? regards, George -- modified at 4:28 Wednesday 19th July, 2006

                      E 1 Reply Last reply
                      0
                      • G George_George

                        Thank you earl! Your method is using more memory compared with my method -- 100 more bytes for the local char array. Agree? regards, George -- modified at 4:28 Wednesday 19th July, 2006

                        E Offline
                        E Offline
                        earl
                        wrote on last edited by
                        #14

                        Sure, but who cares? As written it'll be allocated on the stack and disappear after the function is finished. You also could call fwrite 100 times on a single byte, if you're working in a space where 100b matters. earl

                        G 1 Reply Last reply
                        0
                        • E earl

                          Sure, but who cares? As written it'll be allocated on the stack and disappear after the function is finished. You also could call fwrite 100 times on a single byte, if you're working in a space where 100b matters. earl

                          G Offline
                          G Offline
                          George_George
                          wrote on last edited by
                          #15

                          Thank you earl! regards, George

                          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