944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3156
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 24th, 2007
0

Help with indenting infile -> outfile paragraph...

Expand Post »
Hi,
I am writing a little program in C++ that is used to read a file which contains several paragraphs. The paragraphs are not indented and there are a blank line or sometimes two blank lines before the start of each paragraph.
I have written the program and can read and dump the file into a new file.
Aside from the basic reading of the file and closing the file, I have a while loop with a getline to read each line. I have put an If else statement into it so as I can indent the start of a new paragraph when there is one.
The problem that I am having is I do not know what to put into the "if()" statement to denote the end of blank lines so that I can indent some sppaces. I have searched, read and searched some more for some answers on my own and finally came here for a tip.

Please help me!
Thank you

Here is the While statement that I have going on so far:

C++ Syntax (Toggle Plain Text)
  1. while(inFile)
  2. {
  3. getline(inFile,inputStr);
  4.  
  5. if ()
  6. {
  7. outFile << " " << inputStr << endl;
  8. }
  9. else
  10. {
  11. outFile << inputStr << endl;
  12. }
  13. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R6er is offline Offline
15 posts
since Mar 2007
Mar 24th, 2007
0

Re: Help with indenting infile -> outfile paragraph...

I would use a flag to indicate the end of a paragraph -- the first blank line is the end of the paragraph. When the flag then indent the next non-blank line and reset the flag to false.

The while loop is better written like this
  1. while( getline(inFile,inputStr) )
  2. {
  3. if ( <flag is set> )
  4. {
  5. outFile << " " << inputStr << endl;
  6. <set flag to false>
  7. }
  8. else
  9. {
  10. outFile << inputStr << endl;
  11. <if inputStr is an empty string set flag to true >
  12. }
  13. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Mar 24th, 2007
0

Re: Help with indenting infile -> outfile paragraph...

Thank you for the quick reply.

"first blank line is the end of the paragraph"
Between some of the paragraphs there are two blank lines so Im going to need some type of syntax to note where the begining of a new line of text is, that is something that I cannot seem to find out how to do.


"<if inputStr is an empty string set flag to true >"
Sorry, I am not that advanced and do not know how to note if a
string is an empty one or not.

Thank you again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R6er is offline Offline
15 posts
since Mar 2007
Mar 25th, 2007
0

Re: Help with indenting infile -> outfile paragraph...

>>Sorry, I am not that advanced and do not know how to note if a string is an empty one or not.

a string is empty when any one of these conditions exist.
C++ Syntax (Toggle Plain Text)
  1. inputStr == "";
  2.  
  3. or
  4.  
  5. inputStr.length() == 0
  6.  
  7. or
  8. inputStr.size() == 0

as for multiple blank lines -- it doesn't matter how many blank lines there are, just set the flag to true each time a blank line is detected.
Last edited by Ancient Dragon; Mar 25th, 2007 at 12:12 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Mar 25th, 2007
0

Re: Help with indenting infile -> outfile paragraph...

Don't forget to consider the following situations:

• The blank lines may be composed of only spaces. You might need to write a trim function which first trims the lines read and then processes them.

• The first line can be a non blank requiring you to initialize the flag accordingly.

The trim function can be as simple as (not my function):
C++ Syntax (Toggle Plain Text)
  1. #include <string>
  2.  
  3. const std::string whiteSpaces( " \f\n\r\t\v" );
  4.  
  5. void trimRight( std::string& str,
  6. const std::string& trimChars = whiteSpaces )
  7. {
  8. std::string::size_type pos = str.find_last_not_of( trimChars );
  9. str.erase( pos + 1 );
  10.  
  11. }
  12.  
  13.  
  14. void trimLeft( std::string& str,
  15. const std::string& trimChars = whiteSpaces )
  16. {
  17. std::string::size_type pos = str.find_first_not_of( trimChars );
  18. str.erase( 0, pos );
  19. }
  20.  
  21.  
  22. void trim( std::string& str, const std::string& trimChars = whiteSpaces )
  23. {
  24. trimRight( str, trimChars );
  25. trimLeft( str, trimChars );
  26. }
Last edited by ~s.o.s~; Mar 25th, 2007 at 12:31 am.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Mar 25th, 2007
0

Re: Help with indenting infile -> outfile paragraph...

Thank you for taking the time to give me some hints here.
I have taken your tips into consideration and have formulated this here:

C++ Syntax (Toggle Plain Text)
  1. isSafe = true;
  2. while(getline(inFile,inputStr))
  3. {
  4. if(isSafe)
  5. {
  6. outFile << " " <<inputStr <<endl;
  7. isSafe = false;
  8. }
  9. else if ((inputStr == "") || (inputStr.length() ==0) || (inputStr.size() ==0))
  10. {
  11. isSafe = true;
  12. }
  13. else
  14. {
  15. outFile << inputStr << endl;
  16. }
  17.  
  18. }

It looks as though im really in the same cicumstance as before where as initially I set the flag to true and yes it indents the first paragraph and then the flag is set back to false and then just runs through the rest of the loop as false print out the rest of the file as it sees it.
What im still lacking here is the knowledge of how the flag knows that it can flip back to true. Knowing when to indent; Im still not finding anywhere in my book nor the net what to use a acknowledgment that the marker has "run back into words after lines are empty".
?any ideas?

Thanks again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R6er is offline Offline
15 posts
since Mar 2007
Mar 25th, 2007
0

Re: Help with indenting infile -> outfile paragraph...

Like I said before, trimming the string before processsing it is the way to go in your case. After trimming the string, if its length is zero (check only for length and not the other two), then flip the binary flag.

Maybe something like this: (not tested)

C++ Syntax (Toggle Plain Text)
  1. void leftTrim(string& inStr, const string delimiters = " \t\n\r\v\f")
  2. {
  3. string::size_type pos = inStr.find_first_not_of(delimiters);
  4. inStr.erase(0, pos);
  5. }
  6.  
  7. int main ()
  8. {
  9. string line;
  10. bool flag = true;
  11. ifstream inFile("test.txt");
  12. ofstream outFile("output.txt");
  13. while(getline(inFile, line))
  14. {
  15. leftTrim(line);
  16. if(line.size() == 0)
  17. {
  18. flag = true;
  19. continue; //skip current line and move to next one
  20. }
  21. if(flag) //if a new paragraph is afoot
  22. {
  23. outFile << endl << " " << line << endl;
  24. flag = false;
  25. }
  26. else //its a normal line
  27. {
  28. outFile << line << endl;
  29. }
  30. }
  31. //close the streams and wait for keypress
  32. inFile.close();
  33. outFile.close();
  34. getchar ();
  35. }
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Mar 25th, 2007
0

Re: Help with indenting infile -> outfile paragraph...

It seems as if that did the trick, Its funny that I was never tought that nor could find it in any books, I appreciate the knowledge.

Just curious, what does the:

C++ Syntax (Toggle Plain Text)
  1. getchar ();

mean at the bottem? I am not familiar with that.

Thanks again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R6er is offline Offline
15 posts
since Mar 2007
Mar 25th, 2007
0

Re: Help with indenting infile -> outfile paragraph...

getchar is a standard C /C++ functiona used for getting data from the standard input stream (in normal cases keyboard). It is buffered in the sense it does not see the characters unless the user presses the return key. Since we want to prevent the console window from disappearing after the program execution, we put it at the end of the program before the return statement if any is present.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Mar 25th, 2007
0

Re: Help with indenting infile -> outfile paragraph...

That makes sence, I am suprized that I have not run into that just yet.
Thanks for the tip.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R6er is offline Offline
15 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: hi
Next Thread in C++ Forum Timeline: System.String.LastIndexOf problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC