Adding \n to sequential file

This is a discussion on Adding \n to sequential file within the C++ Programming forums, part of the General Programming Boards category; Does anybody know how to add a return at the end of a sequential file? Any comment would be appreciated. ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    Adding \n to sequential file

    Does anybody know how to add a return at the end of a sequential file? Any comment would be appreciated. Thanks!
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Does anybody know how to add a return at the end of a sequential file?
    Open it, go to the end and hit enter. Or:
    fseek ( fp, 0L, SEEK_END );
    fputc ( '\n', fp );

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    and here is for C++'s fstream.h

    ofstream fout;

    fout.open("text.txt");
    fout << "hello" << "\n" << "world";
    fout.close();

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Thats a pretty dumb reply Mickey since he said at the "END" of the file! It should be this:

    Code:
    #include<fstream>
    using std::ofstream;
    using std::cin;
    using std::cout;
    
    // you can do this b/c fstream includes iostream
    int main(void)
    {    ofstream out;
         out.open("test.txt");
         if(out.good())
         {    out.seekg(ios::end);
              out << '\n';
         }
         out.close();
         
         return(0);
    }
    Last edited by xds4lx; 04-07-2002 at 01:21 PM.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Originally posted by Prelude
    Open it, go to the end and hit enter. Or:
    fseek ( fp, 0L, SEEK_END );
    fputc ( '\n', fp );


    Well, I've tried the fseek, but the "fp" is giving me trouble. I gather it's a pointer but I need to figure out how to declare it properly. Still a little green when it comes to pointers but you've shoved me in the right direction. Thanks!


    Originally posted by xds4lx

    ofstream out;
    out.open("test.txt");
    if(out.good())
    { out.seekg(ios::end);
    out << '\n';
    }
    out.close()


    I've tried this one too, but the compiler gives me an error saying that 'seekg' : is not a member of 'ofstream'. I don't know where to go from there. Thanks for your time though!
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Well, I've tried the fseek, but the "fp" is giving me trouble
    Oops, this is the C++ board. This will work better:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main ( void )
    {
      std::fstream out ( "somefile" ) ;
      out.seekg ( 0, std::ios::end );
      out.put ( '\n' );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Isnt that the same thing I posted prelude? Yes, except you included iostream with is not required if you look through the headers fstream includes iostream.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Isnt that the same thing I posted prelude?
    I was correcting myself, not you.

    >Yes, except you included iostream with is not required if you
    >look through the headers fstream includes iostream.
    Yes, I know this. I include iostream if my program performs any input or output as a matter of style since the inclusion of unused header files has little effect on the overal efficiency of the program, whether or not you agree with my style is a personal problem unless you can prove me wrong to my satisfaction in which case I'll consider changing.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Well actually this is compiler dependent from what one of my old professors said. He said that if you include an unecessary header, the compiler (if its stupid or just microsofts) will go ahead an include everything that header defines (unless it uses namespaces). And I dont have a problem with your style I was just pointing out that it is that it is not necessary to include iostream when including fstream.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I've tried this one too, but the compiler gives me an error saying that 'seekg' : is not a member of 'ofstream'. I don't know where to go from there. Thanks for your time though!

    Follow the examples posted, but substitute seekp() for seekg().

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    >Follow the examples posted, but substitute seekp() for seekg().

    Alas, I tried, and it ended up erasing my file. But it did add a space, some character and a return! I've searched and searched and I am stumped.

    Maybe there would be a way to read in the file into an array and then add a '\n' at the end of the array?
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  12. #12
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    seekg is getpointer and seekp is putpointer so i think seekp should be used.

    you probably are making the mistake of not using ios::app ie append to the end of file.


    try this

    Code:
    #include<fstream.h>
    int main()
    {
     fstream f1;
     f1.open("MYFILE.TXT",ios::app);
     f1.seekp(ios::end);
     f1<<"\n";
     f1.close();
     return 0;
    }

  13. #13
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Here go read this! http://www.cpp-home.com/FileIO_tutorial.php . If you want to give up the way you are trying just read the file into a vector or something like that ex:
    Code:
    vector<string> fileVec;
    ifstream in("test.txt");
    string strLine;
    while(in.good())
    {    getline(in,strLine);
         fileVec.push_back(strLine);
    }
    fileVec.push_back("\n");
    in.close();
    
    ofstream out("test.txt");
    int i = 0;
    while(i < fileVec.size() && out.good())
    {   out << fileVec[i] << endl;
        i++;
    }
    out.close()
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    You were all of great help! Thank you for your time and effort!


    Originally posted by ihsir

    try this

    code:
    #include<fstream.h>
    int main()
    {
    fstream f1;
    f1.open("MYFILE.TXT",ios::app);
    f1.seekp(ios::end);
    f1<<"\n";
    f1.close();
    return 0;
    }


    That worked like a charm!!! Thank you very much!!! It is very appreciated.


    Originally posted by xds4lx

    Here go read this! http://www.cpp-home.com/FileIO_tutorial.php . If you want to give up the way you are trying just read the file into a vector or something like that:


    Thanks for the URL, it was very informative! And thanks for the vector info, I'll keep it in mind.
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 12:41 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21