?
Solved

How do i construct the getline method in C

Posted on 2004-09-02
2
Medium Priority
?
339 Views
Last Modified: 2010-04-15
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);

/*---------------------main----------------------------
 * 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;
}

0
Comment
Question by:pkeketso
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11961939
Hi pkeketso,

fgets(buffer, BUFFER_LEN, fp );

Sunnycoder
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 300 total points
ID: 11962006
pkeketso,

>       while(getline(fp, line, MAXLINE) > 0)
while ( fget (line, MAXLINE,fp) != NULL )

Sunnycoder
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question