-2

I am absolutely a beginner at programming. No this is not a homework either. I am trying to learn it by myself. As stated in the titel itself I would like to input a .txt file and find out a particular word from the file. But thats not exactly what I want. I rather want the line number in which the word lies, so that i can use this line number and print out all the lines in the .txt file after this particular line. I found this code on youtube where it showed me how to find the word. I modified it a bit and its now giving me the line in which the searched word states (NOT THE LINE NUMBER). I am attaching the code.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;

string find_word(string file, string word)
{
  int offset;
  string line1;
  ifstream Myfile;
  Myfile.open(open);

  if (Myfile.is_open())
  {
    while (!Myfile.eof())
    {
      getline(Myfile, line1);
      if ((offset = line1.find(word,o)) != string::npos)
      {
        return line1;
      }
    }
    Myfile.close();
  }
  else
    cout << "couldn't open...." << endl;
}

int main ()
{
  string c = find_word("test.txt", "$COOR");
  cout << c;
  cin.get();
  return 0;
}

right now the text file contains just 8 lines and "$COOR" lies in line 4. the program just gives me the entire line. But I want the line number so that I can print out the lines after line number 4.

I would later like to test it for a file having many lines i.e. more than 50000000 or so.

dandan78
  • 13,328
  • 13
  • 64
  • 78
subhajit
  • 11
  • 2
  • If your code really looks like that you should spend some time indenting it properly so it's easier to read. If it doesn't really look like that please edit the question to indent it properly (you can select a block of code and press Ctrl-K or the `{}` button to indent a block). – Jonathan Wakely Oct 13 '15 at 11:05
  • Also see http://kayari.org/cxx/antipatterns.html#istream-check and http://kayari.org/cxx/antipatterns.html#istream-eof -- you should be checking the result of `getline` not testing `eof` – Jonathan Wakely Oct 13 '15 at 11:06
  • Please indent your code. Using `while ( !file.eof() )` is a bad idea, please see why here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – bku_drytt Oct 13 '15 at 11:11

1 Answers1

4

Think logically. You don't know whether a given line contains the word you're looking for until you read it. Therefore, you always need to know the number of the current line in the text file that you've read, so if the line contains the word, you then print the line number.

You need another variable int, initialized to zero, and incremented every time your code reads a line of text. So, when your code reads the first line of text, it gets incremented to 1. Then, when the code reads the next line of text, the new variable gets incremented to 2, and so on. So, when you find the word, you know where to look to find the line number.

Your code already has a loop for reading each line of text. Don't you think it's now obvious where you will need to increment the line counter?

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148