Adding to file name using a (string)variable + "test.txt)

closed account (17Mo1hU5)
username is a user input and I wanna name a file as username_friends.txt
I used this line of code

string fileName= username + "_friends.txt";

So I can then open a stream like so

ifstream check;
check.open(fileName.c_str());

but the file being created is named _friends.txt, any ideas how to fix that?

I have included:
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>

int main(){
Network n1 = Network();
User va,ar;
cout<<"Aris"<<endl;
ar.getUser();
cout<<"Vasilis"<<endl;
va.getUser();
cout<<"link aris and vasilis"<<endl;
n1.linkFriends(ar,va);
cout<<ar.fileName<<"\t"<<ar.username;
}

classes:

class Network{
private:
User reg;
User tempUser;
User u1;
bool flag;

public:
bool isUser (User u1,User u2){

if (u2.username==u1.username || u2.email == u1.email){
return 1;
}
else return 0;
}

void newUser(){
reg.getUser();

ifstream InUserData;
InUserData.open("Users.txt");
if(InUserData.fail()){
cout<<"File didn't open"<<endl;
exit(0);
}
else{
do{
InUserData>>tempUser.email>>tempUser.username>>tempUser.password;

if(isUser(reg,tempUser)){
cout<<"Email or Username already exist"<<endl;
InUserData.close();
}
else if(InUserData.eof()){
ofstream OutUserData;
OutUserData.open("Users.txt", ios_base::app);
OutUserData << reg.toString();
InUserData.close();
OutUserData.close();
cout<<"Success"<<endl;
}
}
while (InUserData.is_open() && !InUserData.eof());
}
}

void deleteUser(User u1){
flag=0;
ifstream read;
ofstream write;
read.open("Users.txt", ios_base::out);
write.open("temp.txt",ios_base::app);
while(read.is_open() && !read.eof() && write.is_open()){
read>>tempUser.email>>tempUser.username>>tempUser.password;
if(!isUser(tempUser,u1)){
write<<tempUser.toString();
}
else if(isUser(tempUser,u1)){
cout<<"The account: "<<u1.username<<" has been deleted "<<endl;
flag=1;
}
}
read.close();
write.close();
if(flag==0){
cout<<"No user registered with that data"<<endl;
remove("temp.txt");
}
else{
remove("Users.txt");
rename("temp.txt", "Users.txt");
}
}

void linkFriends(User u1,User u2){
if(isUser(u1,u2)){
cout<<"You can not friend yourself"<<endl; //Exception
}
else{
u1.addFriend(u2);
u2.addFriend(u1);
}
}
};

struct User{
private:
string data;
int i=2;

public:
string username;
string password;
string email;
const string fileName= username + "_friends.txt";


User getUser(){
cout<<"Email:\t"<<endl;
getline(cin,email);
cout<<"Username:\t"<<endl;
getline(cin,username);
cout<<"Password:\t"<<endl;
getline(cin,password);
return *this;
}

string toString(){
return this->email + "\n" + this->username + "\n" + this->password + "\n";
}

void addFriend(User u1){
fstream friends;
friends.open(fileName.c_str(), ios::in | ios::out | ios::app);
while(friends.is_open()){
friends>>data;
if(data==u1.email){
cout<<"Already friends"<<endl;
friends.close();
}
else if(friends.eof()){
friends<<u1.email+'\n';
friends.close();
}
}
}
};

Note: I know the code is probably real bad in terms of memory usage and pretty much everything, I am new to c++. Thank you in advance
Last edited on
ifstream is used to read from files. It doesn't create files.
closed account (17Mo1hU5)
true, the thing is that I actually printed fileName and I get _friends.txt (I actually need both to read and write to the file so I will be using it anyway)
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string username = "bilis321";
	string fileName = username + "_friends.txt";
	cout << fileName << '\n';
}

The code outputs "bilis321_friends.txt". I don't know what more to say.
Hello bilis321,

If you need to use "fileName" for both input and output you need to use:
std::fstream check(fileName, std::ios::in | std::ios::out | std::ios::app);
This both sets the file stream as "check" and opens the files for input and output.

The header file "stdio.h" would be better as "cstdio", but even with that you should not be using it in a C++ porgram. Also if "iostream" does not cover what you need it is possible that you have done something wrong. There is also a good chance that "iostream" will include "stdio.h" somewhere along the way through the header files that "iostream" will include.

If you use check.open(fileName.c_str());. From C++11 on the ".cstr()" is not needed as a "std::string" works just fine.

Given the pieces of code you first posted, most of what is there is out of context. Without the full code to look at is is a bit of a guessing game as to what is going wrong.

Hope that helps,

Andy
closed account (17Mo1hU5)
Damn I didnt expect such fast answers, thank you for your time (and patience) <3
Topic archived. No new replies allowed.