Solved

how can the strcmp() be revised?

Posted on 2000-04-06
7
282 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
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
Comment Utility
you are using scanf with %s instead of %c for a char.
0
 
LVL 1

Expert Comment

by:mahno
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:jgrosber
Comment Utility


  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
Comment Utility
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 10 total points
Comment Utility
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
Comment Utility
mahno - Thanks for pointing out the err
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now