Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12

    Deleting several chars in the end of a file


    I have a file and now I want to delete the last 120 chars. Do I have to make a temporary file and copy the whole file or is there some other methods to make file smaller?
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    The best, simplest, most reliable and easiest to debug way is to make a copy. I think there are other OS specific ways to shrink files in place, but it is trivial (unless, of course, you are at your limit on file space or the file is huge) to make a copy.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    So I'll make a copy. :)
  6. #4
  7. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    If I fseek() to a specified position in a file and then write, will chars be replaced or that's not right? :)
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    You will overwrite the data (the OS may actually make a copy of the file, but that is out of your hands), but I thought you wanted to truncate the file (make it shorter). You could try to write an EOF character, but I strongly suggest you not try that as you can screw the file up so that it can only be deleted. If you control the program that is reading the file, you can create your own 'EOF' character and use that to indicate that the file is done, but things get quite complicated quite quickly.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  10. #6
  11. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Yeah, If I could overwrite the last few characters I would not copy.
  12. #7
  13. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    You can't overwrite.
    If you do that, all chars which were not changed, change to some weird shapes...:)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    88
    Rep Power
    12
    Hi,

    fseek from the end 120 chars, and then there's a windows function to mare end of file there. I'll find it and post back. :)
  16. #9
  17. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Let me explain you specifically what I need. I want to continue writing at the specified char (fseek). I can actually do that, but the result is unuseful. :)

    And another question:

    if I write this:
    Code:
    int cutTheFile(long status){
        FILE *temp;
       
        //blabla
    }
    Will the *temp be created with a call to function and removed with the end or it will stay forever?
    Last edited by Loser; September 3rd, 2003 at 12:55 PM.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    88
    Rep Power
    12
    You can only access temp in that function, once out the function returns, it's destroyed. :)

    You can make FILE *temp; a global variable by declaring at top of application, but that's "bad programming habbits" (BS IMHO).

    Now you can always make the function FILE * aswell, and return temp at the end if you realyl want to. ;)

    Regards,
    Someonewhois

    P.S. If I misunderstood the question, please reask, and I'l, retry :p
  20. #11
  21. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Originally posted by someonewhois
    You can only access temp in that function, once out the function returns, it's destroyed. :)
    That's what I wanted to know. :)
  22. #12
  23. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Damn, I still have problems.

    I write to file with for loop and putc(); if I break the program so there's no EOF and no fclose(), the file is empty even if there was 256 chars written already. I open the file in "wb" mode.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    12
    Declaring variables global when they can just as easily be local IS a bad habit.

    The reason for this is that the variable will not be de-allocated until the end of the application, whereas local variables are allocated in the stack, and are removed after the function end (unless of course they are static etc...).

    Don't just put your own EOF in the file; but even if you did, it would not cause great harm, and NO hard to the file system or the OS, some lame ASCII file readers may have a problem with it though.

    Without going into filesystem specifics (whether done through the kernel, or through some low level code fo yoru own), it is better to just make a new copy of the file.
  26. #14
  27. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    But I have another file in the same program - i write to it with fprintf - and it works perfectly. Is there a way to write to a file immediately?
    Last edited by Loser; September 5th, 2003 at 01:22 PM.
  28. #15
  29. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Maybe fprintf? :)
    I think it's slow...
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo