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

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    File Text
Click For Summary

Discussion Overview

The discussion revolves around the challenge of efficiently trimming white spaces from a text file in C++. Participants are exploring methods to locate the end of the file, remove trailing and leading white spaces, and handle new lines during file reading and writing.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant is attempting to read a text file character by character to remove extra white spaces but is struggling to locate the end of the file and the beginnings of new lines.
  • Another participant requests code to better understand the issue and notes the lack of clarity regarding the programming language being used.
  • A participant suggests that checking for the end of the file using eof() is not appropriate after attempting to read characters, as it indicates no more data is available.
  • Concerns are raised about the logic of a specific code snippet provided by the original poster, questioning its effectiveness and clarity.
  • A participant expresses curiosity about the original poster's programming experience in C++, implying a potential gap in knowledge.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the best approach to the problem, and there are competing views regarding the logic and effectiveness of the proposed code snippets.

Contextual Notes

There are limitations in the original poster's code logic and assumptions about file reading methods that remain unresolved. The discussion also highlights a potential lack of clarity in the programming language context.

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?
 

Similar threads

Replies
1
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
1
Views
2K
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K