1. Question:Writing of entire array to a file using c program 

    Answer
    #include<stdio.h>
    int main(){
      FILE *p;
      int i,a[10];
      if((p=fopen("myfile.dat","wb"))==NULL){
          printf("\nUnable to open file myfile.dat");
          exit(1);
      }
      printf("\nEnter ten values, one value on each line\n");
      for(i=0;i<10;i++)
          scanf("%d",&a[i]);
      fwrite(a,sizeof(a),1,p);
      fclose(p);
      return 0;
    }

    1. Report
  2. Question:Concatenate many files and store them in a file in c programming language 

    Answer
    #include<stdio.h>
     void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc);
     int main(int argc,char *argv[]){
       FILE *fp1,*fp2;
       concatenate(fp1,fp2,argv,argc);
       return 0;
     }
    
    void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc){
       int i,ch;
       fp2=fopen("files","a");
       for(i=1;i<argc-1;i++){
          fp1=fopen(argv[i],"r");
          while((ch=getc(fp1))!=EOF)
          putc(ch,fp2);
       }
     }

    1. Report
  3. Question:C program to get total file size 

    Answer
    #include <time.h>
    #include <sys\stat.h>
    #include <stdio.h>
    void main()
    {
        struct stat status;
        FILE *fp;
        fp=fopen("test.txt","r");
        fstat(fileno(fp),&status);
        clrscr();
        printf("Size of file : %d",status.st_size);
        printf("Drive name   : %c",65+status.st_dev);
        getch();
    }

    1. Report
  4. Question:Write a c program to know given file is regular file, character special or it is directory? 

    Answer
    #include "time.h"
    #include "sys\stat.h"
    #include "stdio.h"
    void main(){
        struct stat status;
        FILE *fp;
        stat("c:\\tc\\bin",&status);
        clrscr();
        if (status.st_mode & S_IFDIR)
             printf("It is directory.\n");
        if (status.st_mode & S_IFCHR)
             printf("It is chracter file.");
        if (status.st_mode & S_IFREG)
             printf("It is reggular file.");
        getch();
    }
    Output: It is directory.

    1. Report
  5. Question:Write a c program to know read/write permission of given file. 

    Answer
    #include "time.h"
    #include "sys\stat.h"
    #include "stdio.h"
    void main(){
        struct stat status;
        FILE *fp;
        stat("test.txt",&status);
        clrscr();
        if (status.st_mode & S_IREAD)
             printf("You have read permission.\n");
        if (status.st_mode & S_IWRITE)
             printf("You have write permission.");
        getch();
    
    }

    1. Report
  6. Question:Write a c program to know the last date of modification of any file 

    Answer
    #include "time.h"
    #include "sys\stat.h"
    #include "stdio.h"
    int main(){
        struct stat status;
        FILE *fp;
        fp=fopen("test.txt","r");
        fstat(fileno(fp),&status);
        
        printf("Last date of modification : %s",ctime(&status.st_ctime));
        return 0;
    }

    1. Report
  7. Question:Write a c program to find out the size and drive where file has stored of any given file? 

    Answer
    #include "time.h"
    #include "sys\stat.h"
    #include "stdio.h"
    int main(){
        struct stat status;
        FILE *fp;
        fp=fopen("test.txt","r");
        fstat(fileno(fp),&status);
        printf("Size of file : %d",status.st_size);
        printf("Drive name   : %c",65+status.st_dev);
        return 0;
    }

    1. Report
  8. Question:How to use complex numbers in c 

    Answer
    #include<stdio.h>
    int main(){
    int a,b;
    
    printf("Enter any complex number in the format a+ib: ");
    scanf("%d+i%d",&a,&b);
    
    printf("Real part is: %d",a );
    printf("\nImaginary part is: %d",b);
    
    return 0;
    
    }

    1. Report
  9. Question:C program to printf complex numbers 

    Answer
    #include<stdio.h>
    int main(){
    int a,b;
    
    printf("Enter the real part of complex number: ");
    scanf("%d",&a);
    
    printf("Enter the imaginary part of complex number: ");
    scanf("%d",&b);
    
    printf("\nComplex number is: %d%+di",a,b );
    
    return 0;
    
    }

    1. Report
  10. Question:C program to add two complex numbers 

    Answer
    #include<stdio.h>
    int main(){
      int a,b,c,d,x,y;
      printf("\nEnter the first complex number:");
      scanf("%d%d",&a,&b);
      printf("\nEnter the second complex number:");
      scanf("%d%d",&c,&d);
      if(b<0)
          printf("%d-i\n",a-b);
      else
          printf("d+i\n",a+b);
      if(d<0)
          printf("d-i\n",c-d);
      else
          printf("%d+i\n",c+d);
      printf("\nADDITION ");
      x=a+c;
      y=b+d;
      if(y>0)
          printf("%d-i%d",x,-y);
      else
          printf("%d+i%d",x,+y);
      printf("\n\nSUBTRACTION ");
      x=a-c;
      y=b-d;
      if(y<0)
          printf("%d-i%d",x,-y);
      else
          printf("%d+i%d",x,+y);
      return 0;
    }

    1. Report
Copyright © 2024. Powered by Intellect Software Ltd