ফাইল থেকে তথ্য ব্যবহার করবো কিভাবে?

* কোনো file  এর information :

Rahim = 20;

Karim =25;

Jabbar =13;

Rakib =27;


 একটি programme  তৈরি করবো 

#include

 int main()

{  char name[10];


   printf("Enter A Student's Name : ") ;

   scanf(" %s",&name);

  ............................................................ 

  ............................................................

  return 0;

}

এখন  scan করা উক্ত  নাম এর  মান file থেকে  print করবো কিভাবে ? 

2532 views

1 Answers

  1. // file থেকে read

  2. #include
  3. #include
  4.  
  5. int main()
  6. {
  7.    char ch, file_name[25];
  8.    FILE *fp;
  9.  
  10.    printf("Enter name of a file you wish to see\n");
  11.    gets(file_name);
  12.  
  13.    fp = fopen(file_name, "r"); // read mode
  14.  
  15.    if (fp == NULL)
  16.    {
  17.       perror("Error while opening the file.\n");
  18.       exit(EXIT_FAILURE);
  19.    }
  20.  
  21.    printf("The contents of %s file are:\n",file_name);
  22.  
  23.    while((ch = fgetc(fp)) != EOF)
  24.       printf("%c", ch);
  25.  
  26.    fclose(fp);
  27.    return 0;
  28. }
2532 views Answered

Related Questions

Next steps