#include #define MAX_CHARS_PER_LINE 100 int main () { int lineNumber; // We'll track line numbers. char inputLine [MAX_CHARS_PER_LINE + 1]; // Need an extra char for string-terminator. char ch; // Variable into which we'll read each char. int cursorPosition; // Position in inputLine for current char. FILE *dataFile; // The file. // Open the file. dataFile = fopen ("testfile", "r"); // Initial value - first line will be "1". lineNumber = 0; // Initial position of cursor within inputLine array: cursorPosition = 0; // Get first character. ch = fgetc (dataFile); // Keep reading chars until EOF char is read. while (ch != EOF) { // If we haven't seen the end of a line, put char into current inputLine. if (ch != '\n') { // Of course, we have exceeded the space allotted. if (cursorPosition >= MAX_CHARS_PER_LINE) { // Can't append. printf ("Input line size exceeded - exiting program\n"); exit (0); } // OK, there's space, so append to inputLine. inputLine[cursorPosition] = ch; cursorPosition ++; } else { // Need to place a string-terminator because that's not in the file. inputLine[cursorPosition] = '\0'; // Print. lineNumber ++; printf ("Line %4d: %s\n", lineNumber, inputLine); // Reset cursor. cursorPosition = 0; } // Get next char. ch = fgetc (dataFile); } // end while // Done. fclose (dataFile); }