create file if it doesn't exist when opened

This commit is contained in:
emekoi committed 2019-10-16 12:50:32 -05:00
1 parent 30f7a29491
commit 993f24596a
1 file changed
+9 -1
+9 -1
View File
@@ -1072,13 +1072,21 @@ char* editorRowsToString(int* buf_len) {
return buf;
}
static int fileExists(const char* file_name) {
struct stat s = {0};
return stat(file_name, &s) == 0;
}
void editorOpen(char* file_name) {
free(ec.file_name);
ec.file_name = strdup(file_name);
editorSelectSyntaxHighlight();
FILE* file = fopen(file_name, "r+");
// If the file dosen't exist, create it, otherwise just open it
const char *mode = fileExists(file_name) ? "r+" : "w+";
FILE* file = fopen(file_name, mode);
if (!file)
die("Failed to open the file");