How to save text to a file extension.txt?

I wrote this code and it works, the problem comes when I insert other data in txt file the first thing I inserted is deleted. How to store the old data in same file.

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
132
133
134
135
136
137
138
/// Data Base
#include <stdio.h>
#include <windows.h>
#include <string.h>

void premenu();
void menu();

struct address
{
    char name[50];
    char lastname[50];
    char city[50];
    char country[50];
    char tel[20];
}a1, a2, a3, a4, a5;

unsigned options;
FILE *file;

int main()
{
    premenu();

    for(int i = 0; i < options;)
    {
        if(options == 1)
        {
            menu();
        }
        else if(options == 2)
        {
            printf("\n\n Exiting Data Base");
            Sleep(1500);
            system("cls");
            break;
        }
        else
        {
            printf("\n\n Invalid option.. Choose 1 or 2");
            Sleep(1500);
            system("cls");
            premenu();
        }
    }
    return 0;
}

void premenu()
{
    int temp, status;
    printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
    printf("\n\n\n\tChoose an option:\n\n");
    printf("\n 1. Input address");
    printf("\n 2. Exit Data Base");
    printf("\n\n Select: ");
    status = scanf("%d", &options);
    while(status != 1)
    {
        while((temp = getchar()) != EOF && temp != '\n');
        {
            printf("\n\n Invalid option.. Choose 1 or 2");
            Sleep(1500);
            system("cls");
            printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
            printf("\n\n\n\tChoose an option:\n\n");
            printf("\n 1. Input address");
            printf("\n 2. Exit Data Base");
            printf("\n\n Select: ");
            status = scanf("%d", &options);
        }
    }
}

void menu()
{
    file = fopen("records.txt", "wt");

    if(!file)
    {
        printf("\n File could not be opened\n\n make sure the file is not read only\a");
        Sleep(2500);
        system("cls");
        premenu();
    }

    for(int i = 0; i < 1; ++i)
    {
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       ");
        scanf("%s", a1.name);
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       %s", a1.name);
        printf("\n\n\tLast name:  ");
        scanf("%s", a2.lastname);
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       %s", a1.name);
        printf("\n\n\tLast name:  %s", a2.lastname);
        printf("\n\n\tCity:       ");
        scanf("%s", a3.city);
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       %s", a1.name);
        printf("\n\n\tLast name:  %s", a2.lastname);
        printf("\n\n\tCity:       %s", a3.city);
        printf("\n\n\tCountry:    ");
        scanf("%s", a4.country);
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       %s", a1.name);
        printf("\n\n\tLast name:  %s", a2.lastname);
        printf("\n\n\tCity:       %s", a3.city);
        printf("\n\n\tCountry:    %s", a4.country);
        printf("\n\n\tPhone:      ");
        scanf("%s", a5.tel);
        system("cls");
        printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
        printf("\n\n\n\tName:       %s", a1.name);
        printf("\n\n\tLast name:  %s", a2.lastname);
        printf("\n\n\tCity:       %s", a3.city);
        printf("\n\n\tCountry:    %s", a4.country);
        printf("\n\n\tPhone:      %s", a5.tel);
    }

        fprintf(file, "Name:\t%s   %s\nCity:\t%s\nCountry:   %s\nPhone:      %s",
                a1.name, a2.lastname, a3.city, a4.country, a5.tel);

        fclose(file);

        printf("\n\n\n Contact saved to ledger\n\n ..returning to main menu");
        Sleep(2500);
        system("cls");
        premenu();
}


Last edited on
How to store the old data in same file.
Open the file in append mode so that newly written data is written to the end of the file instead of it the beginning.

http://www.cplusplus.com/reference/cstdio/fopen/
"a" append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek, fsetpos, rewind) are ignored. The file is created if it does not exist.


for(int i = 0; i < 1; ++i)This is kinda silly, it only iterates once.
Last edited on
Ohh thanks you soo much. I think I need to learn more about these modes. If I'd read it entirely now I wouldn't ask such a question. Thank you again that works just fine, and yeah you're right I eliminate that "for" loop there that make non sens.
Cheers !! :)
Topic archived. No new replies allowed.