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

  • Thread starter magnifik
  • Start date
  • Tags
    File Text
In summary, the speaker is seeking help with locating the end of a text file, deleting extra white spaces, and finding the beginning of a new line. They are using instream and outstream and C strings, but are having trouble with their code. They are advised to clarify their programming language and to avoid reading characters after reaching the end of the file. It is also suggested that they have prior experience with programming in C or C++.
  • #1
magnifik
360
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
  • #2
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).
 
  • #3
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
 
  • #4
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.
 
  • #5
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?
 

1. How can I determine the end of a text file?

The end of a text file can be determined by using the "seek" function in most programming languages. This function allows you to move the file pointer to a specific location within the file, and by seeking to the end of the file, you can determine its size and location.

2. Is there a specific character or sequence of characters that indicates the end of a text file?

No, there is no universal character or sequence of characters that indicates the end of a text file. Some programming languages may use a specific character, such as the "EOF" (end of file) character, but this is not a standard and may vary between languages.

3. Can I use the file size to determine the end of a text file?

Yes, the file size can be used to determine the end of a text file. However, this method may not be reliable as the file size can be affected by factors such as file compression or encoding. It is recommended to use the "seek" function instead to accurately locate the end of a text file.

4. How can I locate the end of a text file in a large file?

In large files, locating the end of a text file can be a time-consuming process. One way to speed up this process is to use the "seek" function to move the file pointer in chunks rather than seeking to the end of the file at once. This can help save time and resources.

5. Can I determine the end of a text file without opening it?

No, in order to locate the end of a text file, you need to open the file and access its contents. However, you can use the "stat" function in some programming languages to retrieve information about a file, such as its size, without opening it. This can be useful in certain situations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
Replies
4
Views
929
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
22
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Computing and Technology
Replies
24
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top