Does anybody know how to add a return at the end of a sequential file? Any comment would be appreciated. Thanks!
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. ...
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.
>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.
and here is for C++'s fstream.h
ofstream fout;
fout.open("text.txt");
fout << "hello" << "\n" << "world";
fout.close();
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 12:21 PM.
"only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein
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.
>Well, I've tried the fseek, but the "fp" is giving me trouble
Oops, this is the C++ board. This will work better:
-PreludeCode:#include <iostream> #include <fstream> int main ( void ) { std::fstream out ( "somefile" ) ; out.seekg ( 0, std::ios::end ); out.put ( '\n' ); return 0; }
My best code is written with the delete key.
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
>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.
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
>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().
>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.
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; }
-
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
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.