Structures and fprintf() - c

I was wondering if it is possible to use fprintf() with a structure, because I know you can't use a "%" for the structure.
struct blackhole_register
{
int userID;
float blackhole_Mass;
char blackhole_ID[5];
char name_First[11];
char name_Last[16];
};
int main ()
{
struct blackhole_register input;
struct blackhole_register output;
FILE *blackhole_file;
if ((blackhole_file = fopen("Holter.txt","w")) == NULL)
{
printf("File location not found, the program will now end\n");
}
else
printf("Schwarzschild Radius Application by Jonathan Holter\n\n");
printf("\nFirst Name: ");
fgets(input.name_First,11,stdin);
printf("\nLast Name: ");
fgets(input.name_Last,16,stdin);
printf("\nUser ID: ");
fgets(input.userID,4,stdin);
printf("\nBlack Hole Name/ID: ");
fgets(input.blackhole_ID,20,stdin);
printf("\nBlack Hole Mass (Solar Masses): ");
fgets(input.blackhole_Mass,3,stdin);
This is what I have so far, any help would be wonderful!!

There is no magic %serialize-my-struct flag in printf format strings. You'll need to printf each field in the struct separately. Consider writing a function int print_blackhole_register(FILE*, const blackhole_register*).

Related

What does it mean by too few arguments to function 'addBook'?

I'm basically trying to add a book into my libraryy.txt file my problem is the compiler says that there's too few arguments to perform the addBook() function, what should I include in the addBook() function parentheses in my main function?
struct book{
char *title;
char *author;
char *subject;
};
struct library {
struct book collection;
int num_books;
struct library *next;
};
This is my addBook() function:
void addBook(struct library* thislib){
FILE *cfPtr;
cfPtr = fopen("libraryy.txt", "a");
char strtitle[MAX];
char strauthor[MAX];
char strsubject[MAX];
int operation;
struct library acollection;
printf("Title: ");
scanf("%s", strtitle);
printf("Author: ");
scanf("%s", strauthor);
printf("Subject: ");
scanf("%s", strsubject);
thislib->collection.title = strtitle;
thislib->collection.author = strauthor;
thislib->collection.subject = strsubject;
printf("The book %s author %s subject %s has been added to the library.\n", thislib->collection.title, thislib->collection.author, thislib->collection.subject);
fprintf(cfPtr, "%d %s %s %s\n", operation, collection.title, collection.author, collection.subject);
}
The compiler is telling me that there are too few arguments to perform function addBook..what should I add in the parentheses for addBook?? The error is in my main method below:
FILE *cfPtr;
int operation;
if ((cfPtr = fopen("libraryy.txt","a")) == NULL){
printf("File could not be opened...\n");
}
else {
while(1){
operate();
printf("Enter an operation: ");
scanf("%d", &operation);
switch(operation){
case 1:
addBook(); //it basically gave me the error here which is 'too few arguments to perform function addBook'
break;
Don't get me wrong, I know what does too few arguments/no arguments mean but what should I include in those parentheses as arguments? :") Need some help.

Invocation of structure member info display function spits garbled characters

I created a structure that goes as follows:
typedef struct
{
char name[20];
char country[20];
int age;
float height;
} details;
Accompanying the structure definition are two functions that accept and display the details of the structure's members respectively:
void enter_details(details d1)
{
printf("Enter your name: ");
gets(d1.name);
printf("Enter your country: ");
gets(d1.country);
printf("Enter your age: ");
scanf("%d",&d1.age);
printf("Enter your height (in meters): ");
scanf("%d",&d1.height);
}
void show_details(details d1)
{
printf("Name: ");
puts(d1.name);
printf("\nCountry: ");
puts(d1.country);
printf("\nAge: %d",d1.age);
printf("\nHeight: %.2f m",d1.height);
}
The declaration of a structure object in the main() segment is relatively straightforward and goes as follows:
details d1;
While the compiler doesn't spit any errors as such, and neither does the invocation of the function that accepts information for the structure's members, invocation of the show_details() function spits out nothing but garbled, gibberish characters. What is causing this anomaly?
In C, arguments of a function are passed by value, not by reference, meaning that the argument you pass will be copied. So what you are dealing with inside your function is a copy of your object, not the actual object. In order to refer to the actual object, you need to pass a reference.
Also, avoid using scanf() (and gets()) to read input. Use fgets() instead.
So, you should change your code to:
void enter_details(details *d1)
{
printf("Enter your name: ");
getstring(d1->name, sizeof(d1->name)));
printf("Enter your country: ");
getstring(d1->country, sizeof(d1->country));
printf("Enter your age: ");
getint(&d1->age);
printf("Enter your height (in meters): ");
getint(&d1->height);
}
An alternative to scanf() to read a string:
char *getstring(char *str, int size)
{
if (!fgets(str, size, stdin)) {
fprintf(stderr, "Input error\n");
return NULL;
}
str[strcspn(str, "\n")] = '\0'; // fgets() reads the ending '\n' so null-terminate the string
return str;
}
And to read an int:
int getint(int *i)
{
char buffer[255];
if (!fgets(buffer, 255, stdin)) {
fprintf(stderr, "Input error\n");
return 0;
}
str[strcspn(str, "\n")] = '\0';
if (sscanf(buffer, "%d", i) != 1)
return 0;
return 1;
}
Do not use gets. Use fgets instead. fgets(d1.name, sizeof(d1.name), stdin);
You need to pass the reference to the struct otherwise you will work on the local copy of the structure. You can also return the structure.
void enter_details1(details *d1)
{
printf("Enter your name: ");
fgets(d1 -> name, sizeof(d1 ->name), stdin);
printf("Enter your country: ");
fgets(d1 -> country, sizeof(d1 -> country), stdin);
printf("Enter your age: ");
scanf("%d",&d1 ->age);
printf("Enter your height (in meters): ");
scanf("%d",&d1 ->height);
}
details enter_details(void)
{
details d1;
printf("Enter your name: ");
fgets(d1.name, sizeof(d1.name), stdin);
printf("Enter your country: ");
fgets(d1.country, sizeof(d1.country), stdin);
printf("Enter your age: ");
scanf("%d",&d1.age);
printf("Enter your height (in meters): ");
scanf("%d",&d1.height);
retunrn d1;
}
usage:
int main(void)
{
details d;
enter_details1(&d);
//or
d = enter_details();
/* .... */
}
BTW I hope that you know that height can be only entered in full meters. No fractions

fprintf function in code writes garbage data into .csv file

I'm trying to create a C program which collect's an applicant's information. When a user is prompted to enter their written subjects, the program writes rubbish data into the .csv file when they wrote one. And sometimes does the same when the number of subjects written is two.
I've tried to clear the buffer stream, but it's no use. Strangely, using different compliers like DevC++, Embarcadero DevC and VS Code produces different results.
Edit: I've also noticed the chances of the rubbish values being written into the file are lowered when the grades of the subjects is lower than the number of subjects written.
Attached below is the code. And an image of the output.
// C libraries.
#include <stdio.h> // Contains function prototypes for the standard input/output library functions, and information used by them.
#include <conio.h> // Contains function prototypes for the console input/output library functions.
#include <stdlib.h> // Contains function prototypes for conversions of numbers to text and text to numbers, memory allocation, random numbers and other utility functions.
#include <string.h> // Contains function prototypes for string-processing functions.
#include <time.h> // Contains function prototypes and types for manipulating the time and date.
#include <stdbool.h> // Contains macros defining bool, true and false, used for boolean variables.
struct Applicant
{
int applicationID;
int dateOfApplication;
char lastName[21];
char firstName[21];
char middleName[21];
char dateOfBirth[21];
int age;
char gender;
char address[100];
char phoneNumber[21];
char emailAddress[51];
char mobileNumber[21];
int numSubjectsWritten;
char csecSubjects[20][100];
char grades[20];
char programmeSelection[10];
};
struct Applicant getApplicantData()
{
struct Applicant applicant;
int i = 0;
int numSubjects;
// Asking for applicant input for various fields.
printf("| Personal |");
printf("\nEnter Last Name: ");
scanf(" %20s", &applicant.lastName);
fflush(stdin);
printf("\nEnter First Name: ");
scanf(" %20s", &applicant.firstName);
fflush(stdin);
printf("\nEnter Middle Name (If you don't have a middle name, leave this field blank.): ");
gets(applicant.middleName);
fflush(stdin);
/*
printf("\nEnter Date of Birth: ");
scanf(" %s", &applicant.dateOfBirth);
fflush(stdin);
printf("\nEnter Gender. 'M' for male, 'F' for female, (M|F): ");
scanf(" %c", &applicant.gender);
fflush(stdin);
printf("\n\n| Contact Information |");
printf("\nEnter Address: ");
gets(applicant.address);
fflush(stdin);
printf("\nEnter Phone Number: ");
gets(applicant.phoneNumber);
fflush(stdin);
printf("\nEnter Email Address: ");
gets(applicant.emailAddress);
fflush(stdin);
printf("\nEnter Mobile Number: ");
gets(applicant.mobileNumber);
fflush(stdin);
*/
printf("\n\n| Education |");
printf("\nEnter Number of Subjects Written: ");
scanf("%d", &applicant.numSubjectsWritten);
fflush(stdin);
while (i < applicant.numSubjectsWritten)
{
printf("\nEnter the subject: ");
gets(applicant.csecSubjects[i]);
fflush(stdin);
printf("\nEnter the grade for that subject: ");
scanf(" %c", &applicant.grades[i]);
fflush(stdin);
i++;
}
return applicant;
}
int main(void)
{
FILE *file = fopen("Data.csv", "a+");
int i, j;
if (!file)
{
printf("\nError! Can not open data file.\nPlease contact the Program Addmission Manager as soon as possible with the error message.");
exit(1);
}
else
{
struct Applicant applicant = getApplicantData();
//fprintf(file, "%s:%s:%s:%s:%c:%s:%s:%s:%s", applicant.lastName, applicant.firstName, applicant.middleName, applicant.dateOfBirth, applicant.gender, applicant.address, applicant.phoneNumber, applicant.emailAddress, applicant.mobileNumber);
fprintf(file, "%s:%s:%s:", applicant.lastName, applicant.firstName, applicant.middleName);
for (i = 0; applicant.csecSubjects[i][0] != '\0'; i++)
{
fprintf(file, " %s", applicant.csecSubjects[i]);
fflush(stdout);
fflush(stdin);
fflush(file);
fprintf(file, " ( %c):", applicant.grades[i]);
fflush(stdout);
fflush(stdin);
fflush(file);
}
}
return 0;
}
First problems I see:
Remove the & from all instances where you scanf a string
Don't use gets, or mix scanf and fgets
Don't fflush(stdin)
Instead of scanf, consider using a custom-made input method with condition checking and anything you need. I will give an example.
#define BUFFER_SIZE 512
void input(char* buffer){
memset(buffer, 0, BUFFER_SIZE); // Initializing the buffer.
fgets(buffer, BUFFER_SIZE, stdin);
strtok(buffer,"\n");
}
How to take input using that?
void main(){
int username[BUFFER_SIZE];
input(username);
}
A way to write a structure to a file is shown below.
void Structure_Print(Applicant* applicant, FILE* stream, int no_of_applicant){
if(no_of_applicant==0){
fprintf(stdout, "No applicant yet.\n");
return;
}
fprintf(stream, "%s:%s:%s:", applicant.lastName, applicant.firstName, applicant.middleName);
for (i = 0; applicant.csecSubjects[i][0] != '\0'; i++)
{
fprintf(stream, " %s:", applicant.csecSubjects[i]);
fprintf(stream, " %c:", applicant.grades[i]);
}
return;
}
Also, I noticed how you tried to make it readable while saving it in subject(grade) format. I recommend you to not do that. Your .csv file is just for database. Nobody is going to read it. So just store the data by comma or any character separator. It will make it easier to extract data later.

C structures displaying numbers rather than user data

Kindly help me debug this code. It is not displaying the correct data. The following program is supposed to get book details from the user, dynamically allocate memory to them and display them.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "problem5.h"
int main()
{
struct books *b;
b = (struct books*)malloc(sizeof(struct books));
int command, flag = 0;
int n=0, i;
while(flag == 0)
{
printf ("1. Add Book\n");
printf ("2. View Books\n");
printf ("3. Quit\n");
scanf("%d", &command);
if (command == 1)
{
printf ("Enter Name\n");
//scanf("%d", &(b+i)->name);
scanf(" ");
gets((b+i)->name);
printf ("Enter Author\n");
//scanf("%d", &(b+i)->author);
scanf(" ");
gets((b+i)->author);
printf ("Enter Year Published\n");
scanf("%d", &(b+i)->year_published);
n=n+1;
i=n;
} else if (command == 2)
{
for(i=0; i<n; i++)
{
printf ("%d - %d by %d\n", (b+i)->year_published, (b+i)->name, (b+i)->author);
}
} else if (command == 3)
{
flag = 1;
} else
{
printf ("Invalid choice!\n");
}
}
}
The following is problem5.h header file that has the structure books. Initially I didn't declare the variables in array since I didn't want to use much memory. But I had to due to many errors.
#define PROBLEM3_H_INCLUDED
typedef struct books{
char *name[30];
char *author[30];
int year_published;
};
#endif // PROBLEM3_H_INCLUDED
When I print I am getting random numbers instead of the data the user entered.
The overall design of your code is wrong.
This is basically what you want.
I made following changements:
using meaningful variable names
changed struct book so the structure can contain one book. Also renamed it from struct books to struct book because the structure contains only one book.
allocating memory properly
using books[numberofbooks].x instead of the less readable *(books + numberofbooks)->x
More explanations in the comments.
#include <stdio.h>
#include <stdlib.h>
struct book {
char name[30];
char author[30];
int year_published;
};
int main()
{
struct book* books = NULL; // no books at all initially so we
// initialize to NULL
// so we can simply use realloc
int numberofbooks = 0;
int programend = 0;
while (programend == 0)
{
printf("1. Add Book\n");
printf("2. View Books\n");
printf("3. Quit\n");
int command;
scanf("%d", &command);
if (command == 1)
{
getchar(); // consume Enter key (due su scanf)
// allocate memory for one more book
books = realloc(books, sizeof(struct book) * (numberofbooks + 1));
printf("Enter Name\n");
gets(books[numberofbooks].name);
printf("Enter Author\n");
gets(books[numberofbooks].author);
printf("Enter Year Published\n");
scanf("%d", &books[numberofbooks].year_published);
numberofbooks++; // increment number of books
}
else if (command == 2)
{
for (int i = 0; i < numberofbooks; i++)
{
printf("%d - %s by %s\n", books[i].year_published, books[i].name, books[i].author);
}
}
else if (command == 3)
{
programend = 1;
}
else
{
printf("Invalid choice!\n");
}
}
}
There is still room for improvement though:
error checking for realloc
error checking for interactive I/O
not using the deprecated and dangerous gets
and certainly a few other things
b = (struct books*)malloc(sizeof(struct books));
Here, you are allocating memory for only one instance of struct books , But you are accessing multiple instances of struct books.
printf ("%d - %d by %d\n", (b+i)->year_published, (b+i)->name, (b+i)->author);
For i>=1 (b+i) is not defined, because you did not allocate memory for it. You have allocated memory for only (b+0).
int n=0, i;
gets((b+i)->name);
Here, i has not been initiliazed.

How do I add a contact to a phonebook program in C?

For my intro to programming class, we have to code a phonebook in C that lets users add contacts, as well as delete and display them. It also has to allocate and free memory as necessary (I tried to do this, but I honestly don't really know what I'm doing).
Anyway, I cannot figure out how to add a contact to the phonebook. I've pasted the relevant part of the program so far. It compiles, but it crashes every time I try to add a contact. Once I get this figured out, I think I can get the rest of the functions without too much trouble. If anyone could help me out, I'd really appreciate it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct entry {
char fname[20];
char lname[20];
char pnumber[20];
} entry;
// function prototypes
void addentry(int, entry*, char addfname[20], char addlname[20], char addpnumber[20]);
main() {
int selection = 0;
int inputtest = 1;
int pnum = 0; // keeps track of number of contacts
char addfname[20] = { '\0' };
char addlname[20] = { '\0' };
char addpnumber[20] = { '\0' };
entry *pcontacts;
pcontacts = (entry*)calloc(1, (sizeof(entry)));
if (pcontacts == NULL) {
printf("No memory is available.");
free(pcontacts);
return 0;
}
while (1) {
do {
printf("\nPhonebook Menu\n\n");
printf("1:\tAdd contact\n");
printf("2:\tDelete contact\n");
printf("3:\tDisplay contacts\n");
printf("4:\tExit\n");
printf("\nChoose an action (1-4): ");
scanf("%d", &selection);
if (selection < 1 || selection > 4) {
printf("Invalid input. Please enter an integer between 1 and 4.\n");
inputtest = 0;
}
if (selection == 4) {
free(pcontacts);
printf("\nThank you for using this phonebook.");
return 0;
}
switch (selection) {
case 1:
pnum++;
printf("\nEnter first name: ");
scanf("%s", addfname);
printf("Enter last name: ");
scanf("%s", addlname);
printf("Enter phone number (no spaces): ");
scanf("%s", addpnumber);
addentry(pnum, pcontacts, addfname[20], addlname[20], addpnumber[20]);
break;
}
} while (inputtest == 1);
}
}
void addentry(int pnum, entry *pcontacts, char addfname[20], char addlname[20], char pnumber[20]) {
pcontacts = (entry*)malloc(pnum * (sizeof(entry)));
if (pcontacts != NULL) {
strcpy(*pcontacts[pnum - 1].fname, addfname);
printf("\nContact has been added.");
} else {
printf ("No memory is available.\n");
}
}
You get strings from standard input with scanf, but you should tell scanf the maximum number of bytes to store to the destination arrays to avoid buffer overruns:
scanf("%19s", addfname);
...
scanf("%19s", addlname);
...
scanf("%19s", addpnumber);
The way you call addentry is incorrect:
addentry(pnum, pcontacts, addfname[20], addlname[20], addpnumber[20]);
You actually try to read the byte just after the end of addfname, addlname and addpnumber. You should instead pass the arrays themselves, that will be passed to the function addentry as pointers to their first bytes:
addentry(pnum, pcontacts, addfname, addlname, addpnumber);
addentry should reallocate the array with realloc. It should be passed a pointer to the array pointer to it can update the pointer in main.
addentry does not copy the strings correctly: it only copies one, but with a syntax error.
Here is a corrected version:
void addentry(int, entry**, char addfname[20], char addlname[20], char addpnumber[20]);
int main(void) {
int selection = 0;
int inputtest = 1;
int pnum = 0; // keeps track of number of contacts
char addfname[20];
char addlname[20];
char addpnumber[20];
entry *pcontacts = NULL;
for (;;) {
do {
printf("\nPhonebook Menu\n\n");
printf("1:\tAdd contact\n");
printf("2:\tDelete contact\n");
printf("3:\tDisplay contacts\n");
printf("4:\tExit\n");
printf("\nChoose an action (1-4): ");
scanf("%d", &selection);
if (selection < 1 || selection > 4) {
printf("Invalid input. Please enter an integer between 1 and 4.\n");
inputtest = 0;
}
if (selection == 4) {
free(pcontacts); /* OK for NULL */
printf("\nThank you for using this phonebook.");
return 0;
}
switch (selection) {
case 1:
printf("\nEnter first name: ");
scanf("%19s", addfname);
printf("Enter last name: ");
scanf("%19s", addlname);
printf("Enter phone number (no spaces): ");
scanf("%19s", addpnumber);
addentry(pnum, &pcontacts, addfname, addlname, addpnumber);
pnum++;
break;
}
} while (inputtest == 1);
}
}
/* add an entry at position pnum */
void addentry(int pnum, entry **pp, char addfname[20], char addlname[20], char pnumber[20]) {
entry *pcontact = *pp;
pcontacts = realloc(pcontacts, (pnum + 1) * sizeof(entry));
if (pcontacts != NULL) {
*pp = pcontacts; /* update pointer in main */
strcpy(pcontacts[pnum].fname, addfname);
strcpy(pcontacts[pnum].lname, addlname);
strcpy(pcontacts[pnum].pnumber, addpnumber);
printf("\nContact has been added.");
} else {
printf ("No memory is available.\n");
}
}

Resources