Results 1 to 4 of 4

Thread: Code to search for a string

  1. #1
    Join Date
    Mar 2008
    Posts
    672

    Code to search for a string

    I contacted you because I need help. I wish I could sort all my files. Txt limits by which I cheeks ($ 10, $ 20, $ 50, $ 100, $ 200) by searching on strings specific limit through code. Can nay one give me the example of how to search the string.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Code to search for a string

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int search(char *q[], char *ne);
    
    char *ne[] = {"Jon","J","Jae","games",NULL};
    
    int main(void)
    {
      if(search(names, "James") != -1)
        printf("in list.\n");
    
      if(search(names, "Bill") == -1)
        printf("not found.\n");
    
      return 0;
    }
    
    int search(char *q[], char *name)
    {
      int t;
    
      for(t=0; p[t]; ++t){
        if(!strcmp(q[t], name)) {
           return t;
        }
      }
        return -1; /* not found */
    }

  3. #3
    Join Date
    Jan 2009
    Posts
    199

    Re: Code to search for a string

    Code:
    #include <string.h>
    main()
    {
    FILE *file;
    
            file = fopen("/home/jim/filename","f");
            if(file==NULL)
            {
            printf("error");
            }
    
    }

  4. #4
    Join Date
    Dec 2008
    Posts
    177

    Re: Code to search for a string

    Try this ;
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main () {
    
    char* search = "b"; 
    int offset; 
    string line;
    ifstream file;
    Myfile.open ("file1.txt");
    
    if(file.is_open())
    {
    while(file.eof())
    {
    getline(file,line);
    if ((offset = line.find(search, 0)) != string::npos) {
    cout << "found '" << search << endl;
    }
    }
    file.close();
    }
    else
    cout<<"file can not be open."<<endl;
    
    return 0;
    }

Similar Threads

  1. Search character in a string
    By Zool in forum Software Development
    Replies: 3
    Last Post: 14-12-2009, 02:45 PM
  2. Function search in a string
    By Amandev in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 02:19 PM
  3. Search the presence of n words in a string
    By cobra2008 in forum Software Development
    Replies: 3
    Last Post: 09-10-2009, 09:50 PM
  4. Search a string in a textbox
    By GeforceUser in forum Software Development
    Replies: 4
    Last Post: 07-05-2009, 01:18 PM
  5. How to Search a string from files in Linux
    By EnZo2 in forum Tips & Tweaks
    Replies: 0
    Last Post: 17-03-2009, 10:53 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,241,182.85946 seconds with 17 queries