Putting Strings in files
Hello, when I am doing File I/O how do I output a string into the .txt document?
IDE: Dev-C++
Language: C++
FILE* fp = fopen("test.txt","w");
fprintf(fp,"Hello World");
fclose(fp);
That what you were looking for ? :)
Greetings.
EDIT: fprintf(fp,str); works as well if str is a char*
fprintf(fp,"Hello World");
fclose(fp);
That what you were looking for ? :)
Greetings.
EDIT: fprintf(fp,str); works as well if str is a char*
OR you could do this with the C++ file io
using namespace std;
{
string str = "Hello World";
ofstream of("test.txt");
of << str << endl;
}
using namespace std;
{
string str = "Hello World";
ofstream of("test.txt");
of << str << endl;
}
Quote:Original post by Limitz
FILE* fp = fopen("test.txt","w");
fprintf(fp,"Hello World");
fclose(fp);
That what you were looking for ? :)
Greetings.
EDIT: fprintf(fp,str); works as well if str is a char*
That is the C way of doing things :)
For C++, you can use fstream. Nice thing with fstream is that you can easily use strings with it as well.
string name="dude";
ofstream fo;
fo.open("test.txt");
fo<<"Hello "<<name<<".\n";
fo.close();
Quote:Original post by nooblet
Language: C++
std::string SomeString = "Hello World!";
std::ofstream fout( "Test.txt" );
fout << SomeString << std::endl;
fout.close();
Edit: Blah, way too slow.
That's using the C way. I am sure he wants the C++ way, in which would look something like this I believe
EDIT: WOW! I AM LATE!
#include <fstream>#include <string>using namespace std;int main(){ ofstream SaveFile("file.txt"); string text="hello"; SaveFile << text; SaveFile.close(); return 0;}
EDIT: WOW! I AM LATE!
Hmm, ok well see what I am trying to do is create a program so the people on my team can register with it and it writes it in one file...
Getting mad cause' it will overwrite the username every time someone runs the program and types their name.
#include <iostream>#include <fstream>using namespace std;int main(){string username;ofstream a_file("members.txt");ifstream b_file("members.txt");cout << "Username: ";cin >> username;if( b_file >> username ){a_file << username;}else{cout << "You exist in our database!" << endl;}system("Pause");return 0;}
Getting mad cause' it will overwrite the username every time someone runs the program and types their name.
Quote:Original post by GamerSgQuote:Original post by Limitz
FILE* fp = fopen("test.txt","w");
fprintf(fp,"Hello World");
fclose(fp);
That what you were looking for ? :)
Greetings.
EDIT: fprintf(fp,str); works as well if str is a char*
That is the C way of doing things :)
For C++, you can use fstream. Nice thing with fstream is that you can easily use strings with it as well.
string name="dude";
ofstream fo;
fo.open("test.txt");
fo<<"Hello "<<name<<".\n";
fo.close();
Hehe, i know. But there is no rule that states that c functions are deprecated when using C++ ;) Would it help if i said i included <cstdio> in stead of <stdio.h> ? :)
Greetings.
Quote:Original post by nooblet
Hmm, ok well see what I am trying to do is create a program so the people on my team can register with it and it writes it in one file...
*** Source Snippet Removed ***
Getting mad cause' it will overwrite the username every time someone runs the program and types their name.
ofstream a_file ( "test.txt", ios::app ); ? To append in stead of overwrite
Quote:Original post by Limitz
ofstream a_file ( "test.txt", ios::app ); ? To append in stead of overwrite
How about if you look at the code and figure out what's actually wrong with it? Like assigning multiple strings to the same variable, reading/writing from the same file simultaneously, etc. If you want to help someone, put some effort in there.
(Pot, kettle, black, I know [grin])
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement