Thread: New to board - Question concerning Search and Print Line

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    2

    New to board - Question concerning Search and Print Line

    Hey guys! First off fairly new to coding, spent some time in Java, now Im learning C and C+. Since I still consider myself a beginner, I obviously do not want people to 'solve' or 'do' anything strictly for me, I like to understand. Anyways, the basic premise of my problem is I have to search for a word, in a text file through command line, with a C program. That program then will see if a line contains the word, and print the line number. I can do all of that said in the problem, though Im having problems with printing the line number. Here is the code I have so far: can anyone give me any hints, or point me to material to look at to accomplish what I want to do? So if it finds the word Im searching for, I want it to print out the Line number, for the corresponding line.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 500
    
    
    char line[MAX];
    char filename[20];
    FILE *fp;
    int main()
    {
        // prompt for file name
        printf("Enter file name:   ");
        scanf("%s", filename);
    
    
        fp=fopen(filename, "r");
        
        // check through lines to determine if lines contain 'search' word
        while (fgets(line, sizeof(line), fp))
        {
            if (strstr(line, "hello") != NULL)
            {
                puts(__LINE__);
            }
        // Checks viability of file name input
        }
        fclose(fp);
        return 0;
    }

    So basically, if Im searching for the word 'ferrai', and in a text document you have

    hey ferrari

    long time no see

    do you have a ferrari and another ferrari?

    where is your ferrari

    So then if the word is found in the line, it would print the line number, so:

    1
    5
    7

    or to that extent.
    Last edited by kronose; 02-01-2014 at 03:50 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    __LINE__ returns the line of your source code file, not the input file.

    If you want to know the line number of the input file, you'll have to count it yourself.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    2
    Quote Originally Posted by tabstop View Post
    __LINE__ returns the line of your source code file, not the input file.

    If you want to know the line number of the input file, you'll have to count it yourself.

    Ok, that makes some sense. So how do would I start going about correlating a count increase, 1,2,3,4,5, with the search for the word? So that when the word appears, it prints the number its counting up 1, for each line? As in 'Oh I see the word ferrari is there, so this line contains the word, so I will print a number for this line'

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Generally you start counting at 0, and then when you read a line the count goes up by one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-12-2013, 09:58 PM
  2. how to open files and print line by line in shell
    By omega666 in forum Linux Programming
    Replies: 4
    Last Post: 04-15-2011, 04:54 PM
  3. Print Certain Line in C
    By mancode1009 in forum C Programming
    Replies: 7
    Last Post: 08-27-2008, 07:47 PM
  4. print line by line from a file
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 02-18-2005, 01:36 PM