COMP 206 Lecture Notes - Lecture 17: Data File, Carriage Return, Segmentation Fault
Document Summary
We were talking about basic input and output operations: getchar() and fgetc(filehandle) return one character at a time as an int. So to use the output in a character string, you must be comfortable with converting between integers and character types in c. Use a character variable and just say char c = [value] . Be careful about type conversions and it should work fine. The reason why they don"t return char (which would be sensible because they"re supposed to be reading text from files) is because that isn"t their only goal. They can also read binary data, and it doesn"t necessarily make sense to use a char cata type because we aren"t reading string-type data. Int is the most flexible , and importantly, there"s at least one special return code, which is end-of-file , to tell you when to stop reading. End-of-file is returned by input functions when they can no longer give useful input: