I these contents below written to a file.I want to user to input a number and i will then scan the file to see if it matches my registration number. But in doing this i just want to get the 1001 from the first line.

REGISTRATION: 1001
NAME Donellie Whyte
ADDRESS: 5 Plover Road
NUMBER: 234-7890
DATE OF BIRTH: 12/12/12
LAST TREATMENT: Filling
ALLERGIES: None
LAST APPOINTMENT: 5/09/09

On this forum, we try to NOT give out code, until after the poster (you), have shown some work to make the program.

We want to HELP, not become "homework central" for every student who would like to have their assignment done for them. Do you see what I mean?

Actually, I already wrote up a little program just for you, but I'm not going to post it unless and until you show some EFFORT.

So get posting! ;)

Edited 6 Years Ago by Adak: n/a

I these contents below written to a file.I want to user to input a number and i will then scan the file to see if it matches my registration number. But in doing this i just want to get the 1001 from the first line.

REGISTRATION: 1001
NAME Donellie Whyte
ADDRESS: 5 Plover Road
NUMBER: 234-7890
DATE OF BIRTH: 12/12/12
LAST TREATMENT: Filling
ALLERGIES: None
LAST APPOINTMENT: 5/09/09

Wow! Cool! Super!

What code have you written?

Edited 6 Years Ago by Dave Sinkula: n/a

On this forum, we try to NOT give out code, until after the poster (you), have shown some work to make the program.

We want to HELP, not become "homework central" for every student who would like to have their assignment done for them. Do you see what I mean?

Actually, I already wrote up a little program just for you, but I'm not going to post it unless and until you show some EFFORT.

So get posting! ;)

I was thinking go doing a while loop and fscanf to obtain

void updaterecord(void)
{
     
   system("color 6d");
   
   struct custinfo info;
   
   FILE *customer1;  
   
   char number[5];
   int x;
   
   
  // printf("\n\n\t  PLEASE ENTER A THE REGISTRATION NUMBER TO BE FOUND:");
  // scanf("%s",&number);    


   customer1 = fopen("customer1.txt","r"); 
   
   
   
              fscanf(customer1,"%[^:]:\n",&info.regis);
              fscanf(customer1,"%s\n",info.Name);
              fscanf(customer1,"%s\n",info.treatment); 
              fscanf(customer1,"%s\n",info.allergies);
              fscanf(customer1,"%s\n",info.dob);
              fscanf(customer1,"%s\n",info.app);               
               
                 
                      
                      
              printf("%s\n",info.regis);
              printf("%s\n ",info.Name);
              printf("%s\n",info.treatment);
              printf("%s\n",info.allergies);
              printf("%s\n",info.dob);
              printf("%s\n",info.app); 
               
                
               
     
                       
     
     fclose(customer1);
     
}

Fscanf() has it's place, but the whole family of scanf() is very "fragile" and leads to code that breaks easily.

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

int main() {
  int i, j, regnum; 
  FILE * fp;
  char str[50] = { '\0' };
  char number[10] = { '\0' };

  /* if the file is in another directory, you need to include
  the full path and filename: C:\\directory\filename with two 
  backslashes on the first directory, or C:/directory/filename
  using just one forward slash.

  Weird eh? ;)
  */
  fp = fopen("C:/Rtest.txt", "rt");
  if(fp==NULL) {
    printf("\nFile Error - terminating the program");
    return 1;
  }

  fgets(str, sizeof(str), fp); 
  printf("\n\n%s\n\n\nYour number is: ", str);
  for(i = 0, j = 0; i < strlen(str); i++) {
    if((str[i] >= '0') && (str[i] <= '9')) {
      putchar(str[i]);
      number[j++] = str[i];
    }
  }
  number[j] = '\0';
  regnum = atoi(number);  

  printf("\nRegistration Number is: %d", regnum);

  fclose(fp);       
  printf("\n\n\t\t\t     press enter when ready");

  i = getchar();
  return 0;
}

Edit: Added some code to save the digits in a separate integer, regnum.

Edited 6 Years Ago by Adak: n/a

Comments
i am not a fan of giving away code wholesale. And while scanf() is usually a poor choice for input, fscanf and sscanf are powerful functions and are very good choices when the input is constrained.
To balance the negative of Mr WaltP, just because you added a couple lines more.
We don't do other's homework for them. We help them do their own.

Fscanf() has it's place, but the whole family of scanf() is very "fragile" and leads to code that breaks easily.

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

int main() {
  int i, j, regnum; 
  FILE * fp;
  char str[50] = { '\0' };
  char number[10] = { '\0' };

  /* if the file is in another directory, you need to include
  the full path and filename: C:\\directory\filename with two 
  backslashes on the first directory, or C:/directory/filename
  using just one forward slash.

  Weird eh? ;)
  */
  fp = fopen("C:/Rtest.txt", "rt");
  if(fp==NULL) {
    printf("\nFile Error - terminating the program");
    return 1;
  }

  fgets(str, sizeof(str), fp); 
  printf("\n\n%s\n\n\nYour number is: ", str);
  for(i = 0, j = 0; i < strlen(str); i++) {
    if((str[i] >= '0') && (str[i] <= '9')) {
      putchar(str[i]);
      number[j++] = str[i];
    }
  }
  number[j] = '\0';
  regnum = atoi(number);  

  printf("\nRegistration Number is: %d", regnum);

  fclose(fp);       
  printf("\n\n\t\t\t     press enter when ready");

  i = getchar();
  return 0;
}

Edit: Added some code to save the digits in a separate integer, regnum.

Thank you for that but if i wanted to search the file for other records can this code be modified to achieve that

Thank you for that but if i wanted to search the file for other records can this code be modified to achieve that

Why? Are you planning on using his code as your code or were you planning on fixing your own code after understanding his?

Why? Are you planning on using his code as your code or were you planning on fixing your own code after understanding his?

I am going to fix my code to model this or something of the sort but what if i have multiple records with id numbers such as that above could this code be implemented in such a way to search the file

I am going to fix my code to model this or something of the sort but what if i have multiple records with id numbers such as that above could this code be implemented in such a way to search the file

Then you need to understand not only what he did, but what you need. Then think about how you can accomplish your search.

Direct answer, of course it can. It's code. It can be modified to do anything.

Thank you for that but if i wanted to search the file for other records can this code be modified to achieve that

Can it be modified to search through records? Yes, but you wouldn't want to do that.

In a program, the *design* has to fit the purpose (it's a form and function get-together).

For what you want, the way to go is to make each record into a struct, which allows all these fields for one person, to be "glued" together, like an object, with parts: name, registration number, date of birth, sex, age, address, etc.

Those are handled in a different (binary), file mode, and you wouldn't need to go "hunting" for the registration number, because the struct would have that information, already known by the program.

The field for "registration number:" would never contain the string "registration" or "number" or ":". It would just contain the actual number, and the structure would show the program just where to find it, within the record.

It's very efficient that way - fast and a smaller amount of data per record.

You'll need to do some studying between now, and when you can work with structs - maybe 30-90 days?

You'll need a good book, and time to study it. Practice is also very important. It's all fine to study how to swim on dry land, but you LEARN how to swim, by getting into the water. And if you don't practice C for a week or two, you will quickly start forgetting what you worked hard to learn. Especially as a beginner.

The book I recommend the most for beginners is:

Beginning C Programming, by Ivor Horton

Covers all the basics. The index is not great, but the examples in each chapter are fantastic. Horton's teaching method is gentle, not terse like K&R's book, but he never bores you. He shows examples of using structs and records, of course.

So study up, and come back when you have some more questions.

This article has been dead for over six months. Start a new discussion instead.