By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

How do you make new lines in text files?

compman9902
100+
P: 105
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
Mar 23 '07 #1
Share this Question
Share on Google+
15 Replies


dmjpro
100+
P: 2,476
"\n" is not working ???????
Mar 23 '07 #2

compman9902
100+
P: 105
"\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.
Mar 23 '07 #3

compman9902
100+
P: 105
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...
Mar 23 '07 #4

P: 1
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.
Mar 23 '07 #5

compman9902
100+
P: 105
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.
Mar 23 '07 #6

DeMan
100+
P: 1,806
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
Mar 23 '07 #7

compman9902
100+
P: 105
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.
Mar 23 '07 #8

DeMan
100+
P: 1,806
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().
Mar 23 '07 #9

compman9902
100+
P: 105
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
Mar 23 '07 #10

DeMan
100+
P: 1,806
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)
Mar 23 '07 #11

compman9902
100+
P: 105
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.
Mar 23 '07 #12

compman9902
100+
P: 105
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.
Mar 24 '07 #13

DeMan
100+
P: 1,806
You could incrementally add to the file, that is temp = firstlineline;
then use putline to write temp, set temp to "" and start again
Mar 24 '07 #14

compman9902
100+
P: 105
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?
Mar 24 '07 #15

compman9902
100+
P: 105
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)
Mar 24 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.