Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
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?
 

MongoTheGeek

macrumors 68040
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?

The file should appear in your current path. If not you may want to take a peek in /tmp
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
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();
}
 

MongoTheGeek

macrumors 68040
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?

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.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
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?

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.
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
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.
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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.
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.
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
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.
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
As i said before, I get thte directory bit. Imy current directory is the place where the program is. IT runs fine. i just can't file 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.
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.
 

christo07

macrumors newbie
Sep 25, 2007
2
0
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
 

makku

macrumors member
Mar 22, 2006
60
0
Where is the binary located? Unless it is under your home directory you have to give it permission to create files with chmod.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
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.
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
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.

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.
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
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.

He was using <stuff> as an indication to fill in your own stuff there.
You want system("cd ~/Desktop");
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
He was using <stuff> as an indication to fill in your own stuff there.
You want system("cd ~/Desktop");


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?
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
So did you try chmod? Where is the binary located?

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.
:(
 

makku

macrumors member
Mar 22, 2006
60
0
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.
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
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...
 

stupidregister

macrumors member
Sep 29, 2007
52
0
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();
}
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
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.
 

stupidregister

macrumors member
Sep 29, 2007
52
0
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.

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.