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;

do{
        printf("\n-------------------\n");
printf("Delivery Checker\n");
printf("-------------------\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("-------------------\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);
        }

        switch(value)
        {
            case 1:     // Check Postcode
                break;

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

                    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");
                    fclose(user);
                }
                break;

            case 3:
                break;

            case 4:
                break;

            case 5:
                break;

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

Post a Reply

Replies

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:
/images/forum/upload/2014-12-05/97875ae07b5ce803c5d59733874aec9c.jpg
note: seg fault means accessing inappropriate data

and here's me rerunning the program straight after:
/images/forum/upload/2014-12-05/601581fee23d2491bb466bf81f0d37bb.jpg
:)

one more time, this time overflowing everything:
/images/forum/upload/2014-12-05/b8937f72061c62509768de68fc3d2544.png

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

C

107,019 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator