Thread: Searching a .txt file?

  1. #1
    Registered User
    Join Date
    May 2004

    Question Searching a .txt file?

    Hi, I was wondering how I could search a .txt file for a certain string. What I mean is this, if I have a string:

    string somestring="three";
    and I I have a .txt file that contains the following:

    the program would search the file and compare each line. It would be like this: if somestring equals first line, say "somestring is in the .txt file!", else go to the next line. If somestring equals the second line....etc. Does anyone know how to do this? In other words, I want to know if a string is in a .txt file or not. Thanks in advance!

  2. #2
    Registered User
    Join Date
    Apr 2002
    You said it in your own post. Open up the file, start reading each line one at a time. Then compare the read line with your desired string. If it is the same, output some message or whatever. Otherwise keep reading. You can find some good File IO tutorials on this web site, just look around. Now if you have a specific implementation detail I'd be glad to help.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    You can also read the data into a buffer and then search it with string-handling functions (strstr, et al).
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM