how to compare string value in C (strcmp)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #16
    Originally posted by need some help
    [code=c]#include<stdio. h>
    main()
    {

    char buffer[100];
    // char myrec[100];
    FILE *Nf;
    char *c;
    char *myrec[10];
    int i = 0, j;

    Nf = fopen("wthr.dat a", "r");

    do {
    c = fgets(buffer, 100, Nf);
    myrec[i] = buffer;
    printf("myrecor d is : %s \n", myrec[i]);
    i = i + 1;

    if (c != NULL)
    printf("%s", buffer);
    } while (c != NULL);

    for (j = 0; j < 10; j++) {
    printf("records are : %s \n", myrec[j]);
    }

    fclose(Nf);

    }[/code]

    now its printing with first 2 printfs but why its not printing at printf in for loop?
    Ignore Savage’s last post. (Savage, you should be ashamed using breaks. Tisk-tisk ;))

    Your code looks fairly good but it does have errors in it.
    1. You read in a line up to a max of 100 chars into a buffer (fine).
    2. You then assign myrec[i] to the buffer (not good), if you want a record to by copied either create a 2d array and use strncpy() or assign myrec[i] to a newly allocated memory (length is strlen() + 1, where the extra 1 is for the terminating ‘\0’ (NULL) char) and then use strcpy().
    3. You then print out the record (fine) and increment the index (also fine).
    4. Your terminating condition is almost right. However, what would happen if there were 11 or more lines? You would get a buffer overrun condition resulting in undefined behaviour (could crash or do random things). Make sure you don’t go beyond the array boundaries.
    5. What is the final if statement in the loop for? BTW, it is better to use {} for the body of the loop as it reduces inadvertent bugs.
    6. After the while, in your for loop, you probably want the terminating condition to be j < i, not j < 10. Though I would also recommend using more descriptive variable names for clarity.

      Oh, BTW, although j++ is valid and good, I would recommend getting in the habit of using ++j. This is because older compilers may not optimise it very well, and if you move on to C++, iterators are slightly more efficient when using the pre-increment.


    If you want more parsing information look here.

    Good luck,


    Adrian

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #17
      Originally posted by AdrianH
      Savage, you should be ashamed using breaks. Tisk-tisk ;)
      LOL , I'm

      :P

      Savage

      Comment

      • need some help
        New Member
        • Apr 2007
        • 12

        #18
        Thanks. It works now but if i use 15 or more data records it gives me this error,

        *** glibc detected *** free(): invalid next size (normal): 0x085d1b90 ***
        Abort

        I am using this,

        myrec[b] = (char *) malloc(mylength );


        I have use malloc, as "new" dosen't work here. But I am not using free(). is it the reason I m getting error? If I have to use free() , is it like ,

        free(mtrec[b]) ??? but this dosen't work.






        Originally posted by Savage
        LOL , I'm

        :P

        Savage

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #19
          Originally posted by need some help
          Thanks. It works now but if i use 15 or more data records it gives me this error,

          *** glibc detected *** free(): invalid next size (normal): 0x085d1b90 ***
          Abort

          I am using this,

          myrec[b] = (char *) malloc(mylength );


          I have use malloc, as "new" dosen't work here. But I am not using free(). is it the reason I m getting error? If I have to use free() , is it like ,

          free(mtrec[b]) ??? but this dosen't work.
          What do you mean you are not using free()? If you are just leaving the memory so that you have memory leaks, then you shouldn't be getting this error.

          Are you writing in C or C++? new only works in C++.

          Perhaps mylength is too short? Are you also including the extra char for the terminating '\0'? If not, you will probably screw up the heap resulting in that error.


          Adrian

          Comment

          • need some help
            New Member
            • Apr 2007
            • 12

            #20
            I found the error after my post. I need extra space at myrec[]. if my data includes 20 records i need myrec[21]. For mylength I am using using extra char for \0' .


            Originally posted by AdrianH
            What do you mean you are not using free()? If you are just leaving the memory so that you have memory leaks, then you shouldn't be getting this error.

            Are you writing in C or C++? new only works in C++.

            Perhaps mylength is too short? Are you also including the extra char for the terminating '\0'? If not, you will probably screw up the heap resulting in that error.


            Adrian

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #21
              Originally posted by need some help
              I found the error after my post. I need extra space at myrec[]. if my data includes 20 records i need myrec[21]. For mylength I am using using extra char for \0' .
              So you found the error? Glad to hear it.


              Adrian

              Comment

              Working...