Solved

how can the strcmp() be revised?

Posted on 2000-04-06
7
301 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
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 10 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Test against App 49 137
distributed computing in Python 4 123
Constant string is of type char *   ? 7 30
Test the speeds on my PC Drives 12 48
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…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

777 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