How can you add and load char[]'s to/from a structure then?
Code:
#include <stdio.h>
#include <conio.h>
typedef struct
{
char[10] Var1;
}MYSTRUCTURE;
int main()
{
MYSTRUCTURE Structure1;
FILE* WriteFile;
Structure1.Var1="sghrdhj";
WriteFile=fopen("Myfile.tmp", "wb");
if(WriteFile!=NULL)
{
//Write data
fwrite(&Structure1, sizeof(MYSTRUCTURE), 1, WriteFile);
fclose(WriteFile);
}
else
{
//Error
printf("Error writing file!");
getch();
return 0;
}
MYSTRUCTURE Structure2;
FILE* ReadFile;
ReadFile=fopen("Myfile.tmp", "rb");
if(ReadFile!=NULL)
{
//Read data
fread(&Structure2, sizeof(MYSTRUCTURE), 1, ReadFile);
fclose(ReadFile);
}
else
{
//Error
printf("Error reading file!");
getch();
return 0;
}
//Print the result
printf("value: %d", Structure2.Var1);
getch();
return 0;
}
Certainly doesn't work. Even if it did, the characters of the string would be read with their numerical values.
I need to know how to do this because I want to add and load questions, example:
Code:
struct Database
{
int history_qs_n_ans_number;
char history_qs[1000];
char history_ans[1000];
char note[1000];
};
Database Religious_history[] =
{
{ 1, "Who founded the religion of Zoroastrianism?", "Zarathustra", "The belief of when Zarathustra, or Zoroaster lived, has varied greatly over the years, he is now dated to about 1000 BC. There are some people who even questionhis existence." }, // #1 initialized.
{ 2, "Which was the official religion in the empire of Han(206 BC - 220 AD)?", "Konfucianism", "Konfucianism remains from the belief of the philiosoph Konfucius" }, // #2 initialized.
{ 3, "Which year did Muhammed escape from Mecca?" } // #3 initializexd.
};
How can all this succefully be added and loaded from/to a file?