Solved

How do i construct the getline method in C

Posted on 2004-09-02
2
337 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 100 total points
ID: 11962006
pkeketso,

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

Sunnycoder
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

696 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