c++ files
i actually feel like a n00b asking this, but i'm a little of what you might call rusted in this stuff, since i hadn't need to handle files for a year now...
i searched for some guide, but i only found out how to open a file... like this:
ofstream *pfile;
pfile.open("myFile.txt", ios::in | ios::out);
but i don't know how tocreate the text file from my program...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
so... this is what i would do...
#include <fstream>
#include <iostream>
using namespace std;
int main(){
fstream file("myfile.txt", ios::in | ios::out);
char *name;
cout<<"Enter your name: ";
cin>>name;
file.write((char*)&name,sizeof(name));
file.close();
}
guess there are some mistakes in the write line... correct me if wrong plz...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
line 10 will cause program crash because no memory has been allocated for name -- its only a pointer that points to some random memory location. Since this is a c++ program why don't you use std::string class instread?
include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
fstream file("myfile.txt", ios::in | ios::out);
string name;
cout<<"Enter your name: ";
getline(cin,name);
file << name << endl;
file.close();
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
i had seen that one too... actually that's the one included in my project right now...
let me see what i can do, and i'll post back if i have any more trouble...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
ok... i had it corrected, but it still doesn't create the file i need in to create...
here's the code:
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <fstream>
#include <cstdio>
using namespace std;
struct carro{
char placa[7];
char nom[50];
char marca[20];
char modelo[20];
char color[10];
};
void leerVehiculo(carro cliente){
cout<<"Ingrese el nombre del propietario: ";
gets(cliente.nom);
cout<<"\nIngrese la marca del vehiculo: ";
gets(cliente.placa);
cout<<"\nIngrese el modelo del vehiculo: ";
gets(cliente.modelo);
cout<<"\nIngrese el color del vehiculo: ";
gets(cliente.color);
cout<<"\nIngrese la placa del vehiculo: ";
gets(cliente.placa);
cout<<"\n\n\nPresione >>ENTER<< para continuar";
getchar();
system("cls");
}
void nuevoCliente(){
carro cliente;
leerVehiculo(cliente);
fstream file("Boleta.txt", ios::in | ios::out);
cout<<"Proceder con escritura en archivo\n\n";
file<<"\nNombre: "<<cliente.nom;
file<<"\nMarca: "<<cliente.marca;
file<<"\nModelo: "<<cliente.modelo;
file<<"\nColor: "<<cliente.color;
file<<"\nPlaca: "<<cliente.placa;
file<<"\n\n\n";
file.close();
cout<<"Presione >>ENTER<< para continuar";
getchar();
}
int menu(){
int op;
system("cls");
cout<<"Bienvenido al Sistema de Registro de la Direccion Ejecutiva de Ingresos";
cout<<"\n\n\n\n Que desea hacer?";
cout<<"\n\n 1.-Agregar un nuevo vehiculo\n 2.-Salir\n\n\n Ingrese una opcion: ";
cin>>op;
getchar();
system("cls");
return op;
}
int main(){
do{
if (menu()==2){
_exit(0);
return 0;
}else{
nuevoCliente();
}
}while (1);
}
it runs perfectly except for that small detail (which is the whole idea of the project...)
its in spanish though... hope you don't mind...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
the thing is it doesn't even create the file... that's what i need it to do...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
i don't need it to tell me why it failed... i need it to create a new text file if there is no one to write into...
the error is "No such file or directory"
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
hmmm... idea... maybe it doesn't open it for reading, because there's nothing to read from... lol
thnx for opening my eyes...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
ok... i modified that part of the code, so the file is now crated if there wasn't any... but... now it fails writing into the file... guess it goes the other way around in the if statement...
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <fstream>
#include <cstdio>
using namespace std;
struct carro{
char placa[7];
char nom[50];
char marca[20];
char modelo[20];
char color[10];
};
void leerVehiculo(carro cliente){
cout<<"Ingrese el nombre del propietario: ";
gets(cliente.nom);
cout<<"\nIngrese la marca del vehiculo: ";
gets(cliente.placa);
cout<<"\nIngrese el modelo del vehiculo: ";
gets(cliente.modelo);
cout<<"\nIngrese el color del vehiculo: ";
gets(cliente.color);
cout<<"\nIngrese la placa del vehiculo: ";
gets(cliente.placa);
cout<<"\n\n\nPresione >>ENTER<< para continuar";
getchar();
system("cls");
}
void nuevoCliente(){
carro cliente;
leerVehiculo(cliente);
fstream file("Boleta.txt", ios::out | ios::in);
if (!file.is_open()){
fstream file ("Boleta.txt", ios::out);
}if (file.is_open()){
cout<<"Proceder con escritura en archivo\n\n";
file<<"\nNombre: "<<cliente.nom;
file<<"\nMarca: "<<cliente.marca;
file<<"\nModelo: "<<cliente.modelo;
file<<"\nColor: "<<cliente.color;
file<<"\nPlaca: "<<cliente.placa;
file<<"\n\n\n";
file.close();
}else perror(0);
cout<<"Presione >>ENTER<< para continuar";
getchar();
}
int menu(){
int op;
system("cls");
cout<<"Bienvenido al Sistema de Registro de la Direccion Ejecutiva de Ingresos";
cout<<"\n\n\n\n Que desea hacer?";
cout<<"\n\n 1.-Agregar un nuevo vehiculo\n 2.-Salir\n\n\n Ingrese una opcion: ";
cin>>op;
getchar();
system("cls");
return op;
}
int main(){
do{
if (menu()==2){
_exit(0);
return 0;
}else{
nuevoCliente();
}
}while (1);
}
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57