pkeketso
asked on
How do i construct the getline method in C
I've just constructed a program which reads number of lines in a file.The thing is i've got a problem constructing the getline method.I've already constructed part of the program.When i compile the program, it gives me the name of the file.
I would be very glad if you could help me. The program is as follows :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE 1000
/*------------------------ --Clear Method-------------------- ---------- --
* This methos clears the screen
*/
void clear()
{
printf(" ");
}
int getline( FILE *fp, char *line, int max);
/*---------------------mai n--------- ---------- ---------
* this is the main part of the program which calls the
* getline method to count number of lines in a file
*/222
main(int argc, char *argv[])
{
char *filename = "input.dat";
FILE *fp;
char line[MAXLINE];
int found = 0;
fp = fopen(filename, "r");
if(fp == NULL)
{
fprintf(stderr, "can't open %s\n", filename);
exit(EXIT_FAILURE);
}
if (argc != 2)
printf("Usage: find pattern\n");
else
while(getline(fp, line, MAXLINE) > 0)
if (strstr(line, argv[1]) != NULL)
{
printf("%s", line);
found++;
}
return found;
}
I would be very glad if you could help me. The program is as follows :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE 1000
/*------------------------
* This methos clears the screen
*/
void clear()
{
printf(" ");
}
int getline( FILE *fp, char *line, int max);
/*---------------------mai
* this is the main part of the program which calls the
* getline method to count number of lines in a file
*/222
main(int argc, char *argv[])
{
char *filename = "input.dat";
FILE *fp;
char line[MAXLINE];
int found = 0;
fp = fopen(filename, "r");
if(fp == NULL)
{
fprintf(stderr, "can't open %s\n", filename);
exit(EXIT_FAILURE);
}
if (argc != 2)
printf("Usage: find pattern\n");
else
while(getline(fp, line, MAXLINE) > 0)
if (strstr(line, argv[1]) != NULL)
{
printf("%s", line);
found++;
}
return found;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
fgets(buffer, BUFFER_LEN, fp );
Sunnycoder