Array structure to files

After the first record of array the program gets an error.

Here's the code;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#define MAX_FN 50
#define MAX 3

typedef struct
{
    int id_no;
    char FName[20];
    char LName[20];
}Studrec;

typedef Studrec StudArray[MAX];

void CreateFile(char Filename[])
{
    FILE *TheFile;

    if((TheFile = fopen(Filename, "wb")) != NULL)
    {
        puts("File successfully created!");
        fclose(TheFile);
    }
    else
    {
        perror("ERROR");
        exit(EXIT_FAILURE);
    }
}

Studrec InputRecord()
{
    Studrec RecArray;
    printf("Enter ID #: ");
    fflush(stdin);
    scanf("%d", &RecArray.id_no);

    printf("Enter First Name: ");
    fflush(stdin);
    gets(RecArray.FName);

    printf("Enter Last Name: ");
    fflush(stdin);
    gets(RecArray.LName);

    return RecArray;
}

void ReadFile(char Filename[])
{
    FILE *TheFile;
    int i;
    StudArray RetrieveRec;

    if((TheFile = fopen(Filename, "rb")) != NULL)
    {
        for(i=0;i<MAX;i++)
        {
            fread(&RetrieveRec[i], sizeof(StudArray), 1, TheFile);
            puts(RetrieveRec[i].FName);
        }
        fclose(TheFile);
    }
}

void InputFile(StudArray GetRec[], char Filename[])
{
    FILE *TheFile;
    int i;

    if((TheFile = fopen(Filename, "r+b")) != NULL)
    {
        for(i=0;i<MAX;fwrite(&GetRec[i], sizeof(StudArray), 1, TheFile), i++);
        fclose(TheFile);
    }
}

int main(void)
{
    StudArray GetRec;
    char Filename[MAX_FN];
    int i;
    printf("Name for the new file: ");
    gets(Filename);

    CreateFile(Filename);

    for(i=0;i<MAX;GetRec[i] = InputRecord(), i++);
    InputFile(&GetRec,Filename);
    ReadFile(Filename);

    return 0;
}


Got a couple of questins:
1. Can I use fwrit on .txt flies?
2. Teach said it's impossible to code an array of files. Still I tried coding it but no luck, so anyone here got successfully created a program that consists of an array of flies? It could be useful for creating a virus program (hehe).

Thanks in advance... :)
Last edited on
Teach said it's impossible to code an array of files


Do you mean file names ?
I don't know about file names but yeah he said array of files.
Topic archived. No new replies allowed.