Archived

This topic is now archived and is closed to further replies.

Deleting characters in a FILE*

This topic is 5150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What I want to do is write a small programm that converts a windows text file to a linux one. To do this I need to exchange windows'' end-of-line characters CRLF with linux'' LF. So I thought about loading the textfile in a FILE* and just deleting the CR in front of the LF - but there is no way to delete a character, is there? Overwriting yes, appending yes, but deleting...? Do I really have to open a second file for copying...? Or any other ideas? ------------------------------ There are only 10 kinds of people: those that understand binary and those that don''t.

Share this post


Link to post
Share on other sites
One approach for doing it without a temporary file:


  • Open the file for binary reading

  • Calculate the file''s length in characters

  • Allocate an array the same size as the length of the file, then read the characters into that array

  • Close the file, then re-open if for binary writing
    Iterate through the array, and write only the characters that you want

  • Close the file

  • Deallocate the array


Share this post


Link to post
Share on other sites
Thanks for your answer! My problem is that the file I''m parsing is quite large. I''d love to be able to just remove a couple of characters but if that''s not possible I guess it''s more practical to use a temporary file rather than an array...

------------------------------
There are only 10 kinds of people: those that understand binary and those that don''t.

Share this post


Link to post
Share on other sites
AFAIK there is no way to delete arbitrary characters in the middle of a file without rewriting the entire file. However, this type of file conversion where there is a specific substitution involved is where something like Perl comes in real handy. I don''t know it off the top of my head but I know that this type of conversion can be done with a single line perl command.

Evillive2
E-Mail

Share this post


Link to post
Share on other sites