a suggestion on a data structure

This is a discussion on a suggestion on a data structure within the C Programming forums, part of the General Programming Boards category; Hi, I need some suggestions on what data structure to use for the following problem: I've a flat file having ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    a suggestion on a data structure

    Hi,

    I need some suggestions on what data structure to use for the following problem:

    I've a flat file having alphabets as data like:
    1. asadhfklal
    2. qseinqwankn
    3. asekwqoepqwqerlm
    .
    .
    .
    I need to compare each line with other such that if I've:
    1. the same alphabets for a position I add 5 to the match-point
    2. differenet alphabets for a position I add 0 to the match-point
    e.g comparing 1 and 2 will have a match-point of 10 (s and a match).
    Hence for each pair of comparison I'll get a match - point.
    But I don't know how many lines it may contain and how long each alphabet string may be. This is the part that's worrying me most. Being a freshie I've no clue how to handle this.

    Appreciate your guidance.

    Thanks,
    AK

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    why isn't doing what I want it to do?
    Just add some file reading code to my reply, and you should be on your way.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thanks a lot Salem. I'll work on it. And could you please explain what void *temp denote? (please bear with these novice questions).
    Also how can I read a file having strings of unknown length and also having unknown number of strings? The only thing I know is that each string begins with an integer. How can I leverage this?

    Thanks,
    Angshu

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    some function to read a file

    Hi,

    Are there any function(s) to read a file which has strings delimited by only certain characters say #?e.g.
    #fsdgsdfgsdggfsdfgstyretyvwqoovhfoadcoamhcomkmodaj cojfadojcmofajcoamfcjmfcm
    #asdfasdfamfkansmkfnmasl
    #...
    .
    .
    .
    I mean, I want to read till the m in the 2nd line into one variable, till l in the 3rd line in another and so on...

    Thanks,
    AK

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    edit:
    I mis-read the text, you want a delimiter which isn't \n

    Basically, I would suggest you write something like fgets(), that is reading into a buffer of a specific (usually fairly large) size. Internally to that function, you call fgetc until either the buffer is full or a delimiter of your choice is found.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Now, how can I vary this buffer size? Or do I need to fix it beforehand?
    Also fgets terminates on a new line...so even if I don't get the delimiter in the nextline, it will terminate. am i right? how to overcome that?
    Or should I use only getc until I get the delimiter?

    Please advice.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > Or should I use only getc until I get the delimiter?
    Yes.

    > Or do I need to fix it beforehand?
    Make it the same as fgets, only for a delimiter of your choice rather than '\n'
    You return NULL if you get EOF before you've read any data
    You return buff if the buffer is full, or you see the delimiter.

    Make the lack of a delimiter within the space allocated for the buffer the responsibility of the caller - just like it is with fgets.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thanks Salem. So should I try something like this?
    Code:
    int main ()
    {
      FILE * f;
      char c;
      int n = 0;
      f = fopen("myfile.txt","r");
      if (f==NULL) printf ("Error opening file");
      else
      {
        do {
          c = getc (f);
          if (c == '#'){
    \\      HOW CAN I READ EACH STRING SEPARATELY HERE?
          }
    
          } while (c != EOF);
        fclose (f);
       }
      return 0;
    }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Yes, but make it a function - like this untested example
    Code:
    char *myDelimitedFgets ( char *buff, size_t size, FILE *fp, char delim ) {
      if ( feof(fp) || ferror(fp) ) {
        return NULL;
      } else {
        int i = 0, ch;
        while ( i < size-1 && (ch=fgetc(fp)) != EOF ) {
          buff[i++] = ch;
          if ( ch == delim ) break;
        }
        buff[i] = '\0';
      }
    }

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You don't have a return value for the else.
    Code:
    \\      HOW CAN I READ EACH STRING SEPARATELY HERE?
    ->
    Code:
    //      HOW CAN I READ EACH STRING SEPARATELY HERE?
    And you need an int to store EOF:
    Code:
     char c;
    
        do {
          c = getc (f);
    
          } while (c != EOF);
    ->
    Code:
     int c;
    
        do {
          c = getc (f);
    
          } while (c != EOF);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Hi,

    What is size_t?
    I tried this, but with no results in buff/data

    Code:
    #include<stdio.h>
    #include<string.h>
    
    char *get_data ( char *buff, size_t size, FILE *fp, char delim );
    
    void main(){
    
    	  FILE *f;
    	  char *data;
    	  char *buff;
    	  f=fopen("c:\test.txt","r");
    	  
    	  data = get_data(buff, 10, f, '#');
    	  printf ("%s",data);
    	  fclose(f);
    	
    
       }
    
    char *get_data( char *buff, size_t size, FILE *fp, char delim ) {
      if ( feof(fp) || ferror(fp) ) {
        return NULL;
      } else {
        int i = 0;
        char ch;
        while (i<size-1 && !(feof(fp))){
          ch=fgetc(fp) ;
          printf("%c\n",ch);
          buff[i++] = ch;
          if ( ch == delim ) break;
        }
        buff[i] = '\0';
      }
    }
    [my input file looks like]
    #A1
    ABGXCYQGTFALCOP----

    #A2
    -MNXFYQGAASAS

    #A3
    ---M-YXFYJASHKAH----

    #A4
    -----Y--Y-J-S--AH
    [/my input file looks like]

    Also, in which part are the strings separated? I mean, if I want to access the 3rd element of first string i.e. G, how do I get that?
    Please rectify wherever my concepts are wrong.


    Thanks,
    Angshu

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > What is size_t?
    The type of answer which sizeof evaluates to

    > void main()
    I'm gonna puke.....

    > char *buff;
    There's no space here, move along, nothing to see.
    Try char buff[10];

    > f=fopen("c:\test.txt","r");
    Perhaps if you'd bothered to check the return result, you might have figured out that
    a) the \t was a tab
    b) that f=fopen("c:\\test.txt","r"); would have been the answer.

    > data = get_data(buff, 10, f, '#');
    Better, but had you used an array, then it would have been
    data = get_data(buff, sizeof buff, f, '#');

    > printf ("%s",data);
    Didn't you see that the function could return NULL?
    Did you check for this possibility?

    It's like you just forgot everything you've been told

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    But the function returns null only if the fopen fails. Am i right?
    Also I tried printing out something in the 'else' part of the function. It did print. But then why doesn't it reading anything?

    Also is the way I mentioned (printf("%s"..)) a proper way to print a char *?

    Thanks,
    Ak

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    I guess that's what untested means.
    Note the lack of a return statement in the else part of my code.

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thanks a lot Salem.
    And one last question, how to access each string (each character in the string) in code you have provided?


    Gratefully,
    Angshu

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 11:42 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 05:03 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:38 PM

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