Blank spaces removal in a file

Discussion in 'C Programming' started by pushpakulkar@gmail.com, May 2, 2009.

  1. Guest

    Hi all,

    I have a file in which I have several blank spaces. I would like to
    remove all these blank spaces from the file.
    What is the most efficient way of doing it using minimal storage.
    Other than copying to some other file is
    there any other good way of doing it.

    Thanks for your time.

    Regards,
    Pushpa
     
    , May 2, 2009
    #1
    1. Advertising

  2. Lew Pitcher Guest

    On May 2, 2009 13:43, in comp.lang.c,
    () wrote:

    > Hi all,
    >
    > I have a file in which I have several blank spaces. I would like to
    > remove all these blank spaces from the file.
    > What is the most efficient way of doing it using minimal storage.
    > Other than copying to some other file is
    > there any other good way of doing it.


    The C language doesn't provide a dedicated operation for "removal of spaces
    from a file".

    The closest you can get, in C, is to write your own program that selectively
    copies characters from an input file to an output file. This would be a
    trivial exercise, on par with the exercises from an "Introduction to the C
    language" course.

    HTH
    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, May 2, 2009
    #2
    1. Advertising

  3. Eric Sosman Guest

    wrote:
    > Hi all,
    >
    > I have a file in which I have several blank spaces. I would like to
    > remove all these blank spaces from the file.
    > What is the most efficient way of doing it using minimal storage.
    > Other than copying to some other file is
    > there any other good way of doing it.


    Copying (and omitting the blanks) is probably the most
    efficient.

    If you insist on not copying the file, I think you're
    out of luck. The result will be shorter than the original,
    and C has no way to shorten an existing file (other than to
    length zero). You *could* overwrite the blanks with "ignore
    me" bytes, or squeeze them out and write "ignore me" bytes
    at the end, but that doesn't seem any better than just ignoring
    the blanks in the first place.

    If you're willing to go beyond C's I/O facilities you
    might find platform-specific ways to shorten a file. If so,
    you could "slide" bytes toward the beginning of the file to
    "squeeze out" the blanks, and then use the shortening method
    to chop off the leftover junk at the end. I expect this would
    be far less efficient than the copy-with-filter approach.

    --
    Eric Sosman
    lid
     
    Eric Sosman, May 2, 2009
    #3
  4. <> wrote:
    >I have a file in which I have several blank spaces. I would like to
    >remove all these blank spaces from the file.
    >What is the most efficient way of doing it using minimal storage.


    If you want to do it a character at a time, all in place, I think the
    only thing missing from standard C is some kind of file truncate call.

    You could keep track of the "input" and "output" positions in the file,
    and read and write characters from and to there (of course, only writing
    and moving the output position if the character isn't a space.)

    The resultant file needs to be shorter than the original, though, so
    you'll need to truncate the file in some system-dependent way. (POSIX
    has the ftruncate() function, for example.)

    >Other than copying to some other file is there any other good way of
    >doing it.


    Well, I wouldn't call my above-described method as "good", and
    definitely not "most efficient". It's "slow" and "ugly" and
    "fail-dangerous". But it does get closer to "minimal storage".

    Using the temp file might be the best way, if it's possible with your
    constraints.

    -Beej
     
    Beej Jorgensen, May 2, 2009
    #4
  5. Eric Sosman <> writes:

    > wrote:
    >> Hi all,
    >>
    >> I have a file in which I have several blank spaces. I would like to
    >> remove all these blank spaces from the file.
    >> What is the most efficient way of doing it using minimal storage.
    >> Other than copying to some other file is
    >> there any other good way of doing it.

    >
    > Copying (and omitting the blanks) is probably the most
    > efficient.
    >
    > If you insist on not copying the file, I think you're
    > out of luck. The result will be shorter than the original,
    > and C has no way to shorten an existing file (other than to
    > length zero). You *could* overwrite the blanks with "ignore
    > me" bytes, or squeeze them out and write "ignore me" bytes
    > at the end, but that doesn't seem any better than just ignoring
    > the blanks in the first place.
    >
    > If you're willing to go beyond C's I/O facilities you
    > might find platform-specific ways to shorten a file. If so,
    > you could "slide" bytes toward the beginning of the file to
    > "squeeze out" the blanks, and then use the shortening method
    > to chop off the leftover junk at the end. I expect this would
    > be far less efficient than the copy-with-filter approach.


    If the "slide" is done using something like Unix's mmap, I think this
    could be a lot more efficient. In this case the "chop off" can be done
    with ftruncate. In a simple test, this was about 2-3 times faster than
    a trivial getchar/putchar loop to copy the file.
     
    Nate Eldredge, May 2, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Octopus0
    Replies:
    2
    Views:
    641
    dorayme
    Mar 30, 2006
  2. Ruben
    Replies:
    6
    Views:
    601
    Carlos Ribeiro
    Sep 13, 2004
  3. John B. Matthews
    Replies:
    4
    Views:
    671
    John B. Matthews
    Sep 12, 2008
  4. Roedy Green
    Replies:
    3
    Views:
    627
  5. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,611
    Jan C. Faerber
    Sep 10, 2009
Loading...

Share This Page