Solved

How do i construct the getline method in C

Posted on 2004-09-02
2
333 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Estimate porting issues. 9 119
C simple state machine for client server handshake 4 301
How to programmatically differentiate between C and Java 10 198
Grammars for C C++ and java 1 122
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 and use pointers 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.

803 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