Bifalgorithm post · Computer science one · Plaintext Files

How can fgets() safely read a plaintext file line by line?

How can fgets() safely read a plaintext file line by line? (Computer science one · Plaintext Files) — "Using fscanf() for arbitrary string input is potentially…

Ideas in this flowchart

  • Lecture sûre des chaînes et des lignes d’un fichier en C
  • L’utilisation de fscanf() pour une chaîne arbitraire est dangereuse en raison d’un contrôle limité des limites.
  • Si l’entrée dépasse la capacité du tableau de caractères, un dépassement de tampon peut se produire.
  • fgets() constitue une méthode plus sûre, car elle limite le nombre d’octets lus.
  • La déclaration de fgets() est : char *fgets(char *s, int size, FILE *stream);
  • s désigne la chaîne de caractères dans laquelle les données lues sont placées.
  • size limite le nombre de caractères lus ; fgets() en lit au maximum size-1 pour réserver le caractère nul final.
  • stream désigne le pointeur vers le fichier depuis lequel les données sont lues.
  • fgets() ajoute automatiquement le caractère nul final à la chaîne.
  • La lecture s’arrête à la fin du fichier ou lorsqu’un caractère de nouvelle ligne « \n » est rencontré ; ce caractère peut devoir être supprimé.
  • fgets() permet de traiter un fichier ligne par ligne, puis de tokeniser chaque ligne pour extraire des données, notamment dans un fichier CSV.
  • La valeur de retour NULL indique qu’aucun caractère supplémentaire n’a été lu et que la fin du fichier est atteinte.
  • Exemple : ouvrir data.txt en lecture et utiliser line[1000], en supposant qu’aucune ligne ne dépasse 999 caractères.
Feed/

How can fgets() safely read a plaintext file line by line?

No cover

Channel

Computer science one

1 page

"Using fscanf() for arbitrary string input is potentially dangerous as there is limited bounds checking. We must store the input value from the file into a string (character array), but if the file or line contains more characters than the array can accommodate we may have a buffer overflow. A better way to read input is to use fgets() which allows us to limit the number of bytes that are read.\n\nchar *fgets(char *s, int size, FILE *stream);\n\nThe first argument is the string that the input data will be read into. The second parameter is how we limit the number of characters that will be read. It actually reads in one fewer character, size-1 to account for the null-terminating character which fgets() automatically inserts for us. The last argument is the file pointer that we wish to read from. The behavior of fgets() is that it reads up to size-1 characters from the input file and places the results into s. If fgets() encounters either an EOF symbol or an endline character, '\n\n' it stops reading. In the case of an endline character, it is included in the result and may need to be chomped out (that is, removed).\n\nThe fgets() function can be used to process a file line by line until the end of the file is reached. Each line can be processed (perhaps tokenized) individually to extract particular pieces of data. This is typically how a CSV or similar file may be processed. To determine if the end of the file has been reached, you can use the return value of the function: it returns NULL when no more characters have been read.\n\nFILE *input = fopen(\"data.txt\", \"r\");\n\n//this assumes that no line is more than 999 characters\n\nchar line[1000];"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.