What are some best methods for file input / output in C?

I am writing a pretty basic program for personal use, but I really want to make sure that I use good practices, especially if I decide to make it more reliable later or something like that.

In all senses and purposes, the program takes some input files as arguments, opens them with the help fopen(), reads from files, makes material with this information, and then saves the output as several different files in a subfolder. for example, if the program is in ~/program, then the output files are saved in~/program/csv/

I am just outputting directly to files, for example output = fopen("./csv/output.csv", "w");, printing it with a fprintf(output,"%f,%f", data1, data2);loop, and then closing with fclose(output);, and I just feel that this is bad practice.

Do I have to save it in the temp directory and it is written and then moved when it ends? Should I use more complex file I / O libraries? Did I just completely rethink this?

+5
source share
5 answers

Best practices in my eyes:

  • Check every call to fopen, printf, puts, fprintf, fclose, etc. for mistakes.
  • use getchar if you need, fread if you can
  • use putchar if you should, fwrite if you can
  • avoid arbitrary restrictions on the length of the input line (malloc / realloc may be required)
  • know when you need to open output files in binary mode
  • use Standard C, forget conio.h :-)
  • , - , .. printf ("hello, world\n");, "\nHello, world", , . .
  • 7 ASCII, Unicode ( - UTF-8, ASCII). , - . ISO-8859 - *.
+7

?

. , , " ". , , .

+4

. I/O stdio file, fprintf. , , fclose.

fopen, .. , , , .

0

~/program/csv/output.csv , , , .

FILE *, tmpfile stdio.h , . output.csv.lck , , , .

0

You can create your own program cat, cp, mvto practice.

0
source