Blank spaces removal in a file

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

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.

Advertisements

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.

Advertisements

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.

<> 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.

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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!