Like Tree8Likes
  • 1 Post By Click_here
  • 2 Post By ronin
  • 3 Post By c99tutorial
  • 1 Post By c99tutorial
  • 1 Post By c99tutorial

strcmp

This is a discussion on strcmp within the C Programming forums, part of the General Programming Boards category; Code: scanf(" %[^\n]s",search); while(fread(&rlist,sizeof(rlist),1,fp)) { if(strcmp(search,rlist.surname)==0) { printf("\n\n\t\t*** Customer Information Summary ***"); printf("\n\n\n\t\tName : %s, %s",rlist.surname,rlist.fname); printf("\n\t\tAddress : %s",rlist.address); printf("\n\t\tContact ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    strcmp

    Code:
                  scanf(" %[^\n]s",search);
                       while(fread(&rlist,sizeof(rlist),1,fp))
                       {
                           if(strcmp(search,rlist.surname)==0)
                           {                         
                              printf("\n\n\t\t*** Customer Information Summary ***");
                              printf("\n\n\n\t\tName           : %s, %s",rlist.surname,rlist.fname); 
                              printf("\n\t\tAddress        : %s",rlist.address); 
                              printf("\n\t\tContact number : %s",rlist.telnum); 
                              printf("\n\n\t\tVehicle Rented : %s",rlist.cartype);
                              printf("\n\t\tRent mode      : %s",rlist.mode);
                              printf("\n\n\t\tRent duration  : %s ***",rlist.duration);
                              printf("\n\n\t\tTotal Amount   : Php %s",rlist.amount);
                              printf("\n\t\t*** Cash       : Php %s",rlist.rCash);
                              printf("\n\t\t*** Change     : Php %s ***",rlist.rChange);  
                           }
                           else if(strcmp(search,rlist.surname)!=0)
                           {
                               printf("\n *** No record(s) found, try again... ");
                           }                                           
                       }                 
                       fclose(fp);
                       break;
    How do I print that else if statement ?

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    467
    Um..you are ? Not sure what your question is, but why not just use 'else' instead?

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,303
    strcmp returns a 0 if the two strings are equal.

    To print "No record..." the strcmp must not return 0. Therefore, the strings "search" and "rlist.surname" must be different
    Fact - Beethoven wrote his first symphony in C

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,303
    On second look at your code, I'd just leave it as "else" instead of "else if..."

    The reason for that is if the strings "search" and "rlist.surname" are equal, the "if" statement's original condition is true and the "else if" is never called.

    i.e.
    Code:
    if (strcmp (search, rlist.surname) == 0)
    {                         
        printf("\n\n\t\t*** Customer Information Summary ***");
        printf("\n\n\n\t\tName           : %s, %s",rlist.surname,rlist.fname); 
        printf("\n\t\tAddress        : %s",rlist.address); 
        printf("\n\t\tContact number : %s",rlist.telnum); 
        printf("\n\n\t\tVehicle Rented : %s",rlist.cartype);
        printf("\n\t\tRent mode      : %s",rlist.mode);
        printf("\n\n\t\tRent duration  : %s ***",rlist.duration);
        printf("\n\n\t\tTotal Amount   : Php %s",rlist.amount);
        printf("\n\t\t*** Cash       : Php %s",rlist.rCash);
        printf("\n\t\t*** Change     : Php %s ***",rlist.rChange);  
    }
    else
    {
        printf("\n *** This one record did not match... ");
    }
    Last edited by Click_here; 01-31-2013 at 09:32 PM.
    pipskie likes this.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    if you mean that you don't want to display "no records" on each iteration of the file read where strcmp isn't equal, then you could use a variable to flag whether or not a record has been found.

    Code:
    short found = 0;
    
    while(found == 0 && fread(...) == 1)
    {
       if(strcmp(...))
       {
           ... /print your record
           found = 1;
       }
    }
    
    if(found == 0)
       printf("no records found");
    maybe something like that.
    nonpuz and Salem like this.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    978
    Quote Originally Posted by ronin View Post
    Code:
    short found = 0;
    
    while(found == 0 && fread(...) == 1)
    {
       if(strcmp(...))
       {
           ... /print your record
           found = 1;
       }
    }
    
    if(found == 0)
       printf("no records found");
    What if there are multiple records matching the search query? How about this

    Code:
    bool found=false;
    while (fread(...) == 1) {
       if (strcmp(...) == 0) {
          // ...
          found = true;
       }
    }
    if (!feof(fp)) {
       fprintf(stderr, "Error in reading file!\n");
       exit(EXIT_FAILURE);
    }
    if (!found) {
       printf("No records found!\n");
    }

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Thank you for the replies, helps a lot.

    One more thing though... what if I wanted to print or show those results into a text file or word document maybe, like some sort of a receipt where texts are readable, because the file where those structures are saved are a bunch of characters like ( $˜t t— 2000 ( Ÿt ). So will i just create another FILE and do a normal fwrite ?

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    978
    Once you open a file with a file pointer called fp for example, then a straightforward way to make text files is to just do it like you normally would with printf but use

    Code:
    fprintf(fp, ...);
    fp is what you called your file pointer and the ... is what you normally use with normal printf.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Quote Originally Posted by c99tutorial View Post
    Once you open a file with a file pointer called fp for example, then a straightforward way to make text files is to just do it like you normally would with printf but use

    Code:
    fprintf(fp, ...);
    fp is what you called your file pointer and the ... is what you normally use with normal printf.
    Thanks c99,

    I did this and its working just fine.

    Code:
               fp=fopen("billStatement.txt","a+");
               system("cls");
               fprintf(fp,"\n\n\n\t\t\t\t*** BILLING STATEMENT *** ");
               fprintf(fp,"\n\n\n\t\tName           : %s, %s",rlist.surname,rlist.fname);
    ...would it possible to create a different billStatement.txt to different client? like billStatement2.txt or billStatement3.txt and so on...
    I only did "a+" so it will create another entry at EOF on same file billStatement.txt

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    978
    Quote Originally Posted by pipskie View Post
    ...would it possible to create a different billStatement.txt to different client? like billStatement2.txt or billStatement3.txt and so on...
    I only did "a+" so it will create another entry at EOF on same file billStatement.txt
    Yes. Just do this:
    Code:
    fp=fopen("billStatement.txt",mode);
    fprintf(fp, ...);
    fprintf(fp, ...);
    // ...
    fclose(fp);
    
    fp=fopen("billStatement1.txt",mode);
    fprintf(fp, ...);
    fprintf(fp, ...);
    // ...
    fclose(fp);
    
    // ...
    mode is the modestring like "w" or "a+". For example opening with "w" will truncate the file, thus deleting what was previously inside it. This is normally what programs do when they write a file.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp()
    By Chinchila in forum C Programming
    Replies: 26
    Last Post: 11-02-2012, 01:09 PM
  2. strcmp
    By Soulzityr in forum C Programming
    Replies: 9
    Last Post: 03-23-2010, 04:36 PM
  3. strcmp help!!
    By apm in forum C Programming
    Replies: 14
    Last Post: 06-07-2007, 06:54 PM
  4. strcmp???
    By SvNo7 in forum C Programming
    Replies: 7
    Last Post: 12-30-2006, 03:34 PM
  5. What does strcmp actually do?
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 12:32 PM

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