| | |
Help with indenting infile -> outfile paragraph...
Thread Solved
![]() |
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
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:
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)
while(inFile) { getline(inFile,inputStr); if () { outFile << " " << inputStr << endl; } else { outFile << inputStr << endl; } }
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
The while loop is better written like this
c Syntax (Toggle Plain Text)
while( getline(inFile,inputStr) ) { if ( <flag is set> ) { outFile << " " << inputStr << endl; <set flag to false> } else { outFile << inputStr << endl; <if inputStr is an empty string set flag to true > } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
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
"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
>>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.
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.
a string is empty when any one of these conditions exist.
C++ Syntax (Toggle Plain Text)
inputStr == ""; or inputStr.length() == 0 or 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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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):
• 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)
#include <string> const std::string whiteSpaces( " \f\n\r\t\v" ); void trimRight( std::string& str, const std::string& trimChars = whiteSpaces ) { std::string::size_type pos = str.find_last_not_of( trimChars ); str.erase( pos + 1 ); } void trimLeft( std::string& str, const std::string& trimChars = whiteSpaces ) { std::string::size_type pos = str.find_first_not_of( trimChars ); str.erase( 0, pos ); } void trim( std::string& str, const std::string& trimChars = whiteSpaces ) { trimRight( str, trimChars ); trimLeft( str, trimChars ); }
Last edited by ~s.o.s~; Mar 25th, 2007 at 12:31 am.
I don't accept change; I don't deserve to live.
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
Thank you for taking the time to give me some hints here.
I have taken your tips into consideration and have formulated this here:
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
I have taken your tips into consideration and have formulated this here:
C++ Syntax (Toggle Plain Text)
isSafe = true; while(getline(inFile,inputStr)) { if(isSafe) { outFile << " " <<inputStr <<endl; isSafe = false; } else if ((inputStr == "") || (inputStr.length() ==0) || (inputStr.size() ==0)) { isSafe = true; } else { outFile << inputStr << endl; } }
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
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)
Maybe something like this: (not tested)
C++ Syntax (Toggle Plain Text)
void leftTrim(string& inStr, const string delimiters = " \t\n\r\v\f") { string::size_type pos = inStr.find_first_not_of(delimiters); inStr.erase(0, pos); } int main () { string line; bool flag = true; ifstream inFile("test.txt"); ofstream outFile("output.txt"); while(getline(inFile, line)) { leftTrim(line); if(line.size() == 0) { flag = true; continue; //skip current line and move to next one } if(flag) //if a new paragraph is afoot { outFile << endl << " " << line << endl; flag = false; } else //its a normal line { outFile << line << endl; } } //close the streams and wait for keypress inFile.close(); outFile.close(); getchar (); }
I don't accept change; I don't deserve to live.
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
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:
mean at the bottem? I am not familiar with that.
Thanks again
Just curious, what does the:
C++ Syntax (Toggle Plain Text)
getchar ();
mean at the bottem? I am not familiar with that.
Thanks again
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.
I don't accept change; I don't deserve to live.
![]() |
Other Threads in the C++ Forum
- Previous Thread: hi
- Next Thread: System.String.LastIndexOf problem
| Thread Tools | Search this Thread |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix matrix3d memory multiple news node output parameter pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings temperature template test text text-file tree url variable vector video visualization win32 windows winsock word wordfrequency wxwidgets






