How do you make new lines in text files?  | Member | | Join Date: Mar 2007 Location: Anchorage, Alaska
Posts: 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
|  | Lives Here | | Join Date: Jan 2007 Location: India (West-Bengal)
Posts: 2,451
| | | re: How do you make new lines in text files?
"\n" is not working ???????
|  | Member | | Join Date: Mar 2007 Location: Anchorage, Alaska
Posts: 105
| | | 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: - ...
-
string myString;
-
string text;
-
if (text == "\n")
-
{
-
myString = myString + "\n"
-
}
-
...
-
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.
|  | Member | | Join Date: Mar 2007 Location: Anchorage, Alaska
Posts: 105
| | | re: How do you make new lines in text files? Quote:
Originally Posted by compman9902 Here is the code that is being used: - ...
-
string myString;
-
string text;
-
if (text == "\n")
-
{
-
myString = myString + "\n"
-
}
-
...
-
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
| | | re: How do you make new lines in text files? -
string myString;
-
string text;
-
if (text == "\n")
-
{
-
myString = myString + "\n"
-
}
-
Hmm, let me think now... -
string myString;
-
string text;
-
if (text == "\n")
-
{
-
myString = myString
-
std::cout << "\n"
-
}
-
That should work.
|  | Member | | Join Date: Mar 2007 Location: Anchorage, Alaska
Posts: 105
| | | re: How do you make new lines in text files? Quote:
Originally Posted by Xaro -
string myString;
-
string text;
-
if (text == "\n")
-
{
-
myString = myString + "\n"
-
}
-
Hmm, let me think now... -
string myString;
-
string text;
-
if (text == "\n")
-
{
-
myString = myString
-
std::cout << "\n"
-
}
-
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.
|  | Lives Here | | Join Date: Nov 2006 Location: Adelaide, SA
Posts: 1,748
| | | 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
|  | Member | | Join Date: Mar 2007 Location: Anchorage, Alaska
Posts: 105
| | | 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.
|  | Lives Here | | Join Date: Nov 2006 Location: Adelaide, SA
Posts: 1,748
| | | 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().
|  | Member | | Join Date: Mar 2007 Location: Anchorage, Alaska
Posts: 105
| | | 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: -
string fileOpen;
-
cin >> fileOpen; //The Path Of The File To Be Opened
-
ifstream fileopen;
-
fileopen.open(fileOpen.c_str());
-
if (fileopen.is_open())
-
{
-
while (! fileopen.eof() )
-
{
-
getline (fileopen,line);
-
text = text + line;
-
}
-
fileopen.close();
-
}
-
else
-
{
-
cout << "The File Was Unable To Open For An Unknown Reason. Please Check The Name" << endl;
-
cout << "You Gave The File And Try Again." << endl;
-
cout << " Program Terminated" << endl;
-
system("PAUSE");
-
return 0;
-
}
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
|  | Lives Here | | Join Date: Nov 2006 Location: Adelaide, SA
Posts: 1,748
| | | re: How do you make new lines in text files? -
while (! fileopen.eof() )
-
{
-
getline (fileopen,line);
-
text = text + line + "/n";
-
}
-
LEt us know if it works (or not)
|  | Member | | Join Date: Mar 2007 Location: Anchorage, Alaska
Posts: 105
| | | re: How do you make new lines in text files? Quote:
Originally Posted by DeMan -
while (! fileopen.eof() )
-
{
-
getline (fileopen,line);
-
text = text + line + "/n";
-
}
-
LEt us know if it works (or not) that time it literally put " \n" into the text file..humerous but not functional.
|  | Member | | Join Date: Mar 2007 Location: Anchorage, Alaska
Posts: 105
| | | 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.
|  | Lives Here | | Join Date: Nov 2006 Location: Adelaide, SA
Posts: 1,748
| | | 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
|  | Member | | Join Date: Mar 2007 Location: Anchorage, Alaska
Posts: 105
| | | 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?
|  | Member | | Join Date: Mar 2007 Location: Anchorage, Alaska
Posts: 105
| | | 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)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,533 network members.
|