Blank spaces removal in a file

P

pushpakulkar

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
 
L

Lew Pitcher

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

Eric Sosman

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

Beej Jorgensen

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
 
N

Nate Eldredge

Eric Sosman said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top