Thread: String Search .....

  1. #1

    Question String Search .....

    I'm suppose to search for a string in a file ...
    forexample ...
    The user enters the filename ... say demofile.txt
    demofile.txt contains some string like ... Leon Lewis 444-44444

    Here is my code:

    int main()
    ifstream infile;
    fstream datafile;
    char filename[81];
    char prods[8][28];
    char lookup[28],*strptr=NULL;
    int index,i=0;

    cout<<"Enter a filename: ";
    cin.getline(filename, 81);

    cout<<"Cannot Open"<<filename<<endl;

    cout<<"\nEnter string: ";

    strptr=strstr(prods[index], lookup);
    cout <<prods[index]<< endl;
    i=i+1 ;


    cout<<"\n No string found.\n";
    cout<<"\n"<< i <<"String Found"<<endl;
    return 0;, ios:ut);

    return 0;

    It opens the file but , it says no string found ...
    Any help will be really appreciated ..

  2. #2
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    As far as I can tell without going to deep into your code, you are not even trying to open the fille.

    you have to specify this to open the file:, ios:modes that you want to open the file in) );
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  3. #3


    Ok now i hve opened the file , but it prints everything in the data ... forexample this is my datafile.

    Leon Lewis, 444-4444
    Slyvester Lewis, 999-0000
    Hashim Shahid, 111-2221
    Zeeshan Siddique, 777-1818
    Amir Siddique, 333-1231
    Ron Palmer, 234-5345
    Robert King, 797-3421
    Nabil Rehman, 782-3443

    Now when the user enters "Lewis" i'm suppose to get :
    Leon Lewis, 444-4444
    Slyvester Lewis, 999-0000

    I do'nt know , when i was tryin' this program without takin' filename input it was workin' fine .....
    Need Help !!

  4. #4
    before main()
    declare struct with three stings as members-- first name, last name, and phoneNumber

    in main()
    declare array of the structs
    declare int to act as index of array and assign it value of zero
    declare ifstream and associate it with file
    read data from file into array of structs, stripping away commas
    search array of structs for given string be it a name or phone#
    if given string found in array of struct print struc information to screen adding back the comma.

  5. #5


    Can you please post some code ...
    I'm like a beginner in C++ ... i got u , wat u r sayin' ... but if u post some code , it will be really helpful...

  6. #6
    struct Person
    char firstName
    char lastName
    char phoneNumber

    int main()
    Person book
    int index = 0;
    char filename

    ifstream fin(filename)

    if !fin
    cout unable to open file

    while !fin.eof()
    fin.getline(book[index].firstname, , space)
    getiline(lastName, , comma)
    getline(phoneNumber, , newlinechar)


    int i;
    char search

    cout enter last name to search for
    cin search

    for(i = 0; i < index; i++)
    if strcmp book[i].lastname search is zero
    cout firstname space lastname comma space phone number newline

    return 0

    You can wrap it in a loop using a flag the user can change so the user can do more than one search per run of the program. You can use a flag that is changed if a match is found. After searching the entire container if the flag hasn't been changed then no match was found.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM