how do i test to see if my program is storing info or writing over it - c

ok i have incluced my structure as well as my pointer, here is what i am trying to figure out, i need to store up to 5 peoples profiles, but i do not know how to store these in my array while using a pointer
if i don't have the pointer i can do it like this:
strcpy(user[0].UserName,"whatevername");
strcpy(user[0].UserName,"whateverpwd");
but how do i specify where in the array i want the info while using a point that points to my structure.. i hope this makes sense i don't think i can explain it any better
struct profile
{
char First[15];
char Last[15];
char Pwd[10];
char UserName[10];
};
struct profile user[100];
struct profile *puser;
puser=&user[0];
void add_user(struct profile *puser)
{
int i = 0;
int j = 0;
int quit = 0;
char fname[30];
char lname[30];
char username[30];
char password[30];
do
{
printf("Enter the first name of the user:\n");
fgets((puser+i)->First,15, stdin);
printf("Enter the last name of the user:\n");
fgets((puser+i)->Last, 15, stdin);
printf("Enter the username:\n");
fgets((puser+i)->UserName, 30, stdin);
printf("Enter the password:\n");
fgets((puser+i)->Pwd, 30, stdin);
printf("the first name is: %s\n", (puser+i)->First);
printf("the last name is: %s\n", (puser+i)->Last);
printf("the user name is: %s\n", (puser+i)->UserName);
printf("the password name is: %s\n", (puser+i)->Pwd);
j++;
printf("enter 0 to exit 1 to continue:");
scanf("%d", &quit);
if(quit == 0)
printf("goodbye");
i++;
getchar();
}while(quit == 1);
}

Try this:
#include <stdio.h>
typedef struct
{
char First[15];
char Last[15];
char Pwd[10];
char UserName[10];
}profile;
profile user[100];
profile *puser = user;
void add_user(profile *puser);
void add_user(profile *puser)
{
int i = 0;
int j = 0;
int quit = 0;
char fname[30];
char lname[30];
char username[30];
char password[30];
do
{
printf("Enter the first name of the user:\n");
fgets(puser[i].First,15, stdin);
printf("Enter the last name of the user:\n");
fgets(puser[i].Last, 15, stdin);
printf("Enter the username:\n");
fgets(puser[i].UserName, 30, stdin);
printf("Enter the password:\n");
fgets(puser[i].Pwd, 30, stdin);
printf("enter 0 to exit 1 to continue:");
scanf("%d", &quit);
getchar();
i++;
}while(quit == 1);
for( j = 0; j < i; j++ )
{
printf("first name[%d] is: %s\n", j,(puser+j)->First);
printf("last name[%d] is: %s\n", j,(puser+j)->Last);
printf("user name[%d] is: %s\n", j,(puser+j)->UserName);
printf("password[%d] is: %s\n", j,(puser+j)->Pwd);
}
printf("Goodbye\n");
}
int main(int argc, char *argv[])
{
add_user(puser);
return 0;
}

Related

Store list of courses input by user to an array in c

The program currently only asks the user to input courses, I want the list of courses the user entered to be stored in struct Students{ char courses[NUM_COURSES][100];. The list of courses the user entered should be able to be printed by using
printf("Courses %s\n",temp->courses);
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#define NUM_COURSES 5
int i = 0;
struct Students
{
char first_name[100];
char last_name[100];
int age;
char address[100];
char programme[100];
char courses[NUM_COURSES][100];
int num_selected_courses;
struct Students *next;
} * head;
void enter_student(char *first_name, char *last_name, int age, char *address, char *programme, char *courses, int num_selected_courses)
{
struct Students *student = (struct Students *)malloc(sizeof(struct Students));
strcpy(student->first_name, first_name);
strcpy(student->last_name, last_name);
student->age = age;
strcpy(student->address, address);
strcpy(student->programme, programme);
strcpy(student->courses[i], courses);
student->num_selected_courses = num_selected_courses;
student->next = NULL;
if (head == NULL)
{
// if head is NULL
// set student as the new head
head = student;
}
else
{
// if list is not empty
// insert student in beginning of head
student->next = head;
head = student;
}
}
void search_fname(char *first_name, char *last_name, int age, char *address, char *programme, char *courses, int num_selected_courses)
{
struct Students *temp = head;
char a[50];
int j;
printf("Enter First Name of Student to Search:\n");
scanf("%s", &a);
while (temp != NULL)
{
for (int j = 1; j <= i; j++)
{
if (!strcmp(first_name, a))
{
printf("The Students Details are:\n");
printf("First Name: %s\n", temp->first_name);
printf("Last Name: %s\n", temp->last_name);
printf("Age:%d\n", temp->age);
printf("Address: %s\n", temp->address);
printf("Programme: %s\n", temp->programme);
printf("Courses %s\n", temp->courses); /// list of courses for student name entered prints here
return;
}
}
temp = temp->next;
}
printf("Student name %s not found!!!\n\n\n", a);
}
int main()
{
struct Students stud;
head = NULL;
char first_name[100];
char last_name[100];
int age;
char address[100];
char programme[100];
int selected_courses[NUM_COURSES];
int num_selected_courses = 0;
int c;
const char *const list_courses[NUM_COURSES] = {
"CSE1100",
"CSE1101",
"CSE1102",
"ITE1100",
"ITE1101"};
do
{
int input_valid = 0;
int selected_course;
int d;
while (!input_valid)
{
char input[100];
// prompt user for input
printf(
"Courses available:\n"
" CSE1100\n"
" CSE1101\n"
" CSE1102\n"
" ITE1100\n"
" ITE1101\n"
"\n"
"Enter Course: ");
// attempt to read one word of user input
if (scanf("%99s", input) != 1)
{
fprintf(stderr, "unexpected input failure!\n");
exit(EXIT_FAILURE);
}
// discard remainder of input line
do
{
d = getchar();
} while (d != '\n' && d != EOF);
// determine whether course entered was valid or not
for (int i = 0; i < NUM_COURSES; i++)
{
if (strcmp(input, list_courses[i]) == 0)
{
input_valid = 1;
selected_course = i;
break;
}
}
// if invalid, print error message
if (!input_valid)
printf("Invalid choice! Try again.\n\n");
}
// input was valid, so add course
selected_courses[num_selected_courses] = selected_course;
num_selected_courses++;
// if we have already reached the maximum number of
// courses, then don't ask again
// ask user whether he wants to add another course
printf("Would you like to enter another course? (y or n)\n");
c = d = getchar();
// add spacing
printf("\n");
// discard remainder of input line
while (d != '\n' && d != EOF)
d = getchar();
} while (c == 'y');
for (int i = 0; i < num_selected_courses; i++)
{
// COPYING A SELECTED COURSE INTO STUDENT STRUCT
strcpy(stud.courses[i], list_courses[selected_courses[i]]);
}
// POPULATE NUMBER OF SELECTED COURSES IN STUDENT STRUCT
stud.num_selected_courses = num_selected_courses;
printf("You have selected the following courses:\n");
// PRINTING ALL COURSES IN THE STUDENT STRUCT
for (int i = 0; i < stud.num_selected_courses; i++)
// PRINTING A COURSE IN THE STUDENT STRUCT
printf("%s\n", stud.courses[i]);
}
If you want to be able to use the line
printf("Courses %s\n",temp->courses);
in order to print the courses member of struct Students, then you must make the courses member a null-terminated string. However, you are currently declaring that member as an array of strings.
Therefore, in order to do what you say you want, you would have to change the declaration
char courses[NUM_COURSES][100];
in struct Students to something like:
char courses[100];
You probably want the content of the string to be something like:
"CSE1100, CSE1102, ITE1101"
I have shown you in my answer to your previous question how to build such a string.

Username & Password don't match even if I input correctly

I am trying to create a program to enter into a system with username & password. But after creating the account, while in signin when I entered my username & password (stored from SignUP function), it shows incorrect. Where did I made the mistake and how can I fix it? I have given my specific code related to this problem. TIA
struct information
{
char username[20];
char password[20];
int date, month, year;
char pnumber[20];
int age[20];
char fname[20];
char lname[20];
char fathname[20];
char mothname[20];
char address[50];
};
void signup()
{
char username[20];
char password[20];
int passwordlength, i, seek = 0;
char ch;
FILE *fp, *fu;
struct information u1;
struct information p1;
// Opening file to
// write data of a user
fp = fopen("username.txt", "ab");
system("cls");
printf("\n\n!!!!!CREATE YOUR ACCOUNT!!!!!");
printf("\n\nFIRST NAME...");
scanf("%19s", u1.fname);
printf("\nLAST NAME...");
scanf("%19s", u1.lname);
printf("\nFATHER's NAME...");
scanf("%19s", u1.fathname);
printf("\nMOTHER's NAME...");
scanf("%19s", u1.mothname);
printf("\nADDRESS..");
scanf("%19s", u1.address);
printf("\nDATE OF BIRTH...");
printf("\nDATE-");
scanf("%d", &u1.date);
printf("\nMONTH-");
scanf("%d", &u1.month);
printf("\nYEAR-");
scanf("%d", &u1.year);
printf("\nPHONE NUMBER...");
scanf("%19s", u1.pnumber);
printf("\nAGE...");
scanf("%d", &u1.age);
printf("\nUSERNAME.. ");
scanf("%19s", u1.username);
printf("\nPASSWORD..");
scanf("%19s", p1.password);
fwrite(&u1, sizeof(u1), 1, fp);
fclose(fp);
printf("\n\nACCOUNT CREATED SUCCESSFULLY.\n");
char option[10];
printf("\nPRESS ANY KEY THEN ENTER TO GO TO SIGN IN PAGE");
scanf("%s", option);
signin();
}
void signin()
{
system("cls");
char username[20];
char password[20];
int i, j, k;
char ch;
FILE *fp, *fu;
struct information u1;
struct information p1;
// Opening file of
// user data
fp = fopen("username.txt","rb");
if (fp == NULL)
{
printf("\nERROR IN OPENING FILE\n");
printf("FILE DOESN'T EXIST\nYOU HAVE TO CREATE AN ACCOUNT FIRST\n");
printf("PRESS ANY KEY & ENTER TO CREATE AN ACCOUNT\n");
char option[10];
scanf("%s", option);
signup();
}
gotoxy(35, 10);
printf("==== LOG IN ====");
// Take input
gotoxy(35, 12);
printf("ENTER USERNAME.. ");
scanf("%19s", username);
gotoxy(35, 14);
printf("ENTER PASSWORD.. ");
scanf("%19s", password);
// Checking if username & password
// exists in the file or not
while (fread(&u1, sizeof(u1), 1, fp))
{
if (strcmp(username, u1.username) == 0)
if (strcmp(password, u1.password) == 0)
{
mainmenu();
}
fclose(fp);
}
In the signup() function you are reading the password into the p1.password field, but then only writing the u1 struct to the file. Simply change scanf("%19s", p1.password); to scanf("%19s", u1.password);.

How to get access of file pointer from one function to another?

I want to store the data in location but whenever I run the code it give me message that the fp is undeclared. I want fp to be working in another function. How to do this?
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc(char *name, size_t namesize);
int list_view(char *name);
int main(){
FILE *fp;
fp = fopen("/home/Documents/file.txt","w"); /* this is fp */
int one='1', two='2';
char choice[MAX], name[15] = "";
do{
printf("%c. Create new account\n",one);
printf("Enter your choice: ");
fgets(choice, sizeof choice, stdin);
if (choice[0] == one)
{new_acc(name, sizeof name);}
}
while(two != choice[0]);
return 0;}
int new_acc(char *name, size_t namesize){
printf("Enter your name: ");
fgets(name, namesize, stdin);
fputs(name, fp);
fclose(fp);
return 0;}
int new_acc(char *name, size_t namesize)
Change to:
int new_acc(FILE * fp, char *name, size_t namesize)
Then when you call this function in main:
new_acc(fp, name, sizeof name);
You should check the return value of fopen:
fp = fopen("/home/Documents/file.txt","w");
if(!fp) {
// handle the error.
}
You should move fclose(fp) out of this function.
OT, you should change the type:
int one='1', two='2';
to char one='1', two='2'; because you compare these variables with char value.
Updated for the question in comment:
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc(FILE *fp, char *name, size_t namesize);
int main(){
FILE *fp;
fp = fopen("file.txt","w"); /* this is fp */
if(!fp) {
printf("cannot open the file");
return -1;
}
char one='1', two='2';
char choice[MAX], name[15] = "";
char all_accouts[100][15];
int i = 0;
do{
printf("%c. Create new account\n",one);
printf("Enter your choice: ");
fgets(choice, sizeof choice, stdin);
if (choice[0] == one)
{new_acc(fp, all_accouts[i], sizeof(all_accouts[i])); i++;}
}
while(two != choice[0]);
for(int j = 0; j < i; j++) {
printf("%s\n", all_accouts[j]);
}
fclose(fp);
return 0;
}
int new_acc(FILE *fp, char *name, size_t namesize) {
printf("Enter your name: ");
fgets(name, namesize, stdin);
fputs(name, fp);
return 0;
}
output of test:
1. Create new account
Enter your choice: 1
Enter your name: name1
1. Create new account
Enter your choice: 1
Enter your name: name2
1. Create new account
Enter your choice: 2
name1
name2
Change the following line
int new_acc(char *name, size_t namesize);
to
int new_acc(char *name, size_t namesize, FILE *fp);
As the file pointer will be handy - BTW - Perhaps chose a better name for the variable
Change
{new_acc(name, sizeof name);}
to
{new_acc(name, sizeof name, fp);}
As this will be required
Change
int new_acc(char *name, size_t namesize){
To
int new_acc(char *name, size_t namesize, FILE *fp){
to match above and fp is required in the function
Job done!

Opening and Reading files fails

I am currently trying to read a file and count the number of instances of a user-specified string in an input file and output them to another file,.
However when I try to open the file, fopen returns NULL. Here's what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char in[50];
char out[50];
char target[50];
getIns(in,out,target);
Search(in, out, target);
//printf("string entered: %s\n%s\n%s", in, out, target);
return 0;
}
int getIns(char *i, char *o, char *t)
{
printf("please enter name of input file you wish to search: \n -i ");
fgets(i, 50, stdin);
printf("please enter name of output file you wish to write to: \n -o ");
fgets(o,50,stdin);
printf("Please enter the string you wish to search for \n -c ");
fgets(t, 50, stdin);
return 1;
}
int Search(char *i, char *o, char *t)
{
char*p;
int c = 0;
int start;
char *data = NULL;
FILE*f;
f = fopen(i, "r");
if (f == NULL)
{
printf("file not found \n Quitting...");
exit(1);
}
while(!feof(f))
{
fgets(data, sizeof(data), f);
p = strstr(data,t);
while (p != NULL)
{
c++;
p = strstr(p , t);
}
}
if (c == 0)
{
printf("String not in file\n");
}
if (c > 0)
{
printf("word: %s found: %d times\n", t, c);
}
fclose(f);
return 1;
}
Edit:
I've made some changes to the code following the responses and now, my program crashes on reading the file. New code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Search(char *i, char *o, char *t)
{
char*p;
int c = 0;
int start;
char data[1024];
FILE*f;
f = fopen(i, "r");
if (f == NULL)
{
printf("file not found \n Quitting...");
exit(1);
}
while(fgets(data, sizeof(data), f))
{
fgets(data, sizeof(data), f);
p = strstr(data,t);
while (p != NULL)
c++; p = strstr(p+1 , t);
}
if (c == 0)
{
printf("String not in file\n");
}
if (c > 0)
{
printf("word: %s found: %d times\n", t, c);
}
fclose(f);
return 1;
}
int getIns(char *i, char *o, char *t)
{
printf("please enter name of input file you wish to search: \n -i ");
fgets(i, 50, stdin); i[strcspn(i, "\n")] = 0;
printf("please enter name of output file you wish to write to: \n -o ");
fgets(o, 50, stdin); o[strcspn(o, "\n")] = 0;
printf("Please enter the string you wish to search for \n -c ");
fgets(t, 50, stdin); t[strcspn(t, "\n")] = 0;
return 1;
}
int main(int argc, char **argv)
{
char in[50];
char out[50];
char target[50];
getIns(in,out,target);
Search(in, out, target);
return 0;
}
try this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getIns(char *i, char *o, char *t);
int Search(char *i, char *o, char *t);
int main(int argc, char **argv)
{
char in[50];
char out[50];
char target[50];
getIns(in,out,target);
Search(in, out, target);
//printf("string entered: %s\n%s\n%s", in, out, target);
return 0;
}
int getIns(char *i, char *o, char *t)
{
printf("please enter name of input file you wish to search: \n -i ");
fgets(i, 50, stdin);
i[strcspn(i, "\n")] = 0;
printf("please enter name of output file you wish to write to: \n -o ");
fgets(o,50,stdin);
o[strcspn(o, "\n")] = 0;
printf("Please enter the string you wish to search for \n -c ");
fgets(t, 50, stdin);
t[strcspn(t, "\n")] = 0;
return 1;
}
int Search(char *i, char *o, char *t)
{
char*p;
int c = 0;
int start;
char data[1024];
FILE*f;
f = fopen(i, "r");
if (f == NULL)
{
printf("file not found \n Quitting...");
exit(1);
}
while(fgets(data, sizeof(data), f)){
p = strstr(data,t);
while (p != NULL)
{
c++;
p = strstr(p+1 , t);
}
}
if (c == 0)
{
printf("String not in file\n");
}
if (c > 0)
{
printf("word: %s found: %d times\n", t, c);
}
fclose(f);
return 1;
}

Segmentation Fault when printing Struct Objects

I've been reading around and I assume that I get this error due to not allocating a pointer before using it within my program. Is this the case? How would I go about doing so if it is? (HOMEWORK)
The Struct is in a file data.h
typedef struct student
{
char *firstName;
char *lastName;
int GPA;
float tuitionFees;
int numClass;
}Student;
This is within a file called student.c
Student Student_create()
{
Student *myStudent = malloc(sizeof(Student));
assert(myStudent != NULL);
printf("Please enter a name: ");
scanf("%20s",myStudent->firstName);
printf("\nPlease enter a family name: ");
scanf("%20s",myStudent->lastName);
printf("\nEnter number of enrolled classes: ");
scanf("%d",&myStudent->numClass);
printf("\nEnter students GPA: ");
scanf("%d",&myStudent->GPA);
printf("\nEnter tuition Fee: ");
scanf("%f",&myStudent->tuitionFees);
return myStudent;
}
void Student_print(Student *who)
{
fflush(stdout);
printf("First Name: %s\n, ",who->firstName);
printf("Last Name: %s\n", who->lastName);
printf("\tTuition Fees: %f\n, ",who->tuitionFees);
printf("Number of Courses: %d\n , ",who->numClass);
printf("GPA : %d\n, ",who->GPA);
}
And the main file I'm trying to access it through is labelled col_personal.c
int main(int argc, char *argv[])
{
//This is for the input
int choice;
mainMenu();
return 0;
}
void mainMenu()
{
int choice = 0;
Student class[10];
int ssize = 0;
while(choice != 10){
printf("\n\n\n\t\t\t====School Database====\n");
printf(" 1. Add a new record\n");
printf(" 2. Print Students.\n");
scanf("%d",&choice);
//CHOICE ADD NEW RECORD.
if(choice == 1)
{
printf("1.Student\n");
fflush(stdin);
scanf("%d",&choice);
if(choice == 1){
printf("How many students would you like to input? ");
scanf("%d",&choice);
//Creates Students
for(int i=0; i<choice;i++){
class[i] = Student_create();
ssize = ssize + 1;
}
}
//PRINTING STUDENTS CHOICE 2
} else if (choice == 2){
for(int i=0; i < ssize;i++){
Student_print(class[i]);
}
segmentation falut is due to no memory allocation for the pointers
char *firstName;
char *lastName;
these are just pointers not pointing to any valid memory location.
use
pointer = malloc(<number of character to store> * sizeof(char));
Also free the allocated memory towards the end to avoid memory leak.

Resources