String Search .....

This is a discussion on String Search ..... within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm suppose to search for a string in a file ... forexample ... The user enters the filename ... ...

  1. #1
    cooleeze
    Guest

    Question String Search .....

    Hi,
    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);

    if(!infile)
    {
    cout<<"Cannot Open"<<filename<<endl;
    }

    cout<<"\nEnter string: ";
    cin.getline(lookup,28);

    for(index=0;index<8;index++)
    {
    strptr=strstr(prods[index], lookup);
    if(strptr!=NULL)
    {
    cout <<prods[index]<< endl;
    i=i+1 ;
    }

    continue;
    }

    if(i==NULL)
    cout<<"\n No string found.\n";
    else
    cout<<"\n"<< i <<"String Found"<<endl;
    return 0;


    datafile.open(filename, 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
    Posts
    125
    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:

    infile.open(filename, 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
    cooleeze
    Guest

    Unhappy

    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 !!
    Plz,
    Thanxs.

  4. #4
    Unregistered
    Guest
    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
    cooleeze
    Guest

    Unhappy

    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...
    Thanxs.

  6. #6
    Unregistered
    Guest
    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)
    index++

    index--

    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, 02:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21