Concept
How can you read a text file one line at a time?
ComputerScienceOne / Binary Files
"22. File I/O\n\n4 //read the first line\n\n5 char *s = fgets(line, 1000, input);\n\n6 while(s != NULL) {\n\n7\n\n8 //chomp the endline character from line:\n\n9 line[strlen(line)-1] = '\\0';\n\n10\n\n11 //process the current buffer\n\n12 //for demonstration, we simply print it:\n\n13 printf(\"line = %s\n\n\", line);\n\n14\n\n15 //read the next line\n\n16 s = fgets(line, 1000, input);\n\n17 }\n\nA similar function, int fgetc(FILE *stream) allows us to get a single character from the input file (returned as an int ) if we prefer to read character by character."
Related Ideas
- How do fprintf() and fscanf() read from and write to plaintext files?ComputerScienceOne · Plaintext Files
- What is 22?ComputerScienceOne · Opening Files
- How are files read, written, and properly managed?ComputerScienceOne · Paths
- How do you write and read binary data in C?ComputerScienceOne · Closing Files
- When should data be stored as binary or text files?ComputerScienceOne · Buffered and Unbuffered
- How do buffered and unbuffered file input/output differ?ComputerScienceOne · Buffered and Unbuffered
- How can file input/output programs process text, binary data, and word searches?ComputerScienceOne · Exercises
- How should you close files in C?ComputerScienceOne · Closing Files