Solved

How do i construct the getline method in C

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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 how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now