Interview Questions on C C++

Home  |  About Us  |  Contact
View Questions Contribute Training Misc..

C program to read text file (C Program to read a text file line by line)

You are here : Home » Program to read a text file line by line

/*
Program to read a text file line by line
Author: CppBuzz
Date: 17th Nov 2017
*/

#include <stdio.h>

int main(int argc, char* argv[])
{
//pass the filename as argument
char const* const fileName = argv[1]; 
FILE* file = fopen(fileName, "r"); 
char line[500];

while (fgets(line, sizeof(line), file)) {
printf("%s", line); 
}
fclose(file);
return 0;
}