Skip to content

Instantly share code, notes, and snippets.

@winhtut
Last active February 2, 2020 16:10
Show Gist options
  • Save winhtut/d98954bc13405ce79cf8553304cc4a5d to your computer and use it in GitHub Desktop.
Save winhtut/d98954bc13405ce79cf8553304cc4a5d to your computer and use it in GitHub Desktop.
Reading Data From File
#include<stdio.h>
int main(){
FILE *fptr;
if((fptr=fopen("clients.txt","w"))==NULL){
puts("File could not be opened");
}
else{
puts("Enter the account , age , name , balance");
puts("Enter EOF to end input.");
printf(">:");
unsigned int account;
int age;
char name[30];
double balance;
scanf("%d%d%20s%lf",&account , &age , name , &balance);
while(!feof(stdin)){
fprintf(fptr,"%d %d %s %.2f \n",account ,age, name , balance);
printf(">:");
scanf("%d%d%20s%lf",&account ,&age , name , &balance);
}
}
fclose(fptr);
return 0;
}
#include <stdio.h>
int main() {
FILE * fptr;
char c;
printf("__Reading all data From File ___\n");
fptr = fopen("001testw.txt", "r");
while ((c = getc(fptr)) != EOF) {
printf("%c", c);
}
fclose(fptr);
return 0;
}
#include <stdio.h>
int main() {
FILE * fptr;
char data_buffer[40];
fptr = fopen("1hello.txt", "r");
printf("__Reading a line From File__\n");
fgets(data_buffer,50,fptr);
printf("%s\n",data_buffer);
fclose(fptr);
return 0;
}
#include <stdio.h>
int main() {
FILE * fptr;
char data1[20],data2[20],data3[20],data4[20];
fptr = fopen("1hello.txt", "r");
printf("__Reading datas parse by parse From File__\n");
fscanf(fptr,"%s %s %s %s ",data1,data2,data3,data4);
printf("Reading a string :%s \n",data1);
printf("Reading a string :%s \n",data2);
printf("Reading a string :%s \n",data3);
printf("Reading a string :%s \n",data4);
fclose(fptr);
return 0;
}
#include <stdio.h>
int main() {
FILE *fptr;
fptr= fopen("1hello.txt","r+");
char data1[20],data4[20];
int code=0;
float balance=0.0;
printf("________Reading data parse by parse from specific file______\n");
fscanf(fptr , "%d %s %f %s",&code,data1,&balance,data4);
printf("Reading data from file 1 st %d\n",code);
printf("Reading data from file 2 st %s\n",data1);
printf("Reading data from file 3 st %.2f\n",balance);
printf("Reading data from file 4 st %s\n",data4);
fclose(fptr);
return 0;
}
@STEPHEN1995-programmer
Copy link

good teacher

@winhtut
Copy link
Author

winhtut commented Feb 2, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment