Getting looped struct data into a file in C - c

I have a looped struct array that accepts student name and roll number for as many students the user inputs
Code:
int x;
struct studs
{
char name[50];
char rollno[50];
}student[50];
printf("\t\t________________________________");
printf("\n\n\t\t\tStudent Registration");
printf("\n\t\t_______________________________");
printf("\n\n\tHow many students in %s ", course);
scanf("%d", &stud);
admin = fopen(name, "a");
fprintf(admin, "\nNumber of students for %s: %d", course, stud);
fclose(admin);
for(x=0; x<stud; x++)
{
printf("\n\nStudent %d", x+1);
printf("\n_____________________________");
printf("\n\nStudent Name : ");
scanf("%s", student[x].name);
printf("\nStudent Rollno. : ");
scanf("%s", student[x].rollno);
system("\npause");
goto exam;
}
admin = fopen("student.txt", "w");
fprintf(admin, "Name: %s \nRoll Number: %s\n\n", student.name, student.rollno);
fclose(admin);
As you can see i'm trying to put each name and number into the text file but I keep getting this errors
error: request for member 'name' in something not a structure or union |
error: request for member 'rollno' in something not a structure or union
Is there anyway to get this into a file, with or without the struct?

This
fprintf(admin, "Name: %s \nRoll Number: %s\n\n", student.name, student.rollno);
cannot work, student is an array of stud's (or at least it would be if i were in the school). You have already shown that you know how to iterate over the number of students with for(x=0; x<stud; x++), so just do it again:
for(x=0; x<stud; x++) {
fprintf(admin, "Name: %s \nRoll Number: %s\n\n", student[x].name, student[x].rollno);
}
There are likely going to be other errors in your program (Like that goto exam immediately jumping out of your for loop to somewhere). But this will at least deal with the error you have described.

Related

Program producing a bus error when reading in using scanf - C Program

I'm writing a program for an employee database and I'm writing the function to add an employee. I'm getting a bus error after my final prompt to scan in info. I'm pretty sure its to do with my scanf statement as I have a print statement right after that is not printing. Why would I be getting this error?
The prompt in question is for reading in job title.
void addEmployee(void)
{
char *name;
char gender;
int age;
char *title;
printf("Enter name: \n");
scanf(" %100s", name);
scanf("%*[^\n]%*c");
printf("Enter gender: \n");
scanf(" %1c", &gender);
scanf("%*[^\n]%*c");
printf("Enter age: \n");
scanf(" %d", &age);
scanf("%*[^\n]%*c");
printf("Enter job title: \n");
scanf(" %100s", title);
scanf("%*[^\n]%*c");
printf("Test");
printf("The employee you've entered is: %s %c %d %s \n", name, gender, age, title);
Employee newEmp = {*name, gender, age, *title};
if(employeeList[0] == NULL)
{
employeeList[0] = &newEmp;
nodeCount++;
}
}
Code is passing in an uninitialized pointer.
char *name; // Pointer 'name' not initialize yet.
printf("Enter name: \n");
// 'name' passed to scanf() is garbage.
scanf(" %100s", name);
Instead, pass a pointer to an existing array
char name[100 + 1];
printf("Enter name: \n");
// Here the array 'name' coverts to the address of the first element of the array.
// scanf receives a valid pointer.
scanf("%100s", name);

C: copy string into list of strings

So I have a list of names and corresponding phone numbers, and I want the user to be able to continuously enter a new name-number pair into that list. The part of my code where I try to do that looks something like this:
char name[20], list_names[1000][20], phone[20], list_phone[1000][20];
int n;
n = 0;
do
{
printf("Enter name: ");
scanf("%20[^\n]", name);
printf("Enter phone number of %s: ", name);
scanf("%20[^\n]", phone);
strcpy(list_names[n], name);
strcpy(list_phone[n], phone);
n += 1;
}
while (n < 1000);
This usually gives me an error like "incompatible pointer type". I have to do it the indirect way and first store the name in a separate variable. But how do I get the string from that variable into the list? Probably there's something I don't get in the strcpy() part.
Thanks for helping out!
try this
printf("Enter name: ");
scanf(" %19[^\n]", name);//add one space and turn 20 to 19 (leave space for '\0')
printf("Enter phone number of %s: ", name);
scanf(" %19[^\n]", phone);

Program stops working and an error message is displayed when i run it

This error message is shown in every software.
it has stop working check online solution and close the program
There is no error in code but when I run this program I got this error and I found that most of the program where user input is required it stops working. I have used code blocks, C free, dev C++
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[10];
} stu1 = {100, "ram"};
main()
{
struct student stu2;
printf("2nd student name is: %s \n",stu1.name);
printf("second student roll no: %s \n ",stu1.roll);
printf("enter second student data ");
scanf("%d", &stu2.roll);
printf("enter second student name ");
scanf("%s",&stu2.name);
printf("2nd student name is: %s \n",stu2.name);
printf("second student roll no: %s \n ",stu2.roll);
getch();
}
Image with error message: https://i.stack.imgur.com/ZZsAU.png
#include<stdio.h>
struct student
{
int roll;
char name[10];
}stu1 = {100, "ram"};
int main()
{
struct student stu2;
printf("2nd student name is: %s \n",stu1.name);
printf("second student roll no: %d \n ",stu1.roll); // line 1
printf("enter second student data ");
scanf("%d", &stu2.roll);
printf("enter second student name ");
scanf("%s",stu2.name); // line 2
printf("2nd student name is: %s \n",stu2.name);
printf("second student roll no: %d \n ",stu2.roll); // line 3
return 0;
}
I have corrected the code like this. Lines 1, 2 and 3 was the problem I think. While changing those lines as shown above code no longer runs to segmentation fault.
NB : Don't use scanf for user input. If you use scanf at all, always check its return value. scanf %s is a bug: It can't prevent buffer overflows from longer input.
This could be another approach:
#include<stdio.h>
#include<conio.h>
typedef struct
{
int roll;
char *name;
} studentT;
int main()
{
studentT stu1, stu2;
stu1.roll = 100;
stu1.name = "ram";
printf("2nd student name is: %s \n",stu1.name);
printf("second student roll no: %d \n ",stu1.roll);
printf("enter second student data ");
scanf("%d", &stu2.roll);
printf("enter second student name ");
scanf("%s",&stu2.name);
printf("2nd student name is: %s \n",stu2.name);
printf("second student roll no: %d \n ",stu2.roll);
getch();
}

What is wrong with the following code? I am trying to search for a record in a text file

The program runs fine till I create the file and enter records from structure, but when I try to search a record by using roll no, it crashes.
I am using a structure called student to store data and then I am creating a text file and then I use a for loop to write data on the text file. Then I reopen the file and try to search a record using student roll no.
Program runs fine until I try to search the student roll no. It crashes right after I enter the student roll no to be searched.
Can anyone tell me what modification is needed to make the search work?
Below is my code:
#include<stdio.h>
struct student {
int roll_no;
char name [80];
int age;
}st[30],s;
int main ()
{
int i,n;
char fname[80];
int search;
int found;
FILE *fp;
printf("\nEnter the file name : \n");
scanf("%s",fname);
fp=fopen(fname,"w");
if(fp==NULL)
{
printf("\nCannot create file :");
exit(0);
}
printf("\nNumber of students : \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nInformation for student#%d : \n\n",i+1);
printf("\nStudent roll number : \n" );
scanf("%d",&st[i].roll_no);
printf("\nStudent name: \n: ");
scanf("%s",st[i].name);
printf("\nStudent age : ");
scanf("%d", &st[i].age);
}
fprintf(fp, "\nStudent roll no\t\t Student name\t\t student age\t\t\n\n");
for(i=0;i<n;i++)
{
fprintf(fp, "\n%d \t\t %s \t\t %d \t\t", st[i].roll_no,st[i].name,st[i].age);
}
fclose(fp);
fp=fopen(fname,"r+t");
if(fp==NULL)
{
printf("\nCannot open file\n");
exit(0);
}
printf("\n\nStudent roll no to be searched : ");
found=0;
scanf("%d", search);
while(!(feof(fp)) && (found==0))
{
fscanf(fp,"%d,%s,%d",&s.roll_no,s.name,&s.age);
if(s.roll_no==search)
{
fseek(fp,-sizeof(struct student), SEEK_CUR);
printf("\nEnter new name : \n");
scanf("%s", s.name);
fprintf(fp, "\n%d \t\t %s \t\t %d \t\t", s.roll_no,s.name,s.age);
found=1;
}
}
if(found=0)
{
printf("\nStudent record doesn't exist \n");
}
fclose(fp);
return 0;
}
In your code, you're missing an address-of operator in scanf(), thereby passing an invalid type of argument. Basically
scanf("%d", search);
should be
scanf("%d", &search);
That said, it is always a good practice to size-limit the inputs for string, like, for an array defined like char fname[80];, you should use
scanf("%79s",fname);
to avoid possible buffer overflow by excessively long input.
Also, always check for the return value of scanf() and family of functions to ensure their success.

Data structure and patients records

Computerizing health records could make it easier for the patients to share their health profiles and histories among their various health care professionals. A health clinic needs your help to computerize the patients' health records. The patient's records consist of first name, middle name, last name (including SR. JR., etc), gender, date of birth, height (in inches), weight (in pounds). The clinic requires the following features of the program:
read existing record from a file where each patient record is one line entry separating each data with comma
add additional records to file
a function to calculate and return patients age in 3yrs
a function that calculates body mass index with the given formula BMI=(weight-in-pounds X 703)/(height-in-inches X 2) or BMI = (weight-in-kgs)/(height-in-meters X 2)
search patient's name and display patient's information with age and BMI value including category
update patient's information on date of birth, height and/or weight and save updates to file
display all records in tabular format
So far what I have made is:
#include<stdio.h>
#include<stdlib.h>
main(){
FILE*fin;
char name,fname,mname,lname,ename,gender,ch,getch,patient;
int dob,month,day,year,height,weight;
fin=fopen("oldrec.c","w");{
printf("Error: File does not exists");
return 0;
}
{
printf("Add Record? y/n");
ch=toupper(getch);
if(ch='y')
break;
}while (1);
struct patient{
char name;
char fname[20];
char mname[20];
char lname[20];
char gender;
int dob;
int month;
int day;
int year;
int height;
int weight;
printf("/n Patient's Name");
printf("First Name: ");
scanf("%s", &patient.fname);
printf("Middle Name: ");
scanf("%s", &patient.mname);
printf("Last Name: ");
scanf("%s", &patient.lname);
printf("Gender: ");
scanf("%s", &patient.gender);
printf("Date of Birth");
printf("Month: ");
scanf("&d", &patient.month);
printf("Day: ");
scanf("&d", &patient.day);
printf("Year: ");
scanf("%s", %patient.year);
printf("Height: ");
scanf("%d", & patient.height);
printf("Weight: ");
scanf("%d", &patient.weight);
}
I have made another file already, but when I run the codes, it says "Error: File does not exist". What is wrong, and what are the codes for the other problems? Please help me! This is our final requirement on my data structure subject.
fin=fopen("oldrec.c","w");{ // no if
printf("Error: File does not exists"); // all statements will be executed
return 0; // and function will terminate here
}
Ofcourse it will show that message , no condition . No matter if fopen is successful without if all statements will be executed.
Put it in a if block witn a condition .
Write like this -
fin=fopen("oldrec.c","w");
if(fin==NULL){ // check if fin is NULL
printf("Error: File does not exists");
return 0;
}
Other problems are these statements -
scanf("%s", &patient.fname);
...
scanf("%s", &patient.mname);
...
scanf("%s", &patient.lname);
...
scanf("%s", &patient.gender); // use %c for reading char variable
...
scanf("%s", %patient.year); // use %d to read int
^ whats this
Write these statemetns like this -
scanf("%s", patient.fname);
...
scanf("%s", patient.mname);
...
scanf("%s", patient.lname);
...
scanf("%c", &patient.gender);
...
scanf("%d", &patient.year);

Resources