-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_image.c
62 lines (51 loc) · 1.58 KB
/
add_image.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILENAME_LENGTH 100
#define MAX_ALT_LENGTH 100
#define MAX_IMAGES 100
// Structure to represent an image
typedef struct {
char filename[MAX_FILENAME_LENGTH];
char alt[MAX_ALT_LENGTH];
} Image;
int main() {
Image images[MAX_IMAGES];
int numImages = 0;
FILE *file;
// Read existing image data from imgs.json
file = fopen("imgs.json", "r");
if (file) {
char line[256];
while (fgets(line, sizeof(line), file)) {
sscanf(line, "{\"filename\": \"%[^\"]\", \"alt\": \"%[^\"]\"}",
images[numImages].filename, images[numImages].alt);
numImages++;
}
fclose(file);
}
// Add a new image
char newFilename[MAX_FILENAME_LENGTH];
char newAlt[MAX_ALT_LENGTH];
printf("Enter the filename of the new image: ");
scanf("%s", newFilename);
printf("Enter the alt text for the new image: ");
scanf("%s", newAlt);
strncpy(images[numImages].filename, newFilename, MAX_FILENAME_LENGTH);
strncpy(images[numImages].alt, newAlt, MAX_ALT_LENGTH);
numImages++;
// Write updated image data to imgs.json
file = fopen("imgs.json", "w");
if (file) {
for (int i = 0; i < numImages; i++) {
fprintf(file, "{\"filename\": \"%s\", \"alt\": \"%s\"}\n",
images[i].filename, images[i].alt);
}
fclose(file);
} else {
printf("Error: Could not open file for writing.\n");
return 1;
}
printf("Image added successfully!\n");
return 0;
}