Connecting Tech Pros Worldwide Forums | Help | Site Map

How do you make new lines in text files?

compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#1: Mar 23 '07
How Do You Make New Lines In Text Files? Just A Quick Question In Need Of A Quick Answer. Keep In Mind That The Character(s) That Are Needed Have To Be Stored In A String Variable.
Thank You.

-Parker Woods

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#2: Mar 23 '07

re: How do you make new lines in text files?


"\n" is not working ???????
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#3: Mar 23 '07

re: How do you make new lines in text files?


Quote:

Originally Posted by dmjpro

"\n" is not working ???????

Here is the code that is being used:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. string myString;
  3. string text;
  4. if (text == "\n")
  5. {
  6. myString = myString + "\n"
  7. }
  8. ...
  9.  
After that, it just goes on putting it into the text file as one big string. I know that works because every part of the string gets put in up until the new line.
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#4: Mar 23 '07

re: How do you make new lines in text files?


Quote:

Originally Posted by compman9902

Here is the code that is being used:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. string myString;
  3. string text;
  4. if (text == "\n")
  5. {
  6. myString = myString + "\n"
  7. }
  8. ...
  9.  
After that, it just goes on putting it into the text file as one big string. I know that works because every part of the string gets put in up until the new line.

It just occured to me...When you are getting input from a text file, does it read the "\n", or is it discarded? Hmmm...
Newbie
 
Join Date: Mar 2007
Posts: 1
#5: Mar 23 '07

re: How do you make new lines in text files?


Expand|Select|Wrap|Line Numbers
  1. string myString;
  2. string text;
  3. if (text == "\n")
  4. {
  5. myString = myString + "\n"
  6. }
  7.  
Hmm, let me think now...

Expand|Select|Wrap|Line Numbers
  1. string myString;
  2. string text;
  3. if (text == "\n")
  4. {
  5. myString = myString
  6. std::cout << "\n"
  7. }
  8.  
That should work.
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#6: Mar 23 '07

re: How do you make new lines in text files?


Quote:

Originally Posted by Xaro

Expand|Select|Wrap|Line Numbers
  1. string myString;
  2. string text;
  3. if (text == "\n")
  4. {
  5. myString = myString + "\n"
  6. }
  7.  
Hmm, let me think now...

Expand|Select|Wrap|Line Numbers
  1. string myString;
  2. string text;
  3. if (text == "\n")
  4. {
  5. myString = myString
  6. std::cout << "\n"
  7. }
  8.  
That should work.

Nope, that dosn't work.
Please remember that the newline has to be in a string and that string will be put into a text file.
DeMan's Avatar
Lives Here
 
Join Date: Nov 2006
Location: Adelaide, SA
Posts: 1,748
#7: Mar 23 '07

re: How do you make new lines in text files?


Where are you getting "text" from (I realise the declaration is immediately before the if here just to highlight how it was declared), but I wondered whether you can post where you give text a value
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#8: Mar 23 '07

re: How do you make new lines in text files?


Quote:

Originally Posted by DeMan

Where are you getting "text" from (I realise the declaration is immediately before the if here just to highlight how it was declared), but I wondered whether you can post where you give text a value

I'm getting text from another text file. You see, I'm makeing a decrypter and the encrypter actualy gives every new line a numarical value, so thats whats happening.
DeMan's Avatar
Lives Here
 
Join Date: Nov 2006
Location: Adelaide, SA
Posts: 1,748
#9: Mar 23 '07

re: How do you make new lines in text files?


Sorry, what I meant is what is the command you are using to fill the variable "text".
In the example you declare it and immediately test it (without assigning it a value). I understand this is for demonstration purposes, but how are you reading a value into "text" in your program.....

Assuming you read it line by line using getline, then you don't even need to test for "\n", simply add a "\n" after every call to getline().
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#10: Mar 24 '07

re: How do you make new lines in text files?


Quote:

Originally Posted by DeMan

Sorry, what I meant is what is the command you are using to fill the variable "text".
In the example you declare it and immediately test it (without assigning it a value). I understand this is for demonstration purposes, but how are you reading a value into "text" in your program.....

Assuming you read it line by line using getline, then you don't even need to test for "\n", simply add a "\n" after every call to getline().

Oh, sorry for the misunderstanding. Well here is what I'm using:
Expand|Select|Wrap|Line Numbers
  1. string fileOpen;
  2. cin  >> fileOpen; //The Path Of The File To Be Opened
  3. ifstream fileopen;
  4. fileopen.open(fileOpen.c_str());
  5. if (fileopen.is_open())
  6. {
  7. while (! fileopen.eof() )
  8. {
  9. getline (fileopen,line);
  10. text = text + line;
  11. }
  12. fileopen.close();
  13. }
  14. else
  15. {
  16. cout << "The File Was Unable To Open For An Unknown Reason. Please Check The Name" << endl;
  17. cout << "You Gave The File And Try Again." << endl;
  18. cout << "                  Program Terminated" << endl;
  19. system("PAUSE");
  20. return 0;
  21. }
That is what I'm using to fill in "text".
But I will try to do that, you know, just add a "\n"

--Parker Woods
DeMan's Avatar
Lives Here
 
Join Date: Nov 2006
Location: Adelaide, SA
Posts: 1,748
#11: Mar 24 '07

re: How do you make new lines in text files?


Expand|Select|Wrap|Line Numbers
  1. while (! fileopen.eof() )
  2. {
  3. getline (fileopen,line);
  4. text = text + line + "/n";
  5. }
  6.  
LEt us know if it works (or not)
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#12: Mar 24 '07

re: How do you make new lines in text files?


Quote:

Originally Posted by DeMan

Expand|Select|Wrap|Line Numbers
  1. while (! fileopen.eof() )
  2. {
  3. getline (fileopen,line);
  4. text = text + line + "/n";
  5. }
  6.  
LEt us know if it works (or not)

that time it literally put "\n" into the text file..humerous but not functional.
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#13: Mar 24 '07

re: How do you make new lines in text files?


Quote:

Originally Posted by compman9902

that time it literally put "\n" into the text file..humerous but not functional.

Also, please remember that the string "text" is being put into another text file. Just something to keep in mind.
DeMan's Avatar
Lives Here
 
Join Date: Nov 2006
Location: Adelaide, SA
Posts: 1,748
#14: Mar 24 '07

re: How do you make new lines in text files?


You could incrementally add to the file, that is temp = firstlineline;
then use putline to write temp, set temp to "" and start again
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#15: Mar 24 '07

re: How do you make new lines in text files?


Quote:

Originally Posted by DeMan

You could incrementally add to the file, that is temp = firstlineline;
then use putline to write temp, set temp to "" and start again

Do you mind giving me some code?That just know made my brain explode.
Also, do you ever get the feeling that someone is calling you a geek when you are writing on this site? And third of all, when will my ranking go from newbie to something else?
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#16: Mar 24 '07

re: How do you make new lines in text files?


Thanks alot for your help everyone, but the issue has been resolved. All I had to do was change a little of my code before hand.
Well, thanks. That was the last bug for my program.
But, if any one can tell me how to open the "open" and "close" GUI file dialogs, that would be great for version 1.1!
Just P.M. or email them to me (from my profile, just click on my name for all you newbies out there)
Reply