Efficient Text File Trimming in C++: Locating and Removing White Spaces

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    File Text
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 3K views
magnifik
Messages
350
Reaction score
0
i am trying to locate the end of a text file and delete any extra white spaces following it, but i am having trouble trying to do this. is there an easy way to do this? right now i am reading the text in character by character and outputting the text to another outstream file. i can't seem to locate the end of the file and delete trailing blanks. also, i am having trouble locating the beginning of a new line in my outstream file. i also need to delete leading white spaces. any help would be appreciated.
 
Physics news on Phys.org
Well, it may help if you post some code.
First of all you are not telling us which programming language you are using, and second of all, I can think of dozens of ways to read and write files (about three of them in C++ alone, off the top of my head).
 
i am using instream & outstream and c strings.

for the end of the file part i would do some thing like
Code:
if (inf.eof())
{ outf << "\n";
if (isspace(c))
!inf.get(c);
}

doesn't work the way i wish it did.. i am having trouble actually finding the beginning and end of new lines
 
Why are you attempting to read characters after you've reached the end of the file? eof() returns true (actually a nonzero value) when there are no more data left in the input stream.