-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateFile.c
More file actions
46 lines (37 loc) · 1.14 KB
/
Copy pathcreateFile.c
File metadata and controls
46 lines (37 loc) · 1.14 KB
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
#include <stdio.h>
#include <stdlib.h>
// Function to create a file with the given filename
void createFile() {
FILE *file;
char filename[100];
// Ask the user to input the name of the file
printf("Enter the name of the file to create: ");
scanf("%s", filename); // Read the filename
// Open the file in write mode ("w"). If the file doesn't exist, it will be created.
file = fopen(filename, "w");
// Check if the file was successfully opened
if (file == NULL) {
// If file can't be created/opened, print an error and exit
printf("Error creating the file!\n");
return;
}
// Close the file immediately as no content is written
fclose(file);
printf("File '%s' created successfully with no content.\n", filename);
}
int main() {
int choice;
while (1) {
createFile();
printf("\nActions:\n");
printf("1) Continue 2) Exit\n");
printf("Choice: ");
scanf("%d", &choice);
if (choice == 1) {
continue;
} else {
break;
}
}
return 0;
}