Results 1 to 9 of 9

Thread: Getting a value next to a number

  1. #1

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Getting a value next to a number

    How do I do it?

    I have a text file in the form of:

    18930 da_silvy
    20389 karl_moore
    20138 john

    (for example)

    how could i get it to return da_silvy when 18930 was the value?

  2. #2
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    You do the same thing that if you were in VB...

    Search each line, but it in a String then check for the space:
    Here is the idea :

    [NUMBER][Space][Name][NewLine]

  3. #3

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    So how would i do that?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Have you learned file handling?
    Code:
    // C
    #include <stdio.h>
    
    void getName(int index, char *name)
    {
      FILE *f = fopen("thefile.dat", "rt");
      while(!feof(f))
      {
        int i;
        char buf[100];
        fscanf("%i %s", &i, buf);
        if(i == index)
        {
          strcpy(name, buf);
          fclose(f);
          return;
        }
      }
      name[0] = '\0';
      fclose(f);
    }
    
    // C++
    #include <fstream>
    #include <string>
    using namespace std;
    
    void getName(int index, string &name)
    {
      ifstream f("thefile.dat", ios::in | ios::text);
      while(!f.eof())
      {
        int i;
        string str;
        f >> i >> str;
        if(i == index)
        {
          name = str;
          return;
        }
      }
      name.clear();
    }
    It's really simple.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Oh, I can't test that, so I can't guarantee that there are no errors - that's up to you.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    If it's a few thousand records try reading the file into memory
    use qsort to sort the array()
    use bsearch() to lookup
    This would be for a lookup intensive kind of application, like PLU for a scancode.
    Otherwise for ten-100 records what CB gave you is fine.
    Or for a one-time lookup.

    In a lookup intensive app you don't keep hitting the file, you hit it once and get it over with.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    static int last=(-1);
    void test(FILE *);
    int cmp(void * void *);
    int cmp1(void *, void *);
    int find(const char *);
    static char **list, tmp[256];
    FILE *in;
    int main(int argc, char *argv[]){
         
         int i;
         test(in=fopen("myfile.txt"));
         while (!feof(in)){       // put text into array
             memset(tmp,0x00,sizeof(tmp);
             if(fgets(tmp,255,in)!=NULL){
                  list[++last]=calloc(sizeof(tmp),1);
                  strcpy(list[last],tmp);
             }           
         qsort(list,(size_t) last,sizeof(tmp),cmp);  // sort the array
         fclose(in);
         if( find("18960")!= NULL )                 // bsearch lookup
                   printf("%s\n",list[find(]);
         for(i=0;i<=last;i++) free(list[i]);             
         return 0;
    }
    
    void test(FILE *tst){
        if(tst==NULL){
                 perror("Error opening inpout file");
                 exit(EXIT_FAILURE);
        }
    	return;
    }
    
    int cmp(void *s, void *t){
           return( strcmp( (char*)s,(char*)t);
    }
    
    char* find(const char *s){
         return (char *) bsearch(s,list,sizeof(tmp),last,cmp1);
    }
    int cmp1(void *s,void *t){
         return strncmp( ((char*) s, (char*)s, 6);
    }

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yep right.
    In C++ you might want to read the whole file into a map<int, string> container, with the numbers as keys and the names as values. Will REALLY speed up the lookup.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Originally posted by CornedBee
    Have you learned file handling?
    Code:
    // C
    #include <stdio.h>
    
    void getName(int index, char *name)
    {
      FILE *f = fopen("thefile.dat", "rt");
      while(!feof(f))
      {
        int i;
        char buf[100];
        fscanf("%i %s", &i, buf);
        if(i == index)
        {
          strcpy(name, buf);
          fclose(f);
          return;
        }
      }
      name[0] = '\0';
      fclose(f);
    }
    
    // C++
    #include <fstream>
    #include <string>
    using namespace std;
    
    void getName(int index, string &name)
    {
      ifstream f("thefile.dat", ios::in | ios::text);
      while(!f.eof())
      {
        int i;
        string str;
        f >> i >> str;
        if(i == index)
        {
          name = str;
          return;
        }
      }
      name.clear();
    }
    It's really simple.
    thanks for making me feel stupid

    i haven't learned C++ before and wrote some different code that didn't seem to work

    thanks for your help guys

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Maybe if I got nothing better to do I'll code my second idea...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width