| | |
c++ files
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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: but i don't know how to create the text file from my program...
i searched for some guide, but i only found out how to open a file... like this:
c Syntax (Toggle Plain Text)
ofstream *pfile; pfile.open("myFile.txt", ios::in | ios::out);
-->sometimes i wanna take my toaster in a bath<-- •
•
•
•
c Syntax (Toggle Plain Text)
ofstream *pfile;pfile.open("myFile.txt", ios::in | ios::out);ofstream *pfile;
pfile.open("myFile.txt", ios::in | ios::out);
Use this to create a new text file
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<fstream.h> int main() { fstream file("myfile.txt,ios::in|ios::out); // Use the following to write something into the file file.write((char*)&objectname,sizeof(objectname)); }
Hope it helps!
Last edited by krnekhelesh; Aug 13th, 2007 at 2:34 am.
Ubuntu Linux User #25732
so... this is what i would do...
guess there are some mistakes in the write line... correct me if wrong plz...
c Syntax (Toggle Plain Text)
#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...
-->sometimes i wanna take my toaster in a bath<-- •
•
•
•
file.write((char*)&name,sizeof(name));
struct getname { char name[25]; }; int main() { fstream file("myfile.txt",ios::in|ios::out); getname name1; // Declaring structure variable. You could also use classes cout<<"Enter your name: "; gets(name1.name); fstream.write((char*)&name1,sizeof(name1)); }
And it is not necessary to include
file.close();
When your program ends it automatically ends the file.
I suggest that you use the a structure or a class to write a object into the fil because when you read data from the file it would be easier to get the data.
Last edited by krnekhelesh; Aug 13th, 2007 at 6:03 am.
Ubuntu Linux User #25732
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?
C++ Syntax (Toggle Plain Text)
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(); }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
but i don't know how to create the text file from my program...
•
•
•
•
When your program ends it automatically ends the file.
I think keeping symmetry makes the logic easier to follow. It's also good for consistency with things like returning from main even though you don't have to.•
•
•
•
I suggest that you use the a structure or a class to write a object into the fil because when you read data from the file it would be easier to get the data.
The truth does not change according to our ability to stomach it.
ok... i had it corrected, but it still doesn't create the file i need in to create...
here's the code:
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...
here's the code:
c Syntax (Toggle Plain Text)
#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); }
its in spanish though... hope you don't mind...
Last edited by Nichito; Aug 14th, 2007 at 7:14 pm.
-->sometimes i wanna take my toaster in a bath<-- I think it's failing to open but you don't check for that. If the file fails to open, any operations on it will do nothing so you have to check if the file is open.
C++ Syntax (Toggle Plain Text)
fstream file("Boleta.txt", ios::in | ios::out); if ( !file.is_open() ) { perror( 0 ); }
Last edited by Hamrick; Aug 14th, 2007 at 7:57 pm.
The truth does not change according to our ability to stomach it.
![]() |
Similar Threads
- hjsplit doenst work for avi files? (Windows NT / 2000 / XP)
- "Save Target As.." isn't working in IE6 (Web Browsers)
- Does Samba send deleted files to a recycle bin? (*nix Software)
- problems wid cpp files (C++)
- Dia, .PDB files (C++)
- Cannot transfer files from one hardrive to another =[ please help (Windows NT / 2000 / XP)
- FTP files (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: Node And Class
- Next Thread: C++ and data structure tutorial
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






