-1

I am new to programming and have a few questions as to how to implement this idea.

I am looking to have a user enter their name/string of digits and if their name is on a list, to then execute a string of commands. I am not to sure how to impliment this, but with some gogle-ing I was able to come up with this code:

#include <stdio.h>
#include <stdlib.h>


   int main(void)
   {

      char userName[10];

      printf("\n\n\n\nPlease enter your name: ");
      scanf_s("%s",userName);     // userName should be verified/found inside the results.dat file

      FILE *fp;

      fp = fopen("results.dat", "r");

      if (fp == NULL) {
         printf("I couldn't open results.dat for writing.\n");
         exit(0);
      }

      if (fp == John) {
            //Dispence squence of pills for John
      }

      if (fp == Mary) {
            //Dispence squence of pills for Mary
      }



      return 0;

   }

I do not think I am using the if statement correctly. how can I do something like:

if (content in fp == john, execute/call another function)

Thanks in advance!

Casper Beyer
  • 2,203
  • 2
  • 22
  • 35
James Hayek
  • 643
  • 3
  • 10
  • 35
  • Use `fscanf`. You can't read the content from the handler `fp` directly. – brokenfoot Apr 20 '14 at 22:32
  • A file pointer does not contain the file values. You have to use a [I/O function](http://www.codingunit.com/c-tutorial-file-io-using-text-files) to read the contents. Suggest you get a beginning C book and read it. – OldProgrammer Apr 20 '14 at 22:33
  • "Suggest you get a beginning C book and read it." This is for a project for class. I am trying to learn C by reading the documentation, programming and asking knowledgeable people such as yourself. – James Hayek Apr 20 '14 at 23:24

2 Answers2

2

Try this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

   int main(void)
   {

      char userName[10];
      char names[20];
      printf("\n\n\n\nPlease enter your name: ");
      scanf("%s",userName);     // userName should be verified/found inside the results.dat file

      FILE *fp;

      fp = fopen("results.dat", "r");

      if (fp == NULL) {
         printf("I couldn't open results.dat for writing.\n");
         exit(0);
      }

      while(fgets(names, 20, fp)) // fgets reads a line from the file
      {
        names[strlen(names)-1] = '\0'; // but it leaves the newline character "\n" , so the strings won't match
        if(strcmp(names, userName) == 0) // if the value returned by strcmp is zero then string match
        {
            printf("Match found\n");
        }
      }

      return 0;

   }
1

fopen simply opens a file for reading and/or writing, to read the actual content of the file you need to use functions such as fgets, fscanf and so on.

Short example

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char* argv[])
{
   char name[64];
   char buffer[64];

   printf ("Please enter your name: ");

   file = fopen ("results.dat", "rw");
   if (!file) {
      printf ("Results.dat could not be opened.\n");
      exit(-1);
   }

   if ( fgets (buffer, 64, file)) {
      if (strcmp (buffer, "john")) {
         printf ("Contents of file is john\n");
      }
   }

   return 0;
}
Casper Beyer
  • 2,203
  • 2
  • 22
  • 35