Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Blank spaces removal in a file

Reply
Thread Tools

Blank spaces removal in a file

 
 
pushpakulkar@gmail.com
Guest
Posts: n/a
 
      05-02-2009
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
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      05-02-2009
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. ------


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      05-02-2009
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
 
Reply With Quote
 
Beej Jorgensen
Guest
Posts: n/a
 
      05-02-2009
<> 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

 
Reply With Quote
 
Nate Eldredge
Guest
Posts: n/a
 
      05-02-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
Not able to read blank lines and spaces on a small text file Ruben Python 6 09-13-2004 03:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57