DiegoTc
January 14th, 2010, 02:23 PM
Hi everyone, I am working on trying a small file program.
Only add numbers from keyboard and later saved them on an array.
Just for starting I am using an array, but when I write the file, and read it, they appear some stranges numbers. The array is of size 5. And it has 8 numbers the file
Here is the code
void leer(int arr[],int op){
if(op==1){
FILE *arch=0;
arch=fopen("archivo.gedit","wb");
fwrite(&arr,1,sizeof(arr),arch);
fclose(arch);
}
else if(op==2){
FILE *arch=0;
arch=fopen("archivoB.gedit","wb");
fwrite(&arr,1,sizeof(arr),arch);
fclose(arch);
}
else if(op==3){
FILE *arch=0;
arch=fopen("archivoC.gedit","wb");
fwrite(&arr,1,sizeof(arr),arch);
fclose(arch);
}
}
void escribir(int arr[],int op){
if(op==1){
FILE *arch=0;
arch=fopen("archivo.gedit","rb");
if(arch==0){
cout<<"Error";
}
fread(&arr,1,sizeof(arr),arch);
fclose(arch);
cout<<"Estos son los del A \n";
for(int a=0;a<sizeof(arr);a++)
cout<<arr[a]<<" ";
cout<<"\n";
}
else if(op==2){
FILE *arch=0;
arch=fopen("archivoB.gedit","rb");
if(arch==0){
cout<<"Error";
}
fread(&arr,1,sizeof(arr),arch);
fclose(arch);
cout<<"Estos son los del B \n";
for(int a=0;a<sizeof(arr);a++)
cout<<arr[a]<<" ";
cout<<"\n";
}
else if(op==3){
FILE *arch=0;
arch=fopen("archivoC.gedit","rb");
if(arch==0){
cout<<"Error";
}
fread(&arr,1,sizeof(arr),arch);
fclose(arch);
cout<<"Estos son los del C \n";
for(int a=0;a<sizeof(arr);a++)
cout<<arr[a]<<" ";
cout<<"\n";
}
}
int main(){
int op,contA=0,contB=0,dato,datos;
archivos practica;
int arrA[5];
int arrB[5];
int arrC[100];
do{
cout<<"-------Menu--------\n";
cout<<"1. Data to FileA\n";
cout<<"2. Data to File B\n";
cout<<"3. Read Data from File A and B\n";
//cout<<"4. Mostrar Nuevos Datos\n";
cout<<"5. Salir\n";
cin>>op;
if(op==1){
if(contA<5){
cout<<"Ingrese numero: ";
cin>>arrA[contA];
contA++;
datos=op;
}
}
else if(op==2){
if(contB<5){
cout<<"Ingrese numero: ";
cin>>arrB[contB];
contB++;
datos=op;
}
}
else if(op==3){
practica.leer(arrA,1);
practica.leer(arrB,2);
practica.escribir(arrA,1);
practica.escribir(arrB,2);
}
}while(op!=5);
}
If i write these number on the arrys it prints me this:
File A={1}
It tell me the file has the following numbers=1,32570,4198688,0,0,0,2,0
FIle B={3,4,5,6}
It tell me the file has the following numbers=3,4,5,6,2081045584,4198757,0
Only add numbers from keyboard and later saved them on an array.
Just for starting I am using an array, but when I write the file, and read it, they appear some stranges numbers. The array is of size 5. And it has 8 numbers the file
Here is the code
void leer(int arr[],int op){
if(op==1){
FILE *arch=0;
arch=fopen("archivo.gedit","wb");
fwrite(&arr,1,sizeof(arr),arch);
fclose(arch);
}
else if(op==2){
FILE *arch=0;
arch=fopen("archivoB.gedit","wb");
fwrite(&arr,1,sizeof(arr),arch);
fclose(arch);
}
else if(op==3){
FILE *arch=0;
arch=fopen("archivoC.gedit","wb");
fwrite(&arr,1,sizeof(arr),arch);
fclose(arch);
}
}
void escribir(int arr[],int op){
if(op==1){
FILE *arch=0;
arch=fopen("archivo.gedit","rb");
if(arch==0){
cout<<"Error";
}
fread(&arr,1,sizeof(arr),arch);
fclose(arch);
cout<<"Estos son los del A \n";
for(int a=0;a<sizeof(arr);a++)
cout<<arr[a]<<" ";
cout<<"\n";
}
else if(op==2){
FILE *arch=0;
arch=fopen("archivoB.gedit","rb");
if(arch==0){
cout<<"Error";
}
fread(&arr,1,sizeof(arr),arch);
fclose(arch);
cout<<"Estos son los del B \n";
for(int a=0;a<sizeof(arr);a++)
cout<<arr[a]<<" ";
cout<<"\n";
}
else if(op==3){
FILE *arch=0;
arch=fopen("archivoC.gedit","rb");
if(arch==0){
cout<<"Error";
}
fread(&arr,1,sizeof(arr),arch);
fclose(arch);
cout<<"Estos son los del C \n";
for(int a=0;a<sizeof(arr);a++)
cout<<arr[a]<<" ";
cout<<"\n";
}
}
int main(){
int op,contA=0,contB=0,dato,datos;
archivos practica;
int arrA[5];
int arrB[5];
int arrC[100];
do{
cout<<"-------Menu--------\n";
cout<<"1. Data to FileA\n";
cout<<"2. Data to File B\n";
cout<<"3. Read Data from File A and B\n";
//cout<<"4. Mostrar Nuevos Datos\n";
cout<<"5. Salir\n";
cin>>op;
if(op==1){
if(contA<5){
cout<<"Ingrese numero: ";
cin>>arrA[contA];
contA++;
datos=op;
}
}
else if(op==2){
if(contB<5){
cout<<"Ingrese numero: ";
cin>>arrB[contB];
contB++;
datos=op;
}
}
else if(op==3){
practica.leer(arrA,1);
practica.leer(arrB,2);
practica.escribir(arrA,1);
practica.escribir(arrB,2);
}
}while(op!=5);
}
If i write these number on the arrys it prints me this:
File A={1}
It tell me the file has the following numbers=1,32570,4198688,0,0,0,2,0
FIle B={3,4,5,6}
It tell me the file has the following numbers=3,4,5,6,2081045584,4198757,0