943,993 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1837
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 13th, 2007
0

c++ files

Expand Post »
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...
Similar Threads
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Aug 13th, 2007
0

Re: c++ files

Quote ...
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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 31
Solved Threads: 3
Junior Poster
krnekhelesh is offline Offline
127 posts
since Jul 2007
Aug 13th, 2007
0

Re: c++ files

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...
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Aug 13th, 2007
0

Re: c++ files

Quote ...
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.
Reputation Points: 31
Solved Threads: 3
Junior Poster
krnekhelesh is offline Offline
127 posts
since Jul 2007
Aug 13th, 2007
0

Re: c++ files

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)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 13th, 2007
0

Re: c++ files

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...
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Aug 13th, 2007
1

Re: c++ files

Quote ...
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.

Quote ...
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.

Quote ...
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.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 14th, 2007
0

Re: c++ files

wow, there's so much I have to learn in C++!
Reputation Points: 31
Solved Threads: 3
Junior Poster
krnekhelesh is offline Offline
127 posts
since Jul 2007
Aug 14th, 2007
0

Re: c++ files

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.
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Aug 14th, 2007
0

Re: c++ files

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)
  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.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Node And Class
Next Thread in C++ Forum Timeline: C++ and data structure tutorial





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC