**First of all, sorry about my English**
I'm doing this code and i want to record the struct to a file, but it's not working... When i open the programme again the information that i thought i had saved, isn't there.
I think i know the err, i am recording just the pointer and not the information, but i don't know what i should do.
Can someone, please, help me?
Thanks a lot!
Code:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define MAX1 1000
//////////////////////////////STRUCT
struct book{
char nameofbook[17];
int area;
char autor[60];
char edit[20];
};
/////////////////////////////////////////////////////////INSERT BOOKS
void insertBooks(struct book books[MAX1], int *contbooks){
int ii;
int *pont;
FILE *fptr;
FILE *keep1;
///////////////////READING POSITION
keep1=fopen("keeppointer1.txt","r");
int x=0;
while(fscanf(keep1, "%i", &x) > 0)
{
fprintf(keep1, "%i", x);
printf("\n%i", x);
printf("\nVALUE pointer");
}
///////////////////WRITING INFO
if ((fptr = fopen("filekeeping", "wb")) == NULL){
printf("\nErr");
}else{
contbooks=&x;
do{
printf("\n Name of the book: ");
gets(books[*contbooks].nameofbook);
}while(strlen(books[*contbooks].nameofbook)==0);
fwrite(&books, sizeof(book), 1, fptr);
*contbooks=*contbooks+1;
fclose(fptr);
}
///////////////////READIND INFO
if ((fptr = fopen("filekeeping", "rb")) == NULL)
printf("\nErr");
else{
while(fread(&books, sizeof(book), 1, fptr) == 1){
for(ii = 0; ii <=*contbooks-1; ii++){
printf("\nName: %s\n",books[ii].nameofbook);
}
}
fclose(fptr);
}
///////////////////WRITING POINTER
keep1=fopen("keeppointer1.txt","w");
if(keep1==NULL){
printf("\n erro");
}else{
pont=contbooks;
fprintf(keep1, "%i", *pont);
//printf("\n%i", *pont);
fclose(keep1);
}
///////////////////READING POINTER
if ((fptr = fopen("keeppointer1.txt", "r")) == NULL){
printf("\nErro");
}else{
while (!feof(keep1)){
fscanf(keep1,"%i",&*pont);
printf("\n%i",*pont);
}
}
fclose(keep1);
};////END
////////////////////////////////////////////////
main(){
struct book books[MAX1];
int opt,contbooks=0;
do{
printf("\n\n");
printf("\n Option?");
printf("\n 1 - Insert Books");
printf("\n Option: ");
scanf("%d", &opt);
fflush(stdin);
switch(opt){
case 1:{
insertBooks(books, &contbooks);
break;
}
case 0:{
break;
}
default:{
printf("\n \n =====INVALID===== \n \n");
break;
}
}
}while (opt!=0);
}