Change content/line of text in a File

Hello, I want to change a line of text/content in a text file. There are many options, like copy the entire content of the file to a temporary file, seek the content you want and then delete it, put another content in it, close the temporary file and copy everything back. But is there not a easier way to do this?

Here is the entire 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <windows.h>


    FILE *ofp,*ifp;
    char bnaam[255];


typedef struct {
        char fname['\n'];
        char insertion['\n'];
        char surname['\n'];
        } Name;

typedef struct {
        int number;
        char Character['\n'];
        } Zipcode;

typedef struct  {
        char streetname['\n'];
        int housenum;
        Zipcode zipcode;
        char residence[50];
        } Address;

typedef struct {
        Name name;
        Address address;
        } PD;
        
/* Function for reading the Personal data, scan it and then put it in a file*/

 PD read_PD(void)
{
        PD pd;
      
       printf("Give firstname\n");
       scanf("%s",&PD.name.firstname);
       fprintf(ofp,"%s\n",PD.name.firstname);
       
       printf("Give insertion\n");
       scanf("%s",&PD.name.insertion);
       fprintf(ofp,"%s\n",PD.name.insertion);
       
       printf("Give surname\n");
       scanf("%s",&PD.name.surname);
       fprintf(ofp,"%s\n",PD.name.surname);
       
       printf("Give streetname\n");
       scanf("%s",&PD.address.streetname);
       fprintf(ofp,"%s\n",PD.address.streetname);
       
       printf("Give housenumber\n");
       scanf("%d",&PD.address.housenum);
       fprintf(ofp,"%d\n",PD.address.housenum);
 
       printf("Give zipcode\n");
       scanf("%d%s",&PD.address.zipcode.number,&PD.address.zipcode.character);
       fprintf(ofp,"%d%s\n",PD.address.zipcode.number,&PD.address.zipcode.character);
       
       printf("Give residence\n");
       scanf("%s",&PD.address.residence);
       fprintf(ofp,"%s\n",PD.address.residence);
       
 return pd;
}

/* Function append*/


int append(void)
{
 PD record;
 
if((ofp=fopen(bnaam,"a+"))==NULL)
{
                                 printf("File %s can't be openend for appending",bnaam);
                                 return 0;
                                 }
                                 
record=read_PD();
fclose(ofp);
printf("\nRecord appended\n");
Sleep(1000);
printf("\n");
}

/* END of function append*/

/* Begin function change*/

int change(void)
{
    PD pd;
    int x;
    
    if((ofp=fopen(bnaam,"r+"))==NULL)
{
                                 printf("File %s can't be openend for changing",bnaam);
                                 return 0;
                                 }
                                 
               printf("What do u want to change?\n");
               printf("[1] first name\n");
               printf("[2] insertion\n");
               printf("[3] surname?\n");
               printf("[4] Streetname\n");
               printf("[5] Housenumber\n");
               printf("[6] Zipcode\n");
               printf("[7] Residence\n");
               printf("enter:\t");
               scanf("%d",&x);
               
    switch(x)
    {
             case 1: 
               printf("Give firstname\n");
               scanf("%s",&PD.name.firstname);
               fprintf(ofp,"%s\n",PD.name.firstname);
                  break; 
             case 2: 
              printf("Give insertion\n");
              scanf("%s",&PD.name.insertion);
              fprintf(ofp,"%s\n",PD.name.insertion);
              break;
       
             case 3: 
       printf("Give surname\n");
       scanf("%s",&PD.name.surname);
       fprintf(ofp,"%s\n",PD.name.surname);
                  break; 
             case 4: 
       printf("Give streetname\n");
       scanf("%s",&PD.address.streetname);
       fprintf(ofp,"%s\n",PD.address.streetname);
                  break; 
             case 5: 
      printf("Give housenumber\n");
       scanf("%d",&PD.address.housenum);
       fprintf(ofp,"%d\n",PD.address.housenum);
                  break; 
             case 6: 
       printf("Give zipcode\n");
       scanf("%d%s",&PD.address.zipcode.number,&PD.address.zipcode.character);     fprintf(ofp,"%d%s\n",PD.address.zipcode.number,&PD.address.zipcode.character);
                  break; 
             case 7: 
       printf("Give residence\n");
       scanf("%s",&PD.address.residence);
       fprintf(ofp,"%s\n",PD.address.residence);
                  break; 
             
   }
                  fclose(ofp);
                  printf("\nRecord changed!\n");
                  Sleep(1000);
                  printf("\n");
                  system("cls");
}

int main()
{
    int y;
    int a,b,c,d,e,f;
    char ch;
    
    ifp = fopen("myfile.txt","r");
    ifp == ofp;

    printf("Do you want to use a default file(A) or create one(B)(A/B)?");
    printf("Do you want to close the program (E)?\t");
    printf("\nEnter:\t");
    y=getchar();
    y=toupper(y);
    fflush(stdin);
    system("cls");
    
  if(y=='A'||y=='a'){
                       printf("Enter the file name:\t"); 
                       gets(bnaam);
                       
                       ofp = fopen (bnaam, "r");
                       if(ofp == NULL)
                                       {
                                            printf("Wrong file name\n");
                                            printf("The program is now closing");
                                            Sleep(4000);
                                            return 0;
                                     }
   else{
                  printf("The default file has been openend\n");
                  
       }
       fclose(ofp);
                          } 
     
    
   if(y=='b'||y=='B') {
         printf("Enter the file name you want to create:\t"); 
         scanf("%s",bnaam);
         ofp=fopen(bnaam,"r");
         }
         
    if(y=='E'){
               return 0;
               }
               
    fclose(ofp);           
    
    fflush(stdin);
  
    do {
        
    printf("Welcome at the menu\n\n");
    printf("What do you want to do:\n");
    printf("\n1.\tAppend records?(1)\n");
    printf("2.\tChange Records?(2)\n");
    printf("3.\tShow records?(3)\n");
    printf("4.\tMake a new file?(4)\n");
    printf("5.\tClose program?(5)\n");
    printf("Fill in:\t");
   
   while((y=getchar()) !='\n'){
   
   if(y=='1'){
                  fflush(stdin);
                  system("cls");
                  append();
                  system("cls");
               }
               
   if(y=='2'){
               fflush(stdin);
               system("cls");
               erase();
               }
               
               
   if(y=='3'){
   
              printf("Give the file name that has to be openend\n");
              scanf("%s",bnaam);
 system("cls");
 
 ofp = fopen (bnaam, "r"); 
 
 if (ofp == NULL)
 {
  printf("Can't open the file\n");
  return 0;
 }
 
 ch = fgetc(ofp);
 
 while (ch != EOF)
 {
  printf ("%c", ch);
  ch = fgetc(ofp);
 }
printf("\n\n");
               }
 fclose(ofp);
 
   if(y=='4'){
         printf("Give the name of the file:\t"); 
         scanf("%s",bnaam);
         ofp=fopen(bnaam,"w+");
         fflush(stdin);
         system("cls");
         printf("The new file has been created\n");
         printf("Press enter:");
               }
   if(y=='5'){
               return 0;
               }
   
                                   }
                } 
                while(y !='E');                   
                
    getchar();
    getchar();
    
    fclose(ofp);
    fclose(ifp);
}


NOTE: I am dutch so if there are words in the code that you don't understand, then it's probarly dutch and I didn't change it to English.
Also the text formatting of the code is completely gone, but that's not my fault.
Thanks
1
2
3
4
5
typedef struct {
        char fname['\n'];
        char insertion['\n'];
        char surname['\n'];
        } Name;


I'm curious what you think this is doing.

Hello, I want to change a line of text/content in a text file. There are many options, like copy the entire content of the file to a temporary file, seek the content you want and then delete it, put another content in it, close the temporary file and copy everything back. But is there not a easier way to do this?


The method you describe is not workable. You have the same problem updating the temporary file as you do with the original file. The usual method to make changes is to read in all of the file data, make any changes to the data and write all of the data back to the file.

This can be accomplished in several ways, the simplest (and least safe is to:)

* Open the data file
* Read the data in
* Close the data file
* Make changes to data
* Reopen the file for output, truncating the file
* Write all of the data back to the file
* Close the file

This can be avoided if the data to be replaced is exactly the same length as the data it is being replaced with. There are ways around this, of course, but they're likely to be overkill for your use case -- you'd be much better off using a database of some sort rather than reinventing one.
Topic archived. No new replies allowed.