In: Computer Science
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED
Write a C program using system call I/O to determine how many lines there are in a text file.
Answer:
Code:
#include <stdio.h>
#define MAX_FILE_NAME 100
int main()
{
FILE *f;
int count = 0;
char filename[Your_file_name];
char ch;
// Get file name from user. The file should be
// either in current folder or complete path should be provided
printf("Enter file name: ");
scanf("%s", filename);
// Open the file
f = fopen(filename, "r");
// Check if file exists
if (f == NULL)
{
printf("Could not open file %s", filename);
return 0;
}
// Extract characters from file and store in character c
for (ch = getc(f); ch != EOF; ch = getc(f))
if (ch == '\n') // Increment count if this character is newline
count = count + 1;
// Close the file
fclose(f);
printf("The file %s has %d lines\n ", filename, count);
return 0;
}
Hope I answered the question.
If you have any doubts, or queries, feel free to ask
I'll respond to you as soon as I can.
Have a nice day