Hey, I'm creating a kind of database for a College project that stores values in a txt file. The thing is, it's currently set to "values.txt" and everytime the program runs, it over-writes it.

Basically what I want it to do is ask create a .txt file depending on the user_id, for example "00011.txt" etc. Here's the code:

char Customer::addCustomer()
  ofstream file (customer_id);
  if (file.is_open())
    myfile << "Customer Name: " << customer_name << endl;
    myfile << "Customer ID: " << customer_id << endl;
    myfile << "Customer Address " << customer_address << endl;
    return true;
    return false;

Thanks for any help :) x

7 Years
Discussion Span
Last Post by Ancient Dragon

there is many functions working with files

1) stdio.h

FILE *fp;
fp=fopen("text.txt","ab"); // ab = add binary
                                                              // wb write binary
                                                              // rb  read binary
//get file size
int i;
for (int i=0; fgetc(fp)!=EOF; i++) {}       // EOF end of file
// the function is reading file byte by byte
char *text=new char[i];       // creat file size memory

fseek(fp,0,SEEK_SET);        //sets file read begin

//now read file
for(int j=0; j<=i; j++)
 text[j]= fgetc(fp);
fclose(fp);     //close file

2) fstream.h

ifstream fin;   
fin.open("Stats.txt");          //opening  file
char text[30];
fin >>text;                       // read from file
cout<< text;
fin.seekg(NULL, ios::beg);  //sets file read begin
getline(fin, text);   // This gets a line of the text

fin.close();  // close file

3) windows.h my feivorite it is faster and poverful

HANDLE hfile;   // handel for file
DWORD  din;    //just need

hfile=CreateFile("file.txt",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);

//creating file
// OPEN_EXISTING opens file if it exist
//els we need CREATE_ALWAYS it will creat file always

int size=GetFileSize(hfile,0);  // gets file size

char *text=new char[20];
ReadFile( hfile,text,sizeof(char)*20,&din,0); 
// reads from file, 3 parametr is lengs of reading object 

 // sets file read begin

CloseHandle(hfile);    // close file

Edited by mike_2000_17: Fixed formatting


Is customer_id a string or an integer?

int customer_id = 11;
char filename[255];
sprintf(filename,"%04d.txt", customer_id);
ofstream file (filename);

There are other ways to format the string, but IMHO the above is the simplest.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.