Jump to content
  • Advertisement
Sign in to follow this  
nooblet

Putting Strings in files

This topic is 4352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, when I am doing File I/O how do I output a string into the .txt document? IDE: Dev-C++ Language: C++

Share this post


Link to post
Share on other sites
Advertisement
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*

Share this post


Link to post
Share on other sites
OR you could do this with the C++ file io

using namespace std;

{
string str = "Hello World";
ofstream of("test.txt");
of << str << endl;
}

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
That's using the C way. I am sure he wants the C++ way, in which would look something like this I believe


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

Share this post


Link to post
Share on other sites
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...


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

Share this post


Link to post
Share on other sites
Quote:
Original post by GamerSg
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();


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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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])

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!