?
Solved

how can the strcmp() be revised?

Posted on 2000-04-06
7
Medium Priority
?
325 Views
Last Modified: 2011-09-20
/*  First part works */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <dir.h>
#include <dos.h>

#define first_file  "c:\\password\\password.txt"

void main(void)
{  //start main
 FILE *f1;

 char a_z[81], key_in;
 int handle;
 int compare,directory;
 clrscr();

 f1 = fopen (first_file,"r");
        if(f1 != NULL)
      goto next;

// MAKE DIRECTORY
  if(f1 == NULL)
      {
      directory = mkdir("c:\\password");
        if(!directory)
             printf("Directory created\n");
         else
            {
            printf("Unable to create directory along with file\n");
            printf("The directory may already exist..\n");
            printf("Will default back the password file contents\n");
            }

// MAKE FILE
        if (_dos_creat("C:\\password\\password.txt", _A_NORMAL, &handle) != 0)
             {
              perror("Unable to make file");
             }
             _dos_close(handle);
             fclose(f1);
      }

// WRITE TO FILE
         f1 = fopen (first_file,"w");
         strcpy(a_z,"password\n" );
         fputs(a_z,f1);
         fclose(f1);
         getch();

      next:;


/************************************************/


/* This section that follows will compile */
/* The program will crash */
/* What makes this fail? */

/*   // <--- remove ( or add // )

// reads from file
      fgets(a_z,81,f1);
      printf("Enter password\n");
      scanf("%s", &key_in); // read in keyed value

// this next line to omit
      printf("  %s \n  %s", a_z, &key_in);
      compare = strcmp(a_z, &key_in);
         if(compare == 0)
         {
         printf("\n\nequal\n");
         }
         if(compare != 0)
         {
         printf("\n\nnot equal\n");
         }
      fclose(f1);
      getch();

*/   // <--- remove ( or add // )

/************************************************/

}  //end of main
0
Comment
Question by:jgrosber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2692437
i'm not at home right now so i can't try your code

but about the
// this next line to omit
compare = strcmp(a_z, &key_in);
what are you trying to do
a_z is a string and key_in is a char
and more strcmp when it comparing two string's it goes till '\0' and your char can't have a char and the '\0' char so strcmp should crush
but if you're comparing string with char use the operator ==
a_z[i]==key_in
if you want to compare all the chars in the string with key_in use for  
0
 
LVL 18

Expert Comment

by:deighton
ID: 2692620
you are using scanf with %s instead of %c for a char.
0
 
LVL 1

Expert Comment

by:mahno
ID: 2693157
try to change:
char a_z[81], key_in;
to:
char a_z[81], key_in[81];

and:
all "&key_in" to "key_in"

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:jgrosber
ID: 2695566


  ntdragon- about "// this next line to omit" has the literal meaning of just that, I did not eliminate it after I pasted and submitted. Im not to knowledgable about C and not certain
in what to use in some instances. The
overview is to have a password input and to have a file created behind the scenes in case there wasn't one to begin with. I did not need to include
the first half.

  mahno- I'd did try and with success on a minimized version of the code
compile, step thru and run it. I've then
added the full contents and found that when strcmp(...) came into play, the condition statments always results !=0
even when the password was correct (password). I'd then try to see if a printf statement - printf("%i",compare);
incerted just before the compare condition will reveal anything asper a trouble shooting technic. It came up password10.

  I will need to try and fit in  (== a_z[i]==key_in ) as ntdragon suggest to see how it comes to form. Im not done yet and will work this thru upon suggestions.

0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2695902
i told you why your prog crush i really think that becouse of the strcmp as i said

now about fixing your code:

char key_in[81];//make it a string not
                //a single char

// reads from file
fgets(a_z,81,f1);
printf("Enter password\n");

//without the '&'
scanf("%s", key_in); // read in keyed
                     //  value

// this next line to omit
//you're printing the right password
//if you"ll do that the user will know
//the password so remove the next line
//printf("  %s \n  %s", a_z, &key_in);

//without the '&'
compare = strcmp(a_z, key_in);
   if(compare == 0)
   {
   printf("\n\nequal\n");
   }
   if(compare != 0)
   {
   printf("\n\nnot equal\n");
   }
fclose(f1);
getch();

0
 
LVL 1

Accepted Solution

by:
mahno earned 30 total points
ID: 2699457
Hi jgrosber,

There is a working code (I'm test it):

#include "string.h"
#include "conio.h"

#define first_file "pwd.txt"

void save_pwd() {
  FILE *f1;
  char a_z[81];
  f1 = fopen (first_file,"w");
  strcpy(a_z,"password" ); /*NOTE: Save to file "password", NOT "password\n"!!!*/
  fputs(a_z,f1);
  fclose(f1);
}

int main(int argc, char* argv[]) {
  char key_in[81], a_z[81];
  FILE *f1;
  int compare;

  /*Store pwd to file*/
  save_pwd();

  /*reads from file*/
  f1 = fopen (first_file,"r");
  fgets(a_z,81,f1);
  printf("Enter password\n");
  fclose(f1);

  scanf("%s", key_in); /*read in keyed value*/

  /*compare passwords*/
  compare = strcmp(a_z, key_in);
  if(compare == 0) {
    printf("\n\nequal\n");
  } else {
    printf("\n\nnot equal\n");
  }

  getch();
  return 0;
}

Ur mistake was in line:
 strcpy(a_z,"password\n" );
need to change to:
 strcpy(a_z,"password" );

Hope that helps,
  mahno
0
 

Author Comment

by:jgrosber
ID: 2702882
mahno - Thanks for pointing out the err
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question