mirror of
https://github.com/QB64-Phoenix-Edition/QB64pe.git
synced 2024-09-20 04:24:48 +00:00
More error checking
This commit is contained in:
parent
dedb94223f
commit
fdfd6a2af3
1 changed files with 13 additions and 7 deletions
|
@ -33887,18 +33887,22 @@ qbs *func__readfile(qbs *filespec)
|
|||
|
||||
namez = qbs_add(filespec, func_chr(0)); // add terminator
|
||||
filepath_fix_directory(namez); // fix separators
|
||||
file = fopen((const char*)namez -> chr, "rb");
|
||||
file = fopen((const char*)namez->chr, "rb");
|
||||
|
||||
if (file) {
|
||||
fseek(file, 0, SEEK_END); // end pos
|
||||
len = ftell(file); // = file length
|
||||
rewind(file); // rewind to start
|
||||
cont = qbs_new(len, 1); // get new string for file contents
|
||||
fread(cont -> chr, 1, len, file);
|
||||
fread(cont->chr, 1, len, file);
|
||||
if (ferror(file)) {
|
||||
error(QB_ERROR_PATH_FILE_ACCESS_ERROR); // something went wrong
|
||||
cont = qbs_new_txt(""); // return empty on error
|
||||
}
|
||||
fclose(file);
|
||||
return cont;
|
||||
} else {
|
||||
error(QB_ERROR_FILE_NOT_FOUND); // error, we throw most common
|
||||
error(QB_ERROR_FILE_NOT_FOUND); // most common when trying to read
|
||||
return qbs_new_txt(""); // return empty on error
|
||||
}
|
||||
}
|
||||
|
@ -33910,13 +33914,15 @@ void sub__writefile(qbs *filespec, qbs *contents)
|
|||
|
||||
namez = qbs_add(filespec, func_chr(0)); // add terminator
|
||||
filepath_fix_directory(namez); // fix separators
|
||||
file = fopen((const char*)namez -> chr, "wb");
|
||||
file = fopen((const char*)namez->chr, "wb");
|
||||
|
||||
if (file) {
|
||||
fwrite(contents -> chr, 1, contents -> len, file);
|
||||
fwrite(contents->chr, 1, contents->len, file);
|
||||
if (ferror(file))
|
||||
error(QB_ERROR_PATH_FILE_ACCESS_ERROR); // something went wrong
|
||||
fclose(file);
|
||||
} else {
|
||||
error(QB_ERROR_PATH_FILE_ACCESS_ERROR); // error, we throw best fit
|
||||
error(QB_ERROR_PATH_NOT_FOUND); // most common when making a new file
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue