Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Sunday, January 12, 2020

Write a student data into a file using the C Structure

File Management using C Structure

This C Program will write a student data into a file using the Structure and File Management concept of C programming.

Source Code

#include <stdio.h>
//Structure to store student data.
struct student
{
   char fname[30];
   char lname[30];
   int  rollno;
   float percentage;
};

void main ()
{
   FILE *fp;
   //declare structure(student) variable
   struct student input;
   // open student file for writing
   fp = fopen ("student.dat","w");
   if (fp == NULL){
      printf("\nFile opening error..\n\n");
      exit (1);
     }

   printf("Enter \"exit\" as First Name to stop reading user input.");

   while (1)
     {
      printf("\nFirst Name: ");
      scanf ("%s", input.fname);
     
      if (strcmp(input.fname, "exit") == 0)
         exit(1);
     
      printf("Last Name : ");
      scanf ("%s", input.lname);
      printf("Roll Number  : ");
      scanf ("%d", &input.rollno);
      printf("Percentage : ");
      scanf ("%f", &input.percentage);

      // write student data to student.dat  file
      fwrite (&input, sizeof(struct student), 1, fp);
     }
}

Output of program 

Enter "exit" as First Name to stop reading user input.
First Name: K
Last Name : Patel
Roll Number  : 101
Percentage : 90.50

First Name: ABC
Last Name : Shah
Roll Number  : 102
Percentage : 95.50

First Name: exit

* * * * *


< Read more about C File Management >

< Read more about C Structure >

< Back to Home Page>



Saturday, October 8, 2016

C program to implement Stack using array.



#include<stdio.h>
void push();
void pop();
void display_stack();
int stack_pointer=0, stack[10];

int main()
{
  int n;
  do{
      printf("===== Stack Operation =====\n");
      printf("1. Push\n2. Pop\n3. Display Stack\n4. Exit\n");
      printf("Enter your choice:");
      scanf("%d", &n);
      switch(n){
        case 1: push();
                if(stack_pointer>=10)
                {
                  printf("Stack is full.\n");
                  exit(0);
                }
                break;
        case 2: pop();
                break;
        case 3: display_stack();
                break;
        case 4: printf("Thank you. Have a nice time..");
                break;
        default:
                printf("Select proper menu item.\n");
        }
  }while(n != 4);
  return 0; 
}
void push(){
   printf("\nPush Operation\n");
   printf("--------------\n");
   printf("Enter number to be pushed:");
   scanf("%d", &stack[stack_pointer++]);
   printf("Push Operation performed.\n");
   printf("-------------------------\n\n");
}
void pop(){
   printf("\nPop Operation performed.\n");
   printf("------------------------\n");
   printf("Popped out element from stack is %d\n\n", stack[stack_pointer-1]);
   stack_pointer--;
   getch();//developed by kp
}
void display_stack(){
   int i;
   if(stack_pointer==0)
       printf("Stack is empty.\n\n");
   else
   {
       printf("\nStack Elements are as under:\n");
       printf("----------------------------\n");
       for(i=0; i<stack_pointer; i++)
       {
           printf("%d\n", stack[i]);
       }
   }
   printf("\nPress any key to continue...\n\n");
   getch();//cprogrampracticals.blogspot.in
}


Output of the program:

===== Stack Operation =====
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice:1

Push Operation
--------------
Enter number to be pushed:5
Push Operation performed.
-------------------------

===== Stack Operation =====
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice:1

Push Operation
--------------
Enter number to be pushed:10
Push Operation performed.
-------------------------

===== Stack Operation =====
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice:3

Stack Elements are as under:
----------------------------
5
10

Press any key to continue...

===== Stack Operation =====
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice:2

Pop Operation performed.
------------------------
Popped out element from stack is 10

===== Stack Operation =====
1. Push
2. Pop
3. Display Stack
4. Exit
Enter your choice:4
Thank you. Have a nice time..