Write a C program for File input-output with a Pointer Using Recursion.

recursiveToThree(n)
    print n + 1, "th"
    if n < 3
        r = recursiveToThree(n + 1)
        n = r
    return n

Function Main()
    n = 0
    n = recursiveToThree(0)

    arr = [1, 2, 3]

    ptr = address of arr[2]
    set value at ptr to 5

    d_arry = allocate memory for 3 integers

    pd_arr[0] = allocate memory for 2 integers
    pd_arr[1] = allocate memory for 2 integers

    print "Hello, world!"

    free memory at pd_arr[0]

    open file "PLIVET.txt" for writing as fp
    write "PLIVET" to fp
    close fp

    open file "PLIVET.txt" for reading as fp
    buf = create buffer with size 7
    while read up to 10 characters from fp into buf is not NULL
        print buf
    close fp
End

#include<stdio.h>
int recursiveToThree(int n){
  printf("%d th\n", n + 1);
  if(n < 3){
      int r = recursiveToThree(n + 1);
      n = r;
  }
  return n;
}
int main(){
  int n = 0;//variable declaration

  n = recursiveToThree(0);//recursive function

  int arr[5] = {1, 2, 3};//array variable

  int* ptr = &arr[2];//pointer variable
  *ptr = 5;

  //dynamic memory allocation
  int* d_arry = malloc(sizeof(int) * 3);

  //two-dimensional dynamic array
  int* pd_arr[2];
  pd_arr[0] = malloc(sizeof(int) * 2);
  pd_arr[1] = malloc(sizeof(int) * 2);

  printf("Hello,world!\n");//standard output

  free(pd_arr[0]);//memory leak

  //File Output
  {
    FILE* fp=NULL;
    fp = fopen("PLIVET.txt", "w");
    fputs("PLIVET", fp);
    fclose(fp);
  }

  //File Input
  {
    FILE* fp=NULL;
    char buf[7];
    fp = fopen("PLIVET.txt", "r");
    while(fgets(buf,10,fp) != NULL) {
      printf("%s",buf);
    }
    fclose(fp);
  }
  return 0;
}

1 th
2 th
3 th
4 th
Hello,world!
PLIVET  

Run this code in PLIVET or Others.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.