c++ files

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

c++ files

 
0
  #1
Aug 13th, 2007
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:
  1. ofstream *pfile;
  2. pfile.open("myFile.txt", ios::in | ios::out);
but i don't know how to create the text file from my program...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 126
Reputation: krnekhelesh is an unknown quantity at this point 
Solved Threads: 3
krnekhelesh's Avatar
krnekhelesh krnekhelesh is offline Offline
Junior Poster

Re: c++ files

 
0
  #2
Aug 13th, 2007
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);
The code you wrote just opens the file if it is available but does not create a new one.

Use this to create a new text file
  1. #include<iostream.h>
  2. #include<fstream.h>
  3.  
  4. int main()
  5. {
  6. fstream file("myfile.txt,ios::in|ios::out);
  7. // Use the following to write something into the file
  8. file.write((char*)&objectname,sizeof(objectname));
  9. }
  10.  

Hope it helps!
Last edited by krnekhelesh; Aug 13th, 2007 at 2:34 am.
Ubuntu Linux User #25732
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: c++ files

 
0
  #3
Aug 13th, 2007
so... this is what i would do...
  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7. fstream file("myfile.txt", ios::in | ios::out);
  8. char *name;
  9. cout<<"Enter your name: ";
  10. cin>>name;
  11. file.write((char*)&name,sizeof(name));
  12. file.close();
  13. }

guess there are some mistakes in the write line... correct me if wrong plz...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 126
Reputation: krnekhelesh is an unknown quantity at this point 
Solved Threads: 3
krnekhelesh's Avatar
krnekhelesh krnekhelesh is offline Offline
Junior Poster

Re: c++ files

 
0
  #4
Aug 13th, 2007
file.write((char*)&name,sizeof(name));
I think it is better you do this

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: c++ files

 
0
  #5
Aug 13th, 2007
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?
  1. include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main(){
  8. fstream file("myfile.txt", ios::in | ios::out);
  9. string name;
  10. cout<<"Enter your name: ";
  11. getline(cin,name);
  12. file << name << endl;
  13. file.close();
  14. }
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: c++ files

 
0
  #6
Aug 13th, 2007
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...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: c++ files

 
1
  #7
Aug 13th, 2007
but i don't know how to create the text file from my program...
If you open a file for reading, C++ assumes the file exists. If you open it for writing, the file is created if it doesn't exist. If your open mode contains ios::out or ios::app then it's opened for writing and the file is created if it doesn't exist.

When your program ends it automatically ends the file.
Just because C++ does things for you doesn't mean you should take advantage of it. 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.
Yeah, but if the object isn't a POD type the program might or might not work. It's safer to serialize the object into a character array or xml or something like that to avoid problems and still keep it simple. And it's better to use text than binary because it's portable and readable outside of the application.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 126
Reputation: krnekhelesh is an unknown quantity at this point 
Solved Threads: 3
krnekhelesh's Avatar
krnekhelesh krnekhelesh is offline Offline
Junior Poster

Re: c++ files

 
0
  #8
Aug 14th, 2007
wow, there's so much I have to learn in C++!
Ubuntu Linux User #25732
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: c++ files

 
0
  #9
Aug 14th, 2007
ok... i had it corrected, but it still doesn't create the file i need in to create...

here's the code:
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <conio.h>
  4. #include <fstream>
  5. #include <cstdio>
  6.  
  7. using namespace std;
  8.  
  9. struct carro{
  10. char placa[7];
  11. char nom[50];
  12. char marca[20];
  13. char modelo[20];
  14. char color[10];
  15. };
  16.  
  17. void leerVehiculo(carro cliente){
  18. cout<<"Ingrese el nombre del propietario: ";
  19. gets(cliente.nom);
  20. cout<<"\nIngrese la marca del vehiculo: ";
  21. gets(cliente.placa);
  22. cout<<"\nIngrese el modelo del vehiculo: ";
  23. gets(cliente.modelo);
  24. cout<<"\nIngrese el color del vehiculo: ";
  25. gets(cliente.color);
  26. cout<<"\nIngrese la placa del vehiculo: ";
  27. gets(cliente.placa);
  28. cout<<"\n\n\nPresione >>ENTER<< para continuar";
  29. getchar();
  30. system("cls");
  31. }
  32.  
  33. void nuevoCliente(){
  34. carro cliente;
  35. leerVehiculo(cliente);
  36. fstream file("Boleta.txt", ios::in | ios::out);
  37. cout<<"Proceder con escritura en archivo\n\n";
  38. file<<"\nNombre: "<<cliente.nom;
  39. file<<"\nMarca: "<<cliente.marca;
  40. file<<"\nModelo: "<<cliente.modelo;
  41. file<<"\nColor: "<<cliente.color;
  42. file<<"\nPlaca: "<<cliente.placa;
  43. file<<"\n\n\n";
  44. file.close();
  45. cout<<"Presione >>ENTER<< para continuar";
  46. getchar();
  47. }
  48.  
  49. int menu(){
  50. int op;
  51. system("cls");
  52. cout<<"Bienvenido al Sistema de Registro de la Direccion Ejecutiva de Ingresos";
  53. cout<<"\n\n\n\n Que desea hacer?";
  54. cout<<"\n\n 1.-Agregar un nuevo vehiculo\n 2.-Salir\n\n\n Ingrese una opcion: ";
  55. cin>>op;
  56. getchar();
  57. system("cls");
  58. return op;
  59. }
  60.  
  61. int main(){
  62. do{
  63. if (menu()==2){
  64. _exit(0);
  65. return 0;
  66. }else{
  67. nuevoCliente();
  68. }
  69. }while (1);
  70. }
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...
Last edited by Nichito; Aug 14th, 2007 at 7:14 pm.
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: c++ files

 
0
  #10
Aug 14th, 2007
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.
  1. fstream file("Boleta.txt", ios::in | ios::out);
  2.  
  3. if ( !file.is_open() ) {
  4. perror( 0 );
  5. }
Last edited by Hamrick; Aug 14th, 2007 at 7:57 pm.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC