Writing data to a file

+1 Eric V K · November 26, 2014
hi guys, i have a little problem here. i dunno why when i try to register the name in function 2 below, it doesn't print the name in the file but print the others details beside the name,i try to figure it out but cannot.

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

int main ()
int value,postcode,contactNo,temp,status;
char fullname[30];
char dob [10];
char username [15];
char pwd [20];
char user_pass [30];
char userfile [100];
FILE *user;
FILE *admin;

printf("Delivery Checker\n");
printf("1. Check Malaysia State\n");
printf("2. Register\n");
printf("3. Admin Login\n");
printf("4. Update Details\n");
printf("5. Delete user\n");
printf("6. Exit\n");
printf("Enter a value 1-6:\n");
scanf("%d", &value);

        while (value < 1 || value > 6)        // Validate value input.
            while((temp=getchar()) != EOF && temp != '\n');
            printf("Invalid input... please Enter a value 1-6 \n");
            status = scanf("%d", &value);

            case 1:     // Check Postcode

            case 2:     // User Registration
                if((user = fopen("user.txt","a")) == NULL){
printf("\nError to open file\n");

                    printf("Please enter your Name:\n");
                    scanf("%s", fullname);

                    printf("Please enter your Date of birth:\nFormat: dd/mm/yyyy\n");
                    scanf("%s", dob);

                    printf("Please enter 8 digit of your Contact No:\n+601-xxxxxxxx\n");
                    scanf("%d", &contactNo);

                    printf("Please enter your Postcode:\n");
                    scanf("%d", &postcode);

                    fprintf(user, "%s;%s;+601%d;%d\n", fullname,dob,contactNo,postcode);
                    printf("\nRegistration Complete\n");

            case 3:

            case 4:

            case 5:

            case 6:
                printf("End of Program\n");
    }while (value != 6);
return 0;

Post a Reply


Oldest  Newest  Rating
+1 Mathias Frits Rørvik · November 26, 2014
When you are entering a name to the program, are you entering both last name and first name, or only first name?

Because scanf only reads one word at a time. Instead you can use fgets.

This is how you use them
gets(fullname, sizeof fullname, stdin);
//fgets adds a \n to the end of the name
//so we must remove it
fullname[strlen(fullname) - 1] = '\0'
0 c student · December 5, 2014
if you are having trouble with the full name and you wish to use scanf, there is a way:
scanf is a pretty neat function with its flexibility to read in more than one input.  in your case, it can be resolved with the following:

1. declare a firstname and lastname variable,
2. scanf ("%s %s", firstname, lastname);
3. you may then use the strcat() function from string.h to concatenate the lastname onto the firstname and get a fullname.

here's me trying testing your code:
note: seg fault means accessing inappropriate data

and here's me rerunning the program straight after:

one more time, this time overflowing everything:

to fix your buffer overflow vulnerabilities, you need to place checks to ensure you have a valid input!

p.s. sorry for breaking your program, i just like breaking things :angel:
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator