Binary C++ files in OS X

Discussion in 'Mac Programming' started by Quboid, Sep 25, 2007.

  1. Quboid macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #1
    I am writing a program in c++ in which i am creating and manipulating a binary file. Now normally in windows the file is created in the same folder as the source code. But on osx (compiling from the terminal) its seems as if the file is not even created (it doesn't show up in spotlight). Do i have to specify a path to which i want to store the file, or what am i doing wrong?
     
  2. MongoTheGeek macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #2
    The file should appear in your current path. If not you may want to take a peek in /tmp
     
  3. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #3
    This is a part of the code where i am openening( creating) the binary file. But i have no definded path. I'm not sure what /tmp is. But is th code i have there right for osx?



    void altas(){

    int limit;
    if(!arch){
    arch.open("books.dat",ios::binary|ios::in|ios::eek:ut);

    cout<<"Number of registers to enter?"<<endl;
    cin>>limit;
    for (int i=0;i<limit;i++){
    system("clear");
    cout<<"Datos para la llave"<<reg.llave<<endl<<endl;
    cout<<"titulo"<<endl;
    fflush(stdin);
    cin>>reg.titulo;
    cout<<"ISBN"<<endl;
    cin>>reg.ISBN;
    cout<<"Precio"<<endl;
    cin>>reg.precio;
    cout<<"Cantidad"<<endl;
    cin>>reg.cantidad;
    cout<<"editorial"<<endl;
    cin>>reg.editorial;
    arch.seekp((reg.llave)*sizeof(library));
    arch.write( reinterpret_cast<char*> (&reg),sizeof(library));
    reg.llave++;
    }
    }
    arch.close();
    }
     
  4. MongoTheGeek macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #4
    I am not really a C++ person. When you run the executable how are you running it? From XCode? Command Line?


    / is the top of the unix file hierarchy. /tmp is a hidden directory used for temporary items.
     
  5. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #5
    The path is whereever you're executing the program. If you execute the program from its own directory, then that is the current directory. But you can run a program from anywhere. If you ran it from your home then the current directory would be your home.
     
  6. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #6
    I usdestand the directory bit. I am runing the program from the terminal. I jus don't see teh file being created.

    Thats the only problem.
     
  7. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #7
    By default the Terminal sets your working directory to ~, your home folder. Look in there for the file, unless you cd to some other directory to run the program first - if you do the file will be in there.

    Also, you haven't accidentally given the file a name that starts with a period, have you? Those files are hidden in the Finder by default.
     
  8. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #8
    As i said before, I get thte directory bit. My current directory is the place where the program is. IT runs fine. i just can't find the binary file (fstream) that i am created. This is the bit of code where the file is created.

    void altas(){

    int limit;
    if(!arch){
    arch.open("books.dat",ios::binary|ios::in|ios:ut);

    cout<<"Number of registers to enter?"<<endl;
    cin>>limit;
    for (int i=0;i<limit;i++){
    system("clear");
    cout<<"Datos para la llave"<<reg.llave<<endl<<endl;
    cout<<"titulo"<<endl;
    fflush(stdin);
    cin>>reg.titulo;
    cout<<"ISBN"<<endl;
    cin>>reg.ISBN;
    cout<<"Precio"<<endl;
    cin>>reg.precio;
    cout<<"Cantidad"<<endl;
    cin>>reg.cantidad;
    cout<<"editorial"<<endl;
    cin>>reg.editorial;
    arch.seekp((reg.llave)*sizeof(library));
    arch.write( reinterpret_cast<char*> (&reg),sizeof(library));
    reg.llave++;
    }
    }
    arch.close();
    }


    cheeers.
     
  9. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #9
    Your code looks just fine, though you should have put it in CODE tags so the forum software doesn't try to insert smilies in there.

    It appears to me that your file should be created in the same directory as the program is run in, since no path is specified. Try this, from the Terminal, while in the directory you normally run the program from:
    Code:
    ls -l
    If the listing contains an entry called books.dat then you've just found your file.
     
  10. christo07 macrumors newbie

    Joined:
    Sep 25, 2007
    #10
    find

    Typing at the command shell
    find ~ /tmp /var -name the_file_name
    should reveal the the location

    If not then search for recently changed files

    find ~ /tmp /var -cmin -5
    To list files that have been changed in the last 5 min.

    Christoph
     
  11. makku macrumors member

    Joined:
    Mar 22, 2006
    #11
    Where is the binary located? Unless it is under your home directory you have to give it permission to create files with chmod.
     
  12. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #12
    Instead of searching for the file, create it where you want it. There is a command in the terminal called "cd" that changes the current directory.

    In your application, type

    Code:
    system("cd <the/path/to/a/directory/">)
    and your file will be created in there.
     
  13. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #13
    I am getting an error when i put that line in my code. I inserted it right above where i open/creat the file. I assumed that the qoutation marks closed right before the parenthsis close like so:

    system("cd </desktop>")

    these are the erros messages.
    sh: -c: line 1: syntax error near unexpected token `newline'
    sh: -c: line 1: `cd </desktop>'

    am i making a mistake.
     
  14. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #14
    He was using <stuff> as an indication to fill in your own stuff there.
    You want system("cd ~/Desktop");
     
  15. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #15

    Even like this it doesn't work. I can't belive that i cannot get this work. All i need is a way to save my binary file from c++ on my computer. Can someone try my code on their system and see if creates the file?
     
  16. makku macrumors member

    Joined:
    Mar 22, 2006
    #16
    So did you try chmod? Where is the binary located?
     
  17. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #17
    the binary is not yet located. I am trying to create it. But so far that seems impossible. Doesn't anyone have some sort of sample c++ code that actaully works to create a binary file from the terminal.
    :(
     
  18. makku macrumors member

    Joined:
    Mar 22, 2006
    #18
    Sorry about that, I meant the binary that you compiled. Try chmod to 777 and give it a write access and it should be fine. Good luck.
     
  19. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #19
    I am about to give up now. Restart into bootcamp and reluctantly go back to programming in windows, here's my last blow at this. All my code up to writing in the files.

    Code:
    
    #include<iostream>
    #include<fstream>
    #include<curses.h>
    
    using namespace std;
    fstream arch;
    
    struct library{
    	char titulo[20];
       char ISBN[20];
       int precio;
       int cantidad;
       char editorial;
       int llave;
       };
    
    library reg;
    void menu();
    void altas();
    void bajas();
    void modificar();
    void consultas();
    void consultas2();
    
    
    
    
    
    int main(int argc, char* argv[]){
    	menu();
       }
    
    void menu(){
    	system("clear");
          char opt;
          do{
          system("clear");
          cout<<"$$$$MENU$$$$"<<endl<<endl;
          cout<<"1.altas!"<<endl;
          cout<<"2.Bajas!"<<endl;
          cout<<"3.consultas"<<endl;
          cout<<"4.Modificacion"<<endl;
          cout<<"x.salir"<<endl;
          fflush(stdin);
    	cin.ignore();
          cin>>opt;
    		if (opt=='1'){
             altas();
             } 
          if(opt=='2'){
          	bajas();
             }
        /*  if(opt=='3'){
          	consultas();
             }  */
          }while(opt!='x');
    }
    
    void altas(){
    
    		int limit;
         // if(!arch){
    		system("cd ~/Desktop");
          arch.open("books.dat",ios::binary|ios::in|ios::out);
    
          cout<<"Number of registers to enter?"<<endl;
          cin>>limit;
          for (int i=0;i<limit;i++){
    			system("clear");
          	 cout<<"Datos para la llave"<<reg.llave<<endl<<endl;
              cout<<"titulo"<<endl;
              fflush(stdin);
              cin>>reg.titulo;
              cout<<"ISBN"<<endl;
              cin>>reg.ISBN;
              cout<<"Precio"<<endl;
              cin>>reg.precio;
              cout<<"Cantidad"<<endl;
              cin>>reg.cantidad;
              cout<<"editorial"<<endl;
              cin>>reg.editorial;
              arch.seekp((reg.llave)*sizeof(library));
              arch.write( reinterpret_cast<char*> (&reg),sizeof(library));
              reg.llave++;
              //}
              }
    	arch.close();
    }
    
    
    hoping against hope...
     
  20. stupidregister macrumors member

    Joined:
    Sep 29, 2007
    #20
    According to this ( http://gcc.gnu.org/ml/gcc-prs/2002-02/msg00189.html ), this is occurring because of the C++ standard. If it works in Windows, its because Microsoft is ignoring the standard (like usual). To create a file that doesn't exist, you have to be in
    Code:
    ios::out
    mode. You can only use
    Code:
    ios::in|ios::out
    to write to file if the file already exists.

    Here is a modified version of your code that works. I removed the unneeded system call.

    Code:
    #include<iostream>
    #include<fstream>
    #include<curses.h>
    
    using namespace std;
    fstream arch;
    
    struct library{
    	char titulo[20];
       char ISBN[20];
       int precio;
       int cantidad;
       char editorial;
       int llave;
       };
    
    library reg;
    void menu();
    void altas();
    void bajas();
    void modificar();
    void consultas();
    void consultas2();
    
    
    
    
    
    int main(int argc, char* argv[]){
    	menu();
       }
    
    void menu(){
    	system("clear");
          char opt;
          do{
          system("clear");
          cout<<"$$$$MENU$$$$"<<endl<<endl;
          cout<<"1.altas!"<<endl;
          cout<<"2.Bajas!"<<endl;
          cout<<"3.consultas"<<endl;
          cout<<"4.Modificacion"<<endl;
          cout<<"x.salir"<<endl;
          fflush(stdin);
    	cin.ignore();
          cin>>opt;
    		if (opt=='1'){
             altas();
             } 
          if(opt=='2'){
          	//bajas();
             }
        /*  if(opt=='3'){
          	consultas();
             }  */
          }while(opt!='x');
    }
    
    void altas(){
    
    		int limit;
          arch.open("books.dat",ios::binary|ios::out);
    
          cout<<"Number of registers to enter?"<<endl;
          cin>>limit;
          for (int i=0;i<limit;i++){
    			system("clear");
          	 cout<<"Datos para la llave"<<reg.llave<<endl<<endl;
              cout<<"titulo"<<endl;
              fflush(stdin);
              cin>>reg.titulo;
              cout<<"ISBN"<<endl;
              cin>>reg.ISBN;
              cout<<"Precio"<<endl;
              cin>>reg.precio;
              cout<<"Cantidad"<<endl;
              cin>>reg.cantidad;
              cout<<"editorial"<<endl;
              cin>>reg.editorial;
              arch.seekp((reg.llave)*sizeof(library));
              arch.write( reinterpret_cast<char*> (&reg),sizeof(library));
              reg.llave++;
              //}
              }
    	arch.close();
    }
    
     
  21. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #21
    Thank you so much stupidregister and everybody else. Now all i have to do is find a way to modify this function so that it doesn't overwirte everytime i call i submit register. Thank!!!!

    qbo.
     
  22. stupidregister macrumors member

    Joined:
    Sep 29, 2007
    #22
    If I understand what you want to do correctly, I think
    Code:
    ios::app
    will do what you need. So just modify your
    Code:
    ios::binary|ios::out
    to
    Code:
    ios::binary|ios::out|ios::app
    . This will write to the end of the file rather than overwriting the file.

    This is a good resource: http://www.cplusplus.com/reference/iostream/ofstream/open.html
     

Share This Page