I'm trying to add a username, password, firstname and lastname on a text file. Everytime I run the program, it only accepts one username, password, etc. Can anyone please help? here's my code.

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

typedef struct
{
    char UName[50];
    char PWord[50];
    char FName[50];
    char LName[50];
} list;

char search(list *ptr, int *count, char search_val[50])
{
    int i, found = -1;

    for (i = 0; i < *count; i++)
    {
        if (((ptr + i)->UName) == search_val)
        {
            found = i;
            break;
        }
    }

    return found;
}

void SignUp (list *ptr, int *count)
{
            if (*count == 100)
                printf("\nSorry, the maximum users has been reached. ");
            else
            {
                printf("\nEnter a Username: ");
                fscanf(stdin, "%s", (ptr + *count)->UName);

                if (search(ptr, count, (ptr + *count)->UName) == -1)
                {
                    printf("Enter a Password: ");
                    fscanf(stdin, "%s", (ptr + *count)->PWord);
                    printf("Enter your First Name: ");
                    fgets((ptr + *count)->FName, 50, stdin);
                    fgets((ptr + *count)->FName, 50, stdin);
                    printf("Enter your Last Name: ");
                    fgets((ptr + *count)->LName, 50, stdin);

                    *count = *count + 1;
                }
                else
                    printf("\nUsername %s already exists.\n", (ptr + *count)->UName);
            }
}
int main()
{
    friendster rec[101], *rec_ptr;
    FILE *file_ptr;
    char enter;
    int done = 0;
    int ctr = 0;
    int *count;

    file_ptr = fopen("Users.txt", "r+tb");

    rec_ptr = rec;

    while (!done)
    {
        printf("\n[S]ign up");
        printf("\n[L]og in");
        printf("\n[E]xit\n");

        printf("\nPlease choose any options above: ");
        scanf(" %c", &enter);

        switch (enter)
        {
        case 'S':
        case 's':
        {
            SignUp(rec_ptr, &ctr);
            fprintf(file_ptr, "%s\n%s\n%s%s*\n", rec_ptr->UName, rec_ptr->PWord, rec_ptr->FName, rec_ptr->LName);
            break;
        }

        case 'L':
        case 'l':
        {
        }

        case 'E':
        case 'e':
        {
            printf("Goodbye!!!");
            rewind(file_ptr);
            done = 1;
            break;
        }

        default:
        {
            printf("\nThat is invalid.\n");
            break;
        }
        }
    }

    fclose(file_ptr);
}

smart questions

I'll leave you to figure out the number of places where [code] tags are mentioned, and as to why you failed to read (or heed) any of them.

Hi,
Please be more specific...
First of all, use code tags when you put up the code...
Secondly, whats the actual issue?... Did you mean it runs only once even if you select Sign Up? or is it taking only Username and Password?

Tips : Clear the input buffer before you take in the next input.

Clearer the question, quicker it will get replies...

When I run the program, it isn't saving the actual details that I entered to the text file, also, when i run the program again, the details in the textfile gets overwritten.

How do I clear the input buffer?

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

typedef struct
{
    char UName[50];
    char PWord[50];
    char FName[50];
    char LName[50];
} list;

char search(list *ptr, int *count, char search_val[50])
{
    int i, found = -1;

    for (i = 0; i < *count; i++)
    {
        if (strcmp(((ptr + i)->UName),search_val)==0)
        {
            found = i;
            break;
        }
    }

    return found;
}

void SignUp (list *ptr, int *count)
{
    FILE *file_ptr;
        file_ptr = fopen("Users.txt", "r+tb");
            if (*count == 100)
                printf("\nSorry, the maximum users has been reached. ");
            else
            {
                printf("\nEnter a Username: ");
                fscanf(stdin, "%s", (ptr + *count)->UName);

                if (search(ptr, count, (ptr + *count)->UName) == -1)
                {
                    printf("Enter a Password: ");
                    fscanf(stdin, "%s", (ptr + *count)->PWord);
                    printf("Enter your First Name: ");
                    fgets((ptr + *count)->FName, 50, stdin);
                    fgets((ptr + *count)->FName, 50, stdin);
                    printf("Enter your Last Name: ");
                    fgets((ptr + *count)->LName, 50, stdin);


                }
                else
                    printf("\nUsername %s already exists.\n", (ptr + *count)->UName);
            }
                        fprintf(file_ptr, "%s\n%s\n%s%s*\n", (ptr + *count)->UName, (ptr + *count)->PWord, (ptr + *count)->FName, (ptr + *count)->LName);
                        fclose(file_ptr);
}
int main()
{
    list rec[101], *rec_ptr;
    FILE *file_ptr;
    char enter;
    int done = 0;
    int ctr = 0;

        file_ptr = fopen("Users.txt", "r+tb");

    rec_ptr = rec;


    while (!done)
    {
        printf("\n[S]ign up");
        printf("\n[L]og in");
        printf("\n[E]xit\n");

        printf("\nPlease choose any options above: ");
        scanf(" %c", &enter);

        switch (enter)
        {
        case 'S':
        case 's':
        {
            SignUp(rec_ptr, &ctr);
            ctr += 1;
            break;

        }

        case 'L':
        case 'l':
        {
        }

        case 'E':
        case 'e':
        {
            printf("Goodbye!!!");
            rewind(file_ptr);
            done = 1;
            break;
        }

        default:
            printf("\nThat is invalid.\n");


        }
    }

        fclose(file_ptr);
}

I am a little confused with all the file opening .. first you open Users.txt in your main() function .. then you open it again in your sign-up function. Is there a specific purpose to that ?

And your fopen line file_ptr = fopen("Users.txt", "r+tb"); Is your file binary or text ? Also I believe that "r+tb" implies a compound mode and means that you just want to read the file which is err either binary or text . Its different from say "rb+" which implies a mixed mode ie you can use the file for update with both and writing.

I just thought that I might need it in the latter part. Anyway, I changed it already.

My problem is still the same, If I enter S, to sign up, after signing up, I enter S again to enter another user. when I exit and check the Users.txt, it only shows one user. Can someone please help me???

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

typedef struct
{
    char UName[50];
    char PWord[50];
    char FName[50];
    char LName[50];
} list;

char search(list *ptr, int *count, char search_val[50])
{
    int i, found = -1;

    for (i = 0; i < *count; i++)
    {
        if (strcmp(((ptr + i)->UName),search_val)==0)
        {
            found = i;
            break;
        }
    }

    return found;
}

void SignUp (list *ptr, int *count)
{
    FILE *file_ptr;
        file_ptr = fopen("Users.txt", "r+t");
            if (*count == 100)
                printf("\nSorry, the maximum users has been reached. ");
            else
            {
                printf("\nEnter a Username: ");
                fscanf(stdin, "%s", (ptr + *count)->UName);

                if (search(ptr, count, (ptr + *count)->UName) == -1)
                {
                    printf("Enter a Password: ");
                    fscanf(stdin, "%s", (ptr + *count)->PWord);
                    printf("Enter your First Name: ");
                    fgets((ptr + *count)->FName, 50, stdin);
                    fgets((ptr + *count)->FName, 50, stdin);
                    printf("Enter your Last Name: ");
                    fgets((ptr + *count)->LName, 50, stdin);


                }
                else
                    printf("\nUsername %s already exists.\n", (ptr + *count)->UName);
            }
                        fprintf(file_ptr, "%s\n%s\n%s%s*\n", (ptr + *count)->UName, (ptr + *count)->PWord, (ptr + *count)->FName, (ptr + *count)->LName);
                        fclose(file_ptr);
}
int main()
{
    list rec[101], *rec_ptr;

    char enter;
    int done = 0;
    int ctr = 0;



    rec_ptr = rec;


    while (!done)
    {
        printf("\n[S]ign up");
        printf("\n[L]og in");
        printf("\n[E]xit\n");

        printf("\nPlease choose any options above: ");
        scanf(" %c", &enter);

        switch (enter)
        {
        case 'S':
        case 's':
        {
            SignUp(rec_ptr, &ctr);
            ctr += 1;
            break;

        }

        case 'L':
        case 'l':
        {
        }

        case 'E':
        case 'e':
        {
            printf("Goodbye!!!");
            done = 1;
            break;
        }

        default:
            printf("\nThat is invalid.\n");


        }
    }


}

Try to open the file in the append mode "a+t"

I fixed the signup, but now the search is not working. It's supposed to search the Users.txt if the entered username is already written in the text file. but it's not working, it still accepts the username, even though I already entered it. It should have given me a message that the username i entered is already there.

Please help with my search function. Thanks!

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

typedef struct
{
    char UName[50];
    char PWord[50];
    char FName[50];
    char LName[50];
} list;

char search(list *ptr, int *count, char search_val[50])
{
    int i, found = -1;

    for (i = 0; i < *count; i++)
    {
        if (((ptr + i)->UName) == search_val)
        {
            found = i;
            break;
        }
    }

    return found;
}

void SignUp (list *ptr, int *count)
{

    FILE *file_ptr;
        file_ptr = fopen("Users.txt", "a+");
            if (*count == 100)
                printf("\nSorry, the maximum users has been reached. ");
            else
            {
                printf("\nEnter a Username: ");
                fscanf(stdin, "%s", (ptr + *count)->UName);

                if (search(ptr, count, (ptr + *count)->UName) == -1)
                {
                    printf("Enter a Password: ");
                    fscanf(stdin, "%s", (ptr + *count)->PWord);
                    printf("Enter your First Name: ");
                    fgets((ptr + *count)->FName, 50, stdin);
                    fgets((ptr + *count)->FName, 50, stdin);
                    printf("Enter your Last Name: ");
                    fgets((ptr + *count)->LName, 50, stdin);

                    fprintf(file_ptr, "%s\n%s\n%s%s*\n", (ptr + *count)->UName, (ptr + *count)->PWord, (ptr + *count)->FName, (ptr + *count)->LName);
                }
                else
                    printf("\nUsername %s already exists.\n", (ptr + *count)->UName);
            }

                        fclose(file_ptr);
}

int main()
{
    friendster rec[101], *rec_ptr;
    FILE *file_ptr;
    char enter;
    int done = 0;
    int ctr = 0;
    int *count;

    file_ptr = fopen("Users.txt", "r+tb");

    rec_ptr = rec;

    while (!done)
    {
        printf("\n[S]ign up");
        printf("\n[L]og in");
        printf("\n[E]xit\n");

        printf("\nPlease choose any options above: ");
        scanf(" %c", &enter);

        switch (enter)
        {
        case 'S':
        case 's':
        {
            SignUp(rec_ptr, &ctr);
            fprintf(file_ptr, "%s\n%s\n%s%s*\n", rec_ptr->UName, rec_ptr->PWord, rec_ptr->FName, rec_ptr->LName);
            break;
        }

        case 'L':
        case 'l':
        {
        }

        case 'E':
        case 'e':
        {
            printf("Goodbye!!!");
            rewind(file_ptr);
            done = 1;
            break;
        }

        default:
        {
            printf("\nThat is invalid.\n");
            break;
        }
        }
    }

    fclose(file_ptr);
}

> if (((ptr + i)->UName) == search_val)
1. You can simplify the pointer notation to ptr[i].UName (just like if you were accessing the original array in main
2. Use strcmp to compare strings, like if ( strcmp( ptr[i].UName,search_val) == 0 ) You're also mixing fgets and fscanf, which will lead to surprises.

char buff[BUFSIZ];
fgets( buff, BUFSIZ, stdin );
sscanf( buff, "%s", ptr[*count].UName );

Always fgets() to a temporary buffer, then extract what you need.
Both functions return status results, which later on you'll need to check for really robust code.

hey salem!sorry for the late reply, I had to go to class. I did what you told me, now it's searching fine but when i exit the program and try to add the same user, it doesn't check if the username already exists in the txt file. So it just accepts it and write it on the txt file. even though it already has the same username. Can you tell me what i'm missing here? Really appreciate your help. =)

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

typedef struct
{
    char UName[50];
    char PWord[50];
    char FName[50];
    char LName[50];
} list;

char search(list *ptr, int *count, char search_val[50])
{
     int i, found = -1;
    FILE *file_ptr;

    file_ptr = fopen("Users.txt", "r");
    for (i = 0; i < *count; i++)
    {
        if (strcmp(ptr[i].UName,search_val)==0)
        {
            found = i;
            break;
        }
    }
    return found;
}

void SignUp (list *ptr, int *count)
{
    char buff[50];
    FILE *file_ptr;
        file_ptr = fopen("Users.txt", "a+");
            if (*count == 100)
                printf("\nSorry, the maximum users has been reached. ");
            else
            {
                printf("\nEnter a Username: ");
                fgets(buff, 50, stdin);
                fgets(buff, 50, stdin);
                sscanf(buff, "%s", ptr[*count].UName);

                if (search(ptr, count, (ptr + *count)->UName) == -1)
                {
                    printf("Enter a Password: ");
                    fgets(buff, 50, stdin);
                    sscanf(buff, "%s", ptr[*count].PWord);
                    printf("Enter your First Name: ");
                    fgets(buff, 50, stdin);
                    sscanf(buff, "%s", ptr[*count].FName);
                    printf("Enter your Last Name: ");
                    fgets(buff, 50, stdin);
                    sscanf(buff, "%s", ptr[*count].LName);

                    fprintf(file_ptr, "%s\n%s\n%s\n%s\n*\n", (ptr + *count)->UName, (ptr + *count)->PWord, (ptr + *count)->FName, (ptr + *count)->LName);
                }
                else
                    printf("\nUsername %s already exists.\n", (ptr + *count)->UName);
            }

                        fclose(file_ptr);
}
int main()
{
    list rec[101], *rec_ptr;

    char enter;
    int done = 0;
    int ctr = 0;

    rec_ptr = rec;

    while (!done)
    {
        printf("\n[S]ign up");
        printf("\n[L]og in");
        printf("\n[E]xit\n");

        printf("\nPlease choose any options above: ");
        scanf(" %c", &enter);

        switch (enter)
        {
        case 'S':
        case 's':
        {
            SignUp(rec_ptr, &ctr);
            ctr += 1;
            break;

        }

        case 'L':
        case 'l':
        {
        }

        case 'E':
        case 'e':
        {
            printf("Goodbye!!!");

            done = 1;
            break;
        }

        default:
            printf("\nThat is invalid.\n");


        }
    }

}

Why does search need to open the file (again)?
It doesn't use it (nor does it close it).

To avoid the double fgets(), you need to apply the same technique to main() as well (the scanf call).

Because I need to test it, after adding data, I need to close the file and open it again. When I open it again, and I enter the same username, it should check the file if it already has in common, and if not, it will continue to ask for the password, but if it has a duplicate, then a message will prompt that the username entered already exists in the file.

I didn't get what you mean by placing the scanf call in main. Could you show me how?

Can someone please help me with my search function? If there is data on the text file, and I run the program, when I enter a username, I want it to search for the username that I just entered inside the text file. If it has duplicate, then it will prompt a message that that username already exists in the text file. Can someone please help me to do that? I'm new in programming, and have been researching on the net for 2 days now, and still I can't find how.

First you need to decide which function is supposed to be reading the file.

In your search function, you are just opening the file... you are not reading from it... without reading you are searching...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.