Solved

How do i construct the getline method in C

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

730 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