#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    California
    Posts
    1
    Rep Power
    0

    Finding text in a file


    I'm trying to find all the assignment statements in a file that's being read in.
    I'm not quite sure what the best method is. The code below will
    allow me to retrieve each character in the file but what if I just want the line where an assignment statement occurs?
    For example:
    int x;
    MilesPerGallon = Miles/Gallon;
    ************************************************

    void main ()
    {
    char input[1000];

    ifstream inFile;
    inFile.open("myProgram.cpp");

    while (!inFile.eof())
    {
    inFile.getline(input, 1000);
    cout << input;
    }
    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    You will have to write code that looks into your data retrieved from the file for your pattern. In the C world (baring third party libraries) you are pretty much left with strcmp() or rolling your own. If you want wild cards, you will have to do it yourself. In C++ you can use the "string" class (or some other equivelent), and can probably find some methods that will do the work for you, but I recommend getting down and dirty and learning how things actually work by writing your own comparison routines. That way if you want to make a change it is easy.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    Read from the file once before entering your while loop. C++ won't return eof until after you try to read past the end of the file, not right when you reach it, so you want your first read to be before your first test. Inside the loop, the read should be the last thing and not the first.

    Look into using the strstr() function on each line. somethinge like...

    Code:
    void main () 
    { 
    char input[1000]; 
    char *EqualSign=NULL;
    
    ifstream inFile; 
    inFile.open("myProgram.cpp"); 
    
    inFile.getline(input, 1000); 
    while (!inFile.eof()) 
    {
      EqualSign=strstr(input, "=");
      // if it found an = and it's not the first half of a comparison
      if (EqualSign != NULL && EqualSign[1] != '=')
      {
        // probabaly an assignment, but could still be in a literal
        // or part of <= or >=
        // you'll have to figure how to check for those on your own
      }
      inFile.getline(input, 1000); 
      EqualSign=NULL;
    } 
    }
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn

IMN logo majestic logo threadwatch logo seochat tools logo