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

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    File Text
AI Thread Summary
The discussion focuses on challenges in trimming white spaces from a text file using C++. The user is struggling to locate the end of the file and remove trailing and leading white spaces while reading character by character. Suggestions include posting code for better assistance and clarifying the programming language being used, as well as addressing the misuse of the eof() function. There is confusion about reading characters after reaching the end of the file, indicating a need for a better understanding of file handling in C++. Overall, the conversation emphasizes the importance of proper coding practices and understanding file input/output operations.
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.
 
Apart from the fact that a statement like
!inf.get(c)​
does not really make sense on its own.

Please don't take offense, but I am curious: did you ever program (C(++)) before?
 
Back
Top