Remove element from struct type - c

I'm trying to create this function to remove an element of the user's choosing from a pointer struct array type.
Here is my function. I keep getting this error when my code hits this function.
error: incompatible types when assigning to type ‘char[1000]’ from type ‘char *’
PhoneBook[iRecord - 1].cFirstName = PhoneBook[x].cFirstName;
void delete_record(pb *PhoneBook)
{
int x;
int iRecord = 0;
print(PhoneBook);
printf("\nEnter number of record you want to delete: ");
scanf("%d", &iRecord);
printf("\nRecord to be deleted: %d. %s\n", iRecord - 1, PhoneBook[iRecord - 1].cFirstName);
for (x = strlen(PhoneBook[iRecord - 1].cFirstName); x <= strlen(PhoneBook[iRecord - 1].cFirstName); x--) {
PhoneBook[iRecord - 1].cFirstName = PhoneBook[x].cFirstName;
}
printf("\nModified record: %s\n\n",PhoneBook[iRecord - 1].cFirstName);
}
Here's the full code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct phonebook {
char cFirstName[1000];
char cLastName[1000];
char cNumber[1000];
} pb;
int entry(pb *);
void modify(pb *);
void delete_record(pb *);
void print(pb *);
void convert_u(char *);
//global variable
int MAX_NAME_ENTRY = 0;
int main()
{
int iResponse = 0;
pb *PhoneBook = (pb *) calloc(0, sizeof(pb));
if (PhoneBook == NULL) {
printf("\nMemory allocation failed.\n\n");
return 1;
}
do {
printf("\nPhonebook Menu\n****************\n\n");
printf("1. Enter new contact\n2. Modify existing contact\n3. Delete contact\n4. Print Phonebook \n5. Exit\n\n");
printf("Please make selection: ");
scanf("%d", &iResponse);
if (iResponse == 1) {
entry(PhoneBook);
}
else if (iResponse == 2) {
modify(PhoneBook);
}
else if (iResponse == 3) {
delete_record(PhoneBook);
//printf("\nWorking on it...\n");
}
else if (iResponse == 4) {
print(PhoneBook);
}
} while (iResponse != 5);
free(PhoneBook);
return 0;
}
int entry(pb *PhoneBook)
{
int x;
char yes_no[] = "YES";
pb *newPhoneBook = realloc(PhoneBook, (10 * sizeof(pb)));
if (newPhoneBook == NULL) {
printf("\nOut of memory!\n\n");
return 1;
}
else {
PhoneBook = newPhoneBook;
}
if (MAX_NAME_ENTRY == 10) {
printf("\nMax Number of names entered.\n");
}
for (x = MAX_NAME_ENTRY; x < 10; x++) {
if (MAX_NAME_ENTRY == 9) {
printf("\nLast entry.\n");
}
if (x > 0) {
system("clear");
printf("\nAnother entry(yes/no)? ");
scanf("%s", yes_no);
convert_u(yes_no);
}
if (strcmp(yes_no, "YES") == 0) {
printf("\nFirst Name: ");
scanf("%s", PhoneBook[x].cFirstName);
printf("\nLast Name: ");
scanf("%s", PhoneBook[x].cLastName);
printf("\nPhone Number: ");
scanf("%s", PhoneBook[x].cNumber);
MAX_NAME_ENTRY++;
}
else if (strcmp(yes_no, "NO") == 0) {
break;
}
}
}
void modify(pb *PhoneBook)
{
int iModify = 0;
char name_num[6] = {'\0'};
print(PhoneBook);
printf("\nWhich entry would you like to modify? ");
scanf("%d", &iModify);
printf("\nModify name or number? ");
scanf("%s", name_num);
convert_u(name_num);
if (strcmp(name_num, "NAME") == 0) {
printf("\nEnter new name: ");
scanf("%s %s", PhoneBook[iModify - 1].cFirstName, PhoneBook[iModify - 1].cLastName);
}
else if (strcmp(name_num, "NUMBER") == 0) {
printf("\nEnter new number: ");
scanf("%s", PhoneBook[iModify - 1].cNumber);
}
}
void delete_record(pb *PhoneBook)
{
int x;
int iRecord = 0;
print(PhoneBook);
printf("\nEnter number of record you want to delete: ");
scanf("%d", &iRecord);
printf("\nRecord to be deleted: %d. %s\n", iRecord - 1, PhoneBook[iRecord - 1].cFirstName);
for (x = strlen(PhoneBook[iRecord - 1].cFirstName); x <= strlen(PhoneBook[iRecord - 1].cFirstName); x--) {
PhoneBook[iRecord - 1].cFirstName = PhoneBook[x].cFirstName;
}
printf("\nModified record: %s\n\n",PhoneBook[iRecord - 1].cFirstName);
}
void print(pb *PhoneBook)
{
int x;
for (x = 0; x < 10; x++) {
if (strlen(PhoneBook[x].cFirstName) == 0 && strlen(PhoneBook[x].cLastName) == 0) {
break;
}
printf("\n%d. Name: %s %s", x + 1, PhoneBook[x].cFirstName, PhoneBook[x].cLastName);
printf("\n Number: %s\n\n", PhoneBook[x].cNumber);
}
}
void convert_u(char *string)
{
int x;
for (x = 0; x < strlen(string); x++) {
string[x] = toupper(string[x]);
}
}
Any suggestions on what I'm doing wrong?

An array name is not a modifiable lvalue. Hence, arrays are not assignable in C.
Quoting C11, chapter §6.5.16
An assignment operator shall have a modifiable lvalue as its left operand.
and regarding the modifiable lvalue, chapter §6.3.2.1
A modifiable lvalue is an lvalue that
does not have array type, [...]
You need to use strcpy() instead to copy the content.

it will not work strcpy, you have to copy the whole record over the other. then realloc the phonebook to new size.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct phonebook {
char cFirstName[50];
char cLastName[50];
char cNumber[50];
} pb;
int entry();
void modify();
void delete_record();
void print_record(int rec);
void print();
void convert_u(char *);
//global variables
int MAX_NAME_ENTRY = 0;
pb *PhoneBook = NULL;
#define YES "YES"
#define NO "NO"
const char *menu=
" Phonebook Menu \n"
" **************** \n"
" \n"
" 1. Enter new contact \n"
" 2. Modify existing contact \n"
" 3. Delete contact \n"
" 4. Print Phonebook \n"
" 5. Exit \n"
" \n"
" Please make selection: ";
int main(void){
int iResponse = 0;
do {
printf("%s",menu);
scanf("%d", &iResponse);
switch (iResponse) {
case 1:
entry();
break;
case 2:
modify();
break;
case 3:
delete_record();
break;
case 4:
print();
}
} while (iResponse != 5);
free(PhoneBook);
return 0;
}
int str_input(const char *text, char *buff){
printf("%s" , text);
scanf("%s" , buff);
}
int entry(void)
{
int x;
char yes_no[] = YES;
pb *newPhoneBook;
if (MAX_NAME_ENTRY == 10) {
printf("\nMax Number of names entered.\n");
return 0;
}
while(1) {
newPhoneBook = realloc(PhoneBook,(MAX_NAME_ENTRY +1) * sizeof(pb)) ;
if (!newPhoneBook) {
perror("entry::realloc()");
return 1;
}
PhoneBook = newPhoneBook;
if (MAX_NAME_ENTRY == 9) {
printf("\n *** This is the Last entry. *** \n");
}
printf("RECORD: %d\n",MAX_NAME_ENTRY+1);
str_input("\nFirst Name: " , PhoneBook[MAX_NAME_ENTRY].cFirstName);
str_input("\nLast Name: " , PhoneBook[MAX_NAME_ENTRY].cLastName);
str_input("\nPhone Number: " , PhoneBook[MAX_NAME_ENTRY].cNumber);
MAX_NAME_ENTRY++;
if(MAX_NAME_ENTRY == 10)
break;
printf("\nAnother entry(yes/no)? ");
scanf("%s", yes_no);
convert_u(yes_no);
if (strcmp(yes_no, YES))
break;
system("clear");
}
}
int checkRecordExists(int iRecord){
if(iRecord<1 || iRecord>MAX_NAME_ENTRY){
printf("Record %d do not exists\n",iRecord);
return 0;
}
return 1;
}
void modify(void){
int iModify = 0;
char name_num[6] = {'\0'};
printf("\nWhich entry would you like to modify? ");
scanf("%d", &iModify);
if(!checkRecordExists(iModify))
return;
iModify --;
print_record(iModify);
str_input("\nModify name or number? ", name_num);
convert_u(name_num);
if (strcmp(name_num, "NAME") == 0) {
str_input("\nFirst Name: " , PhoneBook[iModify].cFirstName);
str_input("\nLast Name: " , PhoneBook[iModify].cLastName);
}
else if (strcmp(name_num, "NUMBER") == 0) {
str_input("\nPhone Number: " , PhoneBook[iModify].cNumber);;
}
}
void delete_record(void)
{
int x;
int iRecord = 0;
printf("\nEnter number of record you want to delete: ");
scanf("%d", &iRecord);
if(!checkRecordExists(iRecord))
return;
iRecord--;
MAX_NAME_ENTRY--;
for( x=iRecord;x<MAX_NAME_ENTRY;x++)
memcpy(&PhoneBook[x],&PhoneBook[x+1],sizeof(pb));
PhoneBook = realloc(PhoneBook,(MAX_NAME_ENTRY + 1) * sizeof(*PhoneBook) );
}
void print_record(int rec){
printf("\n"
"%02d. Name: %s %s\n"
" Number: %s\n"
"\n",
rec + 1, PhoneBook[rec].cFirstName, PhoneBook[rec].cLastName, PhoneBook[rec].cNumber);
}
}
void print(void){
int x;
for (x = 0 ; x < MAX_NAME_ENTRY ; x++) {
print_record(x);
}
}
void convert_u(char *string){
while(*string){
*string = toupper(*string);
string ++;
}
}

Related

Get first letter from C string

I have a program in C which is basically a contact book, and I've already done all the functionalities (add contact, delete etc) but I also have to implement a way to search for contacts by the initial letter (the user type any letter, and if they exist contacts that start with that letter they should be displayed) but I'm not getting the first letter of the vector of names to do this... My attempt to do this is in the SearchContactsByFirstLetter function...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <time.h>
#define MAX_LENGTH 50
typedef struct
{
char name[MAX_LENGTH];
char number[MAX_LENGTH];
int bd;
int bdm;
} ContactBook;
void ListContacts(ContactBook **c, int quant)
{
int i;
printf("\n List of contacts: \n");
printf("\t---------------------\n");
for (i = 0; i < quant; i++)
{
printf("\t%d = birthday: %2d month %2d\t name: %s \t number: %s\n", i + 1, c[i]->bd, c[i]->bdm, c[i]->name, c[i]->number);
}
}
int addContacts(ContactBook **c, int quant, int size)
{
if (quant < size)
{
ContactBook *new = malloc(sizeof(ContactBook));
printf("\nenter contact name: ");
scanf("%49[^\n]", new->name);
printf("\nenter number: ");
scanf("%s", new->number);
printf("\nenter the birthday ");
scanf("%d", &new->bd);
printf("\n enter the month birthday: ");
scanf("%d", &new->bdm);
c[quant] = new;
return 1;
}
else
{
printf("\n full list.\n");
return 0;
}
}
int deleteContact(ContactBook **c, int quant)
{
int id;
ListContacts(c, quant);
printf("\n\t Enter the id you want to delete: \n");
scanf("%d", &id);
id--;
if (id >= 0 && id < quant)
{
free(c[id]);
if (id < quant - 1)
{
c[id] = c[quant - 1];
}
return -1;
}
else
{
printf("\n\t wrong code;\n");
return 0;
}
}
void birthdays(ContactBook **c, int quant)
{
int i;
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf(" os aniversariantes do mês são: \n");
for (i = 0; i < quant; i++)
{
if (tm.tm_mon + 1 == c[i]->bdm)
{
printf("\t%d = birthday: %2d month %2d\t name: %s \t number: %s\n", i + 1, c[i]->bd, c[i]->bdm, c[i]->name, c[i]->number);
}
}
}
void SearchContactByFirstLetter(ContactBook **c, int quant)
{
int i;
char searchedName[2];
printf("\n Search a letter: \n");
scanf("%s", searchedName);
getchar();
for (i = 0; i < quant; i++)
{
if (strcmp(searchedName, c[i]->name[0]) == 0)
{
printf("\t\nname: %s, \nnumber: %s, \nbirthday: %d \nmonth birthday %d \t\n", c[i]->name, c[i]->number, c[i]->bd, c[i]->bdm);
}
}
}
void SearchContact(ContactBook **c, int quant)
{
int i;
char searchedName[30];
printf("\n Search name: \n");
scanf("%s", searchedName);
getchar();
for (i = 0; i < quant; i++)
{
if (strcmp(searchedName, c[i]->name) == 0)
{
printf("\t\nname: %s, \nnumber: %s, \nbirthday: %d \nmonth birthday %d \t\n", c[i]->name, c[i]->number, c[i]->bd, c[i]->bdm);
}
}
}
void saveBinary(char arquivo[], ContactBook **c, int quant)
{
FILE *file = fopen(arquivo, "wb");
int i;
if (file)
{
for (i = 0; i < quant; i++)
fwrite(c[i], sizeof(ContactBook), 1, file);
fclose(file);
}
else
printf("erro");
}
int readBinaryArq(char arquivo[], ContactBook **c)
{
int quant = 0;
ContactBook *new = malloc(sizeof(ContactBook));
FILE *file = fopen(arquivo, "rb");
if (file)
{
while (fread(new, sizeof(ContactBook), 1, file))
{
c[quant] = new;
quant++;
new = malloc(sizeof(ContactBook));
}
fclose(file);
}
else
printf("\nerro");
return quant;
}
int main()
{
ContactBook *contacts[50];
int option, size = 50, quant = 0;
char arq2[] = ("agenda.dat");
quant = readBinaryArq(arq2, contacts);
do
{
printf(" \n\t0 - exit\n\t1 - register contact\n\t2 - Remove contact\n\t3- List contacts\n\t4- Search contact\n\t5 - ver aniversariantes do mês\n\t6-Pesquisar por inicial\n ");
scanf("%d", &option);
getchar();
switch (option)
{
case 1:
quant += addContacts(contacts, quant, size);
break;
case 2:
quant += deleteContact(contacts, quant);
break;
case 3:
ListContacts(contacts, quant);
break;
case 4:
SearchContact(contacts, quant);
break;
case 5:
birthdays(contacts, quant);
break;
}
saveBinary(arq2, contacts, quant);
} while (option != 0);
return 0;
}
if (strcmp(searchedName, c[i]->name[0]) == 0)
That should be:
if (searchedName[0] == c[i]->name[0])
But you probably shouldn't read in a string in the first place if you only want to read a single character.

Search function in CRUD with C

I'm making a contact book in C and I've already done the parts of registering contact, listing all contacts and deleting a contact, but I need to elaborate the part of searching the contact, and also searching contacts that start with a given string, but I'm not succeeding... I'll leave my code below to see if anyone can help me identify where I'm going wrong:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 50
typedef struct
{
char name[MAX_LENGTH];
char number[MAX_LENGTH];
int bd;
char bdm[MAX_LENGTH];
} ContactBook;
void ListContacts(ContactBook **c, int quant)
{
int i;
printf("\n List of contacts: \n");
for (i = 0; i < quant; i++)
{
printf("\t%d = birthday: %2d month %s\t name: %s \t number: %s\n", i + 1, c[i]->bd, c[i]->bdm, c[i]->name, c[i]->number);
}
}
int addContacts(ContactBook **c, int quant, int size)
{
if (quant < size)
{
ContactBook *new = malloc(sizeof(ContactBook));
printf("\nenter contact name: ");
scanf("%s", new->name);
printf("\nenter number: ");
scanf("%s", new->number);
printf("\nenter the birthday ");
scanf("%d", &new->bd);
printf("\n enter the month birthday: ");
scanf("%s", new->bdm);
c[quant] = new;
return 1;
}
else
{
printf("\n full list.\n");
return 0;
}
}
int deleteContact(ContactBook **c, int quant)
{
int id;
ListContacts(c, quant);
printf("\n\t Enter the id you want to delete: \n");
scanf("%d", &id);
getchar();
id--;
if (id >= 0 && id < quant)
{
free(c[id]);
if (id < quant - 1)
{
c[id] = c[quant - 1];
}
return -1;
}
else
{
printf("\n\t wrong code;\n");
return 0;
}
}
void SearchContact(ContactBook **c, int quant)
{
int i;
char searchedName[30];
for (i = 0; i < quant; i++)
{
printf("\n Search name: \n");
scanf("%s", searchedName);
getchar();
if (strcmp(searchedName, c[i]->name) == 0)
{
printf("the name: %s was found", c[i]->name);
}
else
{
printf("name not found");
}
}
}
int main()
{
ContactBook *contacts[50];
int option, size = 50, quant = 0;
do
{
printf(" \n\t0 - exit\n\t1 - register contact\n\t2 - Remove contact\n\t3- List contacts\n\t4- Search contact\n\t");
scanf("%d", &option);
getchar();
switch (option)
{
case 1:
quant += addContacts(contacts, quant, size);
break;
case 2:
quant += deleteContact(contacts, quant);
break;
case 3:
ListContacts(contacts, quant);
break;
case 4:
SearchContact(contacts, quant);
break;
}
} while (option != 0);
return 0;
}

assignment to expression with array type error in c

I am getting this error as shown in the image pls tell me how to fix it.
the error is in line 115 of the code
#include<stdio.h>
#include<string.h>
#define MAX 100
#define product_limit 50
typedef struct {
int p_id;
char p_name[product_limit + 1];
int p_quantity;
int unit_price;
char type_product[50];
} product;
int main()
{
int ok = 1, menu;
do
{
menu = display_menu();
if(menu < 1 || menu > 7)
{
printf("Invaid option.\n");
}
else
{
switch(menu)
{
case 1:
add_product();
break;
case 2:
change_product();
break;
case 3:
display_all_products();
break;
}
}
}
while(ok);
printf("Exiting Program.\n");
return 0;
}
int display_menu()
{
int menu;
printf("Choose option from below.\n");
printf("1. Add new product.\n");
printf("2. Update product.\n");
printf("3. Display products.\n");
printf(". Exit.\n");
scanf("%d", &menu);
return menu;
}
product products[MAX];
int c_p = 0;
int product_exists(int id)
{
int x;
for(x=0;x<c_p;x++)
{
if(products[x].p_id == id)
{
return x;
}
}
return -1;
}
int add_product()
{
int price,id,quantity,type;
char name[product_limit + 1];
printf("Product ID: ");
scanf("%d", &id);
if(product_exists(id) != -1)
{
printf("ID: %d already exists.\n", id);
return;
}
printf("Enter Product Name: ");
scanf("%s", name);
printf("Enter Quantity: ");
scanf("%d", &quantity);
printf("Enter Price: ");
scanf("%d", &price);
printf("Enter Product Type:");
scanf("%s",type);
products[c_p].p_id = id;
strcpy(products[c_p].p_name, name);
products[c_p].p_quantity = quantity;
products[c_p].unit_price = price;
c_p++;
printf("Product added Successfully\n");
}
int change_product()
{
int id, exists,name;
char z[2];
printf("Product ID: ");
scanf("%d", &id);
exists = change_product(id);
if(exists == -1) {
printf("ID: %d not exists.\n", id);
printf("Type Y to try once more or N back to menu: ");
scanf("%s", z);
if(strcmp(z, "Y") == 0)
{
change_product();
}
} else {
printf("Product found successfully\n");
printf("Product Name: %s\n", products[exists].p_name);
printf("Type new name: ");
scanf("%d", name);
products[exists].p_name += name;
printf("Successfully updated.\n");
}
}
int display_products()
{
int x;
if(c_p == 0)
{
printf("No products were added\n");
return;
}
printf("Products\n\n");
for(x = 0; x < c_p; x++)
{
printf("Product ID: %d\n", products[x].p_id);
printf("Product Name: %s\n", products[x].p_name);
printf("Quantity: %d\n", products[x].p_quantity);
printf("Product price: %d\n", products[x].unit_price);
printf("Product type:%s\n",products[x].p_name);
printf("\n");
}
}
You're trying to add an integer to a string. This is not allowed in C:
products[exists].p_name += name;
Looking at the change_product method it seems like maybe you want to update the name field or concatenate it. I think you should do these things:
Read all compiler warnings and consider addressing them, They are addressing valid concerns like not declaring your functions first, not returning any int value in add_product, display_product etc which is supposed to return int. Provide a function body for display_all_products (Did you mean display_products()?)
Use a string variable for updating name char newName[product_limit + 1] and use library functions from C string library to either concatenate(strcat) or copy(strcpy) newName taken as user input to products[exists].p_name:
char newName[product_limit + 1];
// ...
} else {
printf("Type new name: ");
fgets(newName, product_limit + 1, stdin);
strcpy(products[exists].p_name, newName);
}
}
You can't add integer to string.
You have used string functions like strcpy() and should do the same for this.
You have also made other errors :
declaring int for string(char array)
wrong return data type
calling wrong funtion
I have fixed those errors, do side by side comparison.
#include<stdio.h>
#include<stdlib.h>//here
#include<string.h>
#define MAX 100
#define product_limit 50
//here
void add_product(void);
void change_product(void);
int display_menu(void);
void display_products(void);
int product_exists(int);
typedef struct {
int p_id;
char p_name[product_limit + 1];
int p_quantity;
int unit_price;
char type_product[50];
} product;
int main()
{
int ok = 1, menu;
do
{
menu = display_menu();
if(menu < 1 || menu > 4)
{
printf("Invaid option.\n");
}
else
{
switch(menu)
{
case 1:
add_product();
break;
case 2:
change_product();
break;
case 3:
display_products();//here
break;
case 4:
exit(0);//here
}
}
}
while(ok);
printf("Exiting Program.\n");
return 0;
}
int display_menu()
{
int menu;
printf("Choose option from below.\n");
printf("1. Add new product.\n");
printf("2. Update product.\n");
printf("3. Display products.\n");
printf("4. Exit.\n");
scanf("%d", &menu);
return menu;
}
product products[MAX];
int c_p = 0;
int product_exists(int id)
{
int x;
for(x=0;x<c_p;x++)
{
if(products[x].p_id == id)
{
return x;
}
}
return -1;
}
void add_product() // here
{
int price,id,quantity;
char type[product_limit + 1];
char name[product_limit + 1];//here
printf("Product ID: ");
scanf("%d", &id);
if(product_exists(id) != -1)
{
printf("ID: %d already exists.\n", id);
return;
}
printf("Enter Product Name: ");
scanf("%s", name);
printf("Enter Quantity: ");
scanf("%d", &quantity);
printf("Enter Price: ");
scanf("%d", &price);
printf("Enter Product Type:");
scanf("%s",type);
products[c_p].p_id = id;
strcpy(products[c_p].p_name, name);
products[c_p].p_quantity = quantity;
products[c_p].unit_price = price;
strcpy(products[c_p].type_product,type);//here
c_p++;
printf("Product added Successfully\n");
}
void change_product() //here
{
int id, exists;
char z[2];
char name[product_limit + 1];//here
printf("Product ID: ");
scanf("%d", &id);
exists = product_exists(id);//here
if(exists == -1)
{
printf("ID: %d not exists.\n", id);
printf("Type Y to try once more or N back to menu: ");
scanf("%s", z);
if(strcmp(z, "Y") == 0)
{
change_product();
}
}
else
{
printf("Product found successfully\n");
printf("Product Name: %s\n", products[exists].p_name);
printf("Type new name: ");
scanf("%s", &name);//here
strcpy(products[exists].p_name, name);//here
printf("Successfully updated.\n");
}
}
void display_products() //here
{
int x;
if(c_p == 0)
{
printf("No products were added\n");
return;
}
printf("Products\n\n");
for(x = 0; x < c_p; x++)
{
printf("Product ID: %d\n", products[x].p_id);
printf("Product Name: %s\n", products[x].p_name);
printf("Quantity: %d\n", products[x].p_quantity);
printf("Product price: %d\n", products[x].unit_price);
printf("Product type:%s\n",products[x].type_product);//here
printf("\n");
}
}

I have got a problem with reading binary file into array of structures in c

Here is part of my code:
Here I want to transfer already saved .bin file into a new database structure student s, but it is not transferring more than one member of a structure.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20];
int date;
int month;
int year;
int id;
int pnum;
}student;
int count = 0;
void swap(student* s1, student* s2) {
student* temp;
temp = s1;
s1 = s2;
s2 = temp;
}
void sort(student* s) {
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
if (strcmp((s + i)->name, (s + j)->name) > 0) {
student temp = *(s + i);
*(s + i) = *(s + j);
*(s + j) = temp;
}
}
}
}
void addInf(student* s) {
printf("\t==================================Adding information=====================================\n\n");
printf("\t\tPlease input following information: \n");
printf("\tName: ");
scanf("%s", (s + count)->name);
printf("\tDate of birth (yyyymmdd): ");
scanf("%4d%2d%2d", &(s + count)->year, &(s + count)->month, &(s + count)->date);
printf("\tStudent ID: ");
scanf("%d", &(s + count)->id);
printf("\tPhone number: ");
scanf("%d", &(s + count)->pnum);
count++;
printf("\tEntry succeded.\n");
printf("\t=========================================================================================\n\n");
}
void print(student* s) {
printf("\t\tName: %s\n", s->name);
printf("\t\tBidthday: %d/%02d/%02d\n", s->year, s->month, s->date);
printf("\t\tID: %d\n", s->id);
printf("\t\tPhone number: %d\n", s->pnum);
}
void delInf(student* s, int n) {
int com;
printf("\t=================================Deleting information====================================\n\n");
printf("\t=========================================================================================\n\n");
char name[20];
printf("\t\tPlease input name of the student that you want to delete: ");
scanf("%s", &name);
for (int i = 0; i < count; i++) {
if ((strcmp(name, (s + i)->name)) == 0) {
printf("\t\tInformation that you want to delete\n");
print((s + i));
for (int j = i; j < count; j++) {
*(s + i) = *(s + i + 1);
count--;
printf("\t\tInformation was succesfully deleted.\n\n");
}
}
}
}
void searchByID(student* s) {
int key;
printf("\t======================================Searching by ID====================================\n\n");
printf("\t\tEnter ID: ");
scanf("%d", &key);
printf("\t=========================================================================================\n\n");
int i;
for (i = 0; i < count; i++) {
if (key == (s + i)->id) {
break;
}
}
print((s + i));
}
void searchByName(student* s) {
char key[20];
int i;
int size, check = 0;
printf("\t======================================Searching by Name==================================\n\n");
printf("\t\tEnter Name: ");
scanf("%s", &key);
printf("\t=========================================================================================\n\n");
for (i = 0; i < count; i++) {
if (strcmp(key, (s + i)->name) == 0) {
print((s + i));
}
}
}
void searchByBirthDate(student* s) {
int key, command;
int i;
printf("\t================================Searching by Birthdate================================\n\n");
printf("\t\t1.By Date\t\t 2.By Month\t\t 3.By Year\t\t 4.By All\n");
printf("\t\tCommand: ");
scanf("%d", &command);
printf("\t=========================================================================================\n\n");
if (command == 1) {
printf("Enter date: ");
scanf("%2d", &key);
printf("\t\tStudent with same date\n");
for (i = 0; i < count; i++) {
if (key == (s + i)->date) {
printf("\t\t---------%d----------\n\n", i + 1);
print((s + i));
}
}
}
if (command == 2) {
printf("\t\tEnter month: ");
scanf("%2d", &key);
printf("\t\tStudent with same month\n");
for (i = 0; i < count; i++) {
if (key == (s + i)->month) {
printf("\t\t---------%d----------\n\n", i + 1);
print((s + i));
}
}
}
if (command == 3) {
printf("\t\tEnter year: ");
scanf("%4d", &key);
printf("\t\tStudent with same year\n");
for (i = 0; i < count; i++) {
if (key == (s + i)->year) {
printf("\t\t---------%d----------\n\n", i + 1);
print(s + i);
}
}
}
if (command == 4) {
int yy, mm, dd;
printf("\t\tEnter birthdate: ");
scanf("%4d%2d%2d", &yy, &mm, &dd);
for (i = 0; i < count; i++) {
if (yy == (s + i)->year && mm == (s + i)->month && dd == (s + i)->date) {
break;
}
}
print(s + i);
}
}
void printTable(student* s) {
printf("\t============================================Table========================================\n\n");
printf("\t\tName\t\t\tBirthday\t\t\tStudent ID\t\t\tPhone number\n\n");
for (int i = 0; i < count; i++) {
printf("\t%d. %s\t\t\t\t%d/%d/%d\t\t\t%d\t\t\t0%d\n\n", i + 1, (s + i)->name, (s + i)->year, (s + i)->month, (s + i)->date, (s + i)->id, (s + i)->pnum);
}
printf("\t=========================================================================================\n\n");
}
void search(student* s) {
printf("\t=========================================Search==========================================\n\n");
printf("\t\tAvailable commands: \n");
printf("\t\t1. Search by name\t\t\t2. Search by ID\n\t\t3. Search by birthday\n");
printf("\t=========================================================================================\n\n");
printf("\t\tPlease choose command: ");
int com;
scanf("%d", &com);
switch (com) {
case 1: searchByName(s);
break;
case 2: searchByID(s);
break;
case 3: searchByBirthDate(s);
break;
}
}
void menu() {
printf("\n\t======================================MENU===============================================\n");
printf("\t\tAvailable commands: \n");
printf("\t\t1. Add Student\t\t\t2.Delete student\n\t\t3. Find student\t\t\t4. Table of all students\n");
printf("\t\t5. Transfer information from binary file\n\t\t6. Save information into binary file.\n\t\t0. Exit\n\n");
printf("\t=========================================================================================\n\n");
printf("\t\tPlease choose command: ");
}
int main() {
FILE* fp;
FILE* fpr;
printf("\t\tEnter a name of binary file that you want to create: ");
char filename[20];
scanf("%s", &filename);
strcat(filename, ".bin");
fp = fopen(filename, "ab");
if (fp == NULL) {
printf("\t\tUnable to open the file.\nError\n");
exit(1);
}
else printf("\t\t\tFile %s successfuly created\n\n", filename);
int iCount;
int n, c;
student* s;
printf("\t\tPlease enter number of students: ");
scanf("%d", &n);
printf("\n\n");
s = (student*)calloc(n, sizeof(student));
int quit = 1;
while (quit) {
menu();
scanf("%d", &c);
printf("\n");
switch (c) {
case 0:
quit = 0;
break;
case 1:
addInf(&s);
break;
case 2:
delInf(&s, n);
break;
case 3:
search(&s);
break;
case 4:
if (count == 0) printf("\t\tThere is no any given information yet.\n\n");
else printTable(&s);
break;
case 5:
printf("\t\tEnter a name or path of file that you want to open: ");
char readfilename[30];
scanf("%s", &readfilename);
strcat(readfilename, ".bin");
fpr = fopen(readfilename, "rb+");
if (fpr == NULL) {
printf("\t\tUnable to open the file.\nError\n");
break;
}
else printf("\t\tFile %s successfuly opened for reading\n", readfilename);
printf("\t\t\ttransfering binary data from %s into database\n", readfilename);
while ((fread(&s, sizeof(student), 1, fpr)) == 1) {
count++;
}
printf("\t\t%d student information was successfuly transferred\n", count);
break;
case 6:
iCount = fwrite(&s, sizeof(student), count, fp);
if (iCount != count) printf("Information could be missed in %s file\n", filename);
else printf("\t\tAll information was successfuly copied into %s file", filename);
}
sort(&s);
}
fclose(fp);
return 0;
}
First of all, enable warnings when compiling, and follow up on them!
This should tell you that &s is wrong in every place where you used it.
This causes your program to suffer from undefined behavior, which is a total pest when trying to debug your program!
Now then, on to the code that reads students from file:
while ((fread(s, sizeof(student), 1, fpr)) == 1) {
count++;
}
Every student is read into the same memory area (pointed to by s). They overwrite each other, leaving only the last one read. Try s + count instead of s:
while ((fread(s + count, sizeof(student), 1, fpr)) == 1) {
count++;
}

invalid argument read write file

typical beginner's phonebook program, attempting to add read and write to file capabilities. It's not compiling just fine but when I execute either functions 7 or 8, my errorhandler returns "invalid argument"
EDIT* updated code for the whole thing, including several fixes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct phonebook
{
char cFirstName[20];
char cLastName[20];
char PhoNo[20];
} pb;
//function prototypes
void AddContact (pb * );
void DeleteContact (pb * );
void ShowContacts (pb * );
void FindContact (pb * );
void RandContact (pb * );
void FindContact (pb * );
void DeleteAll (pb *);
void Read (pb *);
void Write (pb *);
char FileName[100];
FILE *pRead;
FILE *pWrite;
int counter = 0;
main ()
{
pb *phonebook;
phonebook = (pb*) malloc(sizeof(pb)*1);
int iChoice = 0;
while (iChoice <= 8)
{
printf("\n-Choose an option- \n");
printf("\n\t(1)\tAdd Contact");
printf("\n\t(2)\tDelete Contact");
printf("\n\t(3)\tShow All Contacts");
printf("\n\t(4)\tSearch for a Contact");
printf("\n\t(5)\tRandom Contact");
printf("\n\t(6)\tDelete All Contacts");
printf("\n\n\t(7)\tWrite contacts to file");
printf("\n\t(8)\tRead contacts from file");
printf("\n\n\t(9)\tExit\n\n\t");
scanf("%d", &iChoice);
if (iChoice == 1)
{
AddContact(phonebook);
}
if (iChoice == 2)
{
DeleteContact (phonebook);
}
if (iChoice == 3)
{
ShowContacts(phonebook);
}
if (iChoice == 4)
{
FindContact(phonebook);
}
if (iChoice == 5)
{
RandContact(phonebook);
}
if (iChoice == 6)
{
DeleteAll(phonebook);
}
if (iChoice == 7)
{
Write(phonebook);
}
if (iChoice == 8)
{
Read(phonebook);
}
if (iChoice == 9)
{
free(phonebook);
return 0;
}
} //end while
} //end main
//function definitions
//add contact
void AddContact (pb * phonebook)
{
counter++; //counter incremented for each entry
realloc(phonebook, sizeof(pb)); //realloc with every new contact
printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].cFirstName);
printf("Last Name: ");
scanf("%s", phonebook[counter-1].cLastName);
printf("Phone Number: ");
scanf("%s", phonebook[counter-1].PhoNo);
printf("\n\tContact added\n");
}
//delete contact
void DeleteContact (pb * phonebook)
{
int x = 0;
char scrapcFirstName[20]; //strings for deleting original strings
char scrapcLastName[20];
char nullStr[20] = {"\0"};
printf("\nFirst name: ");
scanf("%s", scrapcFirstName);
printf("Last name: ");
scanf("%s", scrapcLastName);
//compare strings
for (x = 0; x < counter; x++)
{
if (strcmp(scrapcFirstName, phonebook[x].cFirstName) == 0)
{
for (x = 0; x < counter; x++)
{
if (strcmp(scrapcLastName, phonebook[x].cLastName) == 0)
{
strcpy(phonebook[x].cFirstName, nullStr);
strcpy(phonebook[x].cLastName, nullStr);
strcpy(phonebook[x].PhoNo, nullStr);
}//end if
else
{
printf("Invalid Input");
}
}//end for
}//end if
} // end for
counter--; // Contact deleted, update counter
printf("Contact Deleted\n");
}
// show phonebook
void ShowContacts (pb * phonebook)
{
int x = 0;
printf("\nPhonebook:\n\n ");
for( x = 0; x < counter; x++)
{
printf("\n(%d)\n", x+1);
printf("Name: %s %s\n", phonebook[x].cFirstName, phonebook[x].cLastName);
printf("Number: %s\n", phonebook[x].PhoNo);
} //end for
}
//Find a specific contact
void FindContact (pb * phonebook)
{
int x = 0;
char TempFirstName[20];
char TempLastName[20];
printf("\nWho are you looking for?");
printf("\n\nFirst Name: ");
scanf("%s", TempFirstName);
printf("Last Name: ");
scanf("%s", TempLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(TempFirstName, phonebook[x].cFirstName) == 0)
{
if (strcmp(TempLastName, phonebook[x].cLastName) == 0)
{
printf("\n%s %s \n%s\n", phonebook[x].cFirstName, phonebook[x].cLastName, phonebook[x].PhoNo);
}
}
}
}
//show a random contact
void RandContact (pb * phonebook)
{
int iRand = 0;
srand(time(NULL));
iRand = rand() % counter;
int x = iRand;
printf("\n%s %s\n", phonebook[x].cFirstName, phonebook[x].cLastName);
printf("%s\n", phonebook[x].PhoNo);
}
//delete all
void DeleteAll (pb * phonebook)
{
int x = 0;
char nullStr[20] = {'\0'};
for ( x = 0; x < counter; x++ )
{
strcpy(phonebook[x].cFirstName, nullStr);
strcpy(phonebook[x].cLastName, nullStr);
strcpy(phonebook[x].PhoNo, nullStr);
--counter;
}
printf("Contacts have been wiped.\n");
}
void Read(pb * phonebook)
{
FILE *pRead;
char name[256];
printf("File to read: ");
gets(name);
pRead=fopen(name,"a");
if(pRead != NULL)
{
printf("Contact List");
while(!feof(pRead)){
fread(phonebook, sizeof (struct phonebook), 1, pRead);
fclose(pRead);
if (!feof(pRead)){
fread(phonebook, sizeof (struct phonebook), 1, pRead);
fclose(pRead);
}
}
}
else{
goto ErrorHandler;
}
exit(EXIT_SUCCESS);
ErrorHandler:
perror("The following error occured");
exit(EXIT_FAILURE);
}
void Write(pb * phonebook)
{
FILE *pWrite;
char name[256];
printf("File to write:");
gets(name);
pWrite=fopen(name,"a");
if(pWrite != NULL)
{
fwrite(phonebook, sizeof (struct phonebook), 1, pRead);
fclose(pWrite);
}
else{
goto ErrorHandler;
}
exit(EXIT_SUCCESS);
ErrorHandler:
perror("The following error occured");
exit(EXIT_FAILURE);
}
It's still giving me the same error, "invalid argument"
In read function you have used pWrite instead of pRead
pWrite=fopen(name,"a");
And checking for EOF condition for pRead
while(!feof(pRead))
The file name is not being set: char name[256]; and then fopen(name,"a") that's why fopen fails.
Apparently gets() does not work after your scanf, correct your scanf on line 49: to scanf("%d\n"
Input in C. Scanf before gets. Problem
The other problems have been already pointed out (*pRead instead of *pWrite), not closing files etc.

Resources