How do you make new lines in text files?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compman9902
    New Member
    • Mar 2007
    • 105

    How do you make new lines in text files?

    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
    Top Contributor
    • Jan 2007
    • 2476

    #2
    "\n" is not working ???????

    Comment

    • compman9902
      New Member
      • Mar 2007
      • 105

      #3
      Originally posted by dmjpro
      "\n" is not working ???????
      Here is the code that is being used:
      Code:
      ...
      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.

      Comment

      • compman9902
        New Member
        • Mar 2007
        • 105

        #4
        Originally posted by compman9902
        Here is the code that is being used:
        Code:
        ...
        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...

        Comment

        • Xaro
          New Member
          • Mar 2007
          • 1

          #5
          Code:
          string myString;
          string text;
          if (text == "\n")
          {
          myString = myString + "\n"
          }
          Hmm, let me think now...

          Code:
          string myString;
          string text;
          if (text == "\n")
          {
          myString = myString
          std::cout << "\n"
          }
          That should work.

          Comment

          • compman9902
            New Member
            • Mar 2007
            • 105

            #6
            Originally posted by Xaro
            Code:
            string myString;
            string text;
            if (text == "\n")
            {
            myString = myString + "\n"
            }
            Hmm, let me think now...

            Code:
            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.

            Comment

            • DeMan
              Top Contributor
              • Nov 2006
              • 1799

              #7
              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

              Comment

              • compman9902
                New Member
                • Mar 2007
                • 105

                #8
                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.

                Comment

                • DeMan
                  Top Contributor
                  • Nov 2006
                  • 1799

                  #9
                  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().

                  Comment

                  • compman9902
                    New Member
                    • Mar 2007
                    • 105

                    #10
                    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 misunderstandin g. Well here is what I'm using:
                    Code:
                    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

                    Comment

                    • DeMan
                      Top Contributor
                      • Nov 2006
                      • 1799

                      #11
                      Code:
                      while (! fileopen.eof() )
                      {
                      getline (fileopen,line);
                      text = text + line + "/n";
                      }
                      LEt us know if it works (or not)

                      Comment

                      • compman9902
                        New Member
                        • Mar 2007
                        • 105

                        #12
                        Originally posted by DeMan
                        Code:
                        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.

                        Comment

                        • compman9902
                          New Member
                          • Mar 2007
                          • 105

                          #13
                          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.

                          Comment

                          • DeMan
                            Top Contributor
                            • Nov 2006
                            • 1799

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

                            Comment

                            • compman9902
                              New Member
                              • Mar 2007
                              • 105

                              #15
                              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?

                              Comment

                              Working...