write to files a linked list of structures

My problem is as follows: write to files a linked list of structures to save a database and then later you can open that file, read the structures, recreating the linked list and work on it.
In what ways I open the file? What is the function used to write data?
If you can post some sample code, thanks.
Have you searched the forum for anything related to a Linked List?

Open and reading of files are in the tutorials around this sight, and many posts cover them. Writing to them is just opening them differently and writing and the pattern is much the same as reading.

Putting sample code up would be answering your entire homework for you.
search for ifstream and ofstream .
I've tried to write every items of the structures. This is 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void read()
{
    char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '\0';

    totc = 0;
    if(fr = fopen(path, "r"))
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;
        char v[10];

        fscanf(fr, "%i%*c", nn->code);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '\0';

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '\0';

        fscanf(fr, "%i%*c", &nn->disp);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            fscanf(fr, "%i%*c", &nn->code);

            ch = getc(fr);
            while(ch != '\n' && ch != 1)
            {
                nn->name[i++] = ch;
                ch = getc(fr);
            }
            nn->name[i] = '\0';

            ch = getchar();
            while(ch != '\n' && ch != 1)
            {
                nn->des[i++] = ch;
                ch = getc(fr);
            }
            nn->des[i] = '\0';

            fscanf(fr, "%i%*c", &nn->disp);

            nn->n = c;
            p->n = nn;
            totc++;
        }
    }
    fclose(fr);
    fflush(NULL);
}

void write()
{
char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '\0';

    if(fw = fopen(path, "w"))
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);;
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
    fflush(NULL);
}


It returns error in read();
Read function can be written in two ways .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void read(char* path)
{
	ifstream text(path, ios::in);        //Opendata3.txt  for read-in through the <fstream.h>                  
	comp nn ; 
	i = 0;
	do                                          //Do while loop is used to place data
	{      text>>n.code[i];                                     //from open stream into storage.
		text>>nn.name; 
		text>>nn.des;
		text>>nn.desp[i];
		i++;
	}	while(nn.des[i]>0);
	i-=2;
	text.close();                               //Closes open stream.
}


and
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void read( char* path)
{
	comp nn[100]; 
    ifstream fin;
    fin.open(path);
    cout <<endl;
 
    int i= 0;
     if(!fin.is_open())
            {
                    cout<<"Sorry, No file found"<<endl;
            }
            else{
                    while (!fin.eof())
                    {
                          getline( fin, nn[i].code ) ; 
                          getline(fin,nn[i].name);             
						  getline(fin,nn[i].des);
						  getline(fin,nn[i].desp);
						  i++;
                   };
               }
    fin.close();
}
Last edited on
"Code" corresponds to the component code and is an integer, "name" to its name and is a character array, "des" and its description is a vector of characters and "disp" to its availability and is an integer. Perciòil your code does not make sense.
I have edited the above code .. please look at the second read function ,... .
I solved. Here is the complete 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
struct comp
{
    int code;
    char name[27];
    char des[102];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fr;
    struct comp *nn = calloc(1, sizeof(struct comp)), *p = 0;

    fputs("\nInserisci il percorso e il nome del file: ", stderr);

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '\0';

    if((fr = fopen(path, "rb")) != NULL)
    {
        comps = nn;

        //-----------------------------------------------------------------   prima struttura
        fread(&nn->code, sizeof(int), 1, fr);

        i = 0;
        ch = getc(fr);
        do
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        while(ch != '\0');

        i = 0;
        ch = getc(fr);
        do
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        while(ch != '\0');

        fread(&nn->disp, sizeof(int), 1, fr);

        getc(fr);

        p = nn;
        nn = calloc(1, sizeof(struct comp));
        //-----------------------------------------------------------------   fine prima struttura

        while((ch = getc(fr)) && (!feof(fr)) && ungetc(ch, fr))
        {
            fread(&nn->code, sizeof(int), 1, fr);

            i = 0;
            ch = getc(fr);
            do
            {
                nn->name[i++] = ch;
                ch = getc(fr);
            }
            while(ch != '\0');

            i = 0;
            ch = getc(fr);
            do
            {
                nn->des[i++] = ch;
                ch = getc(fr);
            }
            while(ch != '\0');

            fread(&nn->disp, sizeof(int), 1, fr);

            getc(fr);

            p->n = nn;
            p = nn;
            nn = calloc(1, sizeof(struct comp));
        }
        p->n = 0;
    }

    fclose(fr);
    free(path);
    free(nn);
}

void salva()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fputs("\nInserisci il percorso e il nome del file: ", stderr);

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '\0';

    if((fw = fopen(path, "wb")) != NULL)
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc('\0', fw);
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc('\0', fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }

    fclose(fw);
    free(path);
}


Thanks to all for your help.
Topic archived. No new replies allowed.