Sign in to follow this  
nooblet

Putting Strings in files

Recommended Posts

Limitz    342
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
GamerSg    378
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
Scet    960
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
Chad Smith    1343
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
nooblet    167
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
Limitz    342
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
Limitz    342
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
Prototype    2014
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
Limitz    342
Ow yeah i see... sorry :)

I usually use stdio for my i/o, i don't really like iostream

I know you would like to use iostream for your program, so i don't think i can help you there. I still believe that there is nothing wrong with using stdio in C++ so i did write the solution to your problem using stdio. Use it, don't use it... whatever :)


#include <cstdio>
#include <cstring>

int main()
{
FILE* f = fopen("users.txt","a+");
char username[80], checkuser[80];
bool found = false;

printf("Username: ");
scanf("%s",username);

while (fscanf(f,"%s",checkuser)>0)
{
if (!strcmp(checkuser,username))
{
printf("User already in database");
found = true;
break;
}
}
if (!found) {
fprintf(f, "%s\n",username);
printf("User added to database");
}
fclose(f);
return 0;
}


Greetings. :)

Share this post


Link to post
Share on other sites
Aardvajk    13207
Limitz - I'm not going to get all evangelical about iostream, but what would happen with your code if I typed a user name with more than 80 characters? Big crash and security hole is the answer.

This would not be a problem with std::string and getline(cin,string) for example. iostream is not recommended as a question of style or taste, but as a question of safety and security.

I really don't mean to be nit-picky about this. It is just that I feel these issues are of particular importance in the beginners section. I am sorry if this post comes across as rude in any way.

Paul

Share this post


Link to post
Share on other sites
Limitz    342
Quote:
Original post by EasilyConfused
Limitz - I'm not going to get all evangelical about iostream, but what would happen with your code if I typed a user name with more than 80 characters? Big crash and security hole is the answer.

This would not be a problem with std::string and getline(cin,string) for example. iostream is not recommended as a question of style or taste, but as a question of safety and security.

I really don't mean to be nit-picky about this. It is just that I feel these issues are of particular importance in the beginners section. I am sorry if this post comes across as rude in any way.

Paul


I do agree that he should use iostream if possible. I am not trying to get him to switch to stdio or anything, but i did want to give him a solution to his problem. Maybe someone should give a code sample for iostream, and we'll all be happy :)

Just for my defense, not to be a stubborn jackass or somthing ;) : stdio provides for safety too, if you'd use the format: %79s in stead of %s it would only read 79 chars + \0. The format that reads from the file could be %79s%*[^\n] for safety (read the first 79 chars and drop the rest of the line). However i do agree that that would be beyond the scope of a thread in the beginners section :)

Greetings.

Share this post


Link to post
Share on other sites
Aardvajk    13207
That's a fair comment. Here's how I'd do it personally with C++.


#include <string>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
vector<string> users;

ifstream is("users.txt"); if(!is.open()) return -1;

while(!is.eof())
{
string s;
getline(is,s);
users.push_back(s);
}

is.close();

string new_user;
cout << "Enter user name: ";
getline(cin,new_user);

for(int i=0;i<users.size();++i)
{
if(users[i]==new_user)
{
cout << "User already registered.\n";
return -1;
}
}

users.push_back(new_user);
cout << "Registered new user: " << new_user << "\n";

ofstream os("users.txt"); if(!os.open()) return -1;

for(int i=0;i<users.size();++i) os << users[i] << "\n";

return 0;
}





I hate patching existing files directly personally.

Share this post


Link to post
Share on other sites
Omid Ghavami    1007
This is way off-topic, and I apologize in advance.
I don't know exactly what your goal is, but from what I've gathered you need some kind of database. Could I perhaps recommend that you look into SQLite? I think that it might fit your needs perfectly [smile]

Hope this helps

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Omid Ghavami
This is way off-topic, and I apologize in advance.
I don't know exactly what your goal is, but from what I've gathered you need some kind of database. Could I perhaps recommend that you look into SQLite? I think that it might fit your needs perfectly [smile]

Hope this helps


Eh? Beginners don't need a database. C++ is difficult enough as is without trying to learn SQL at the same time.

Share this post


Link to post
Share on other sites
Chad Smith    1343
I agree.

I don't think he is trying to create a MMO here or anything like that. It just seems like he is using something with File I/O. No reason for a database.


Chad

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this