// C program to implement// the above approach#include<stdio.h>#include<stdlib.h>#include<string.h>// Driver codeintmain(){
FILE* ptr;char ch;// Opening file in reading mode
ptr =fopen("test.txt","r");if(NULL== ptr){printf("file can't be opened
");}printf("content of this file are
");// Printing what is written in file// character by character using loop.do{
ch =fgetc(ptr);printf("%c", ch);// Checking if character is not EOF.// If it is EOF stop eading.}while(ch !=EOF);// Closing the filefclose(ptr);return0;}
char* buffer =0;long length;
FILE * f =fopen(filename,"rb");if(f){fseek(f,0,SEEK_END);
length =ftell(f);fseek(f,0,SEEK_SET);
buffer =malloc(length);if(buffer){fread(buffer,1, length, f);}fclose(f);}if(buffer){// start to process your data / extract strings here...}
#include<stdio.h>main(){
FILE *fp;
fp =fopen("/tmp/test.txt","w+");fprintf(fp, "This is testing for fprintf...
");fputs("This is testing for fputs...
", fp);fclose(fp);}#https://moneyconvert.net/
#include<stdio.h>#include<stdlib.h>intmain(){int num;
FILE *fptr;if((fptr =fopen("C:program.txt","r"))==NULL){printf("Error! opening file");// Program exits if the file pointer returns NULL.exit(1);}fscanf(fptr,"%d",&num);printf("Value of n=%d", num);fclose(fptr);return0;}
#include<fcntl.h>#include<stdio.h>#include<unistd.h>#defineMAXLENGTH256intdisplay(char* FILE){int f =open(FILE, O_RDONLY);// we open the file in read onlyif(f ==-1){// open() returns -1 if there's an errorperror("open");close(f);return1;}else{char buffer[MAXLENGTH];int r =read(f,&buffer, MAXLENGTH);if(r ==-1){// read() returns -1 if there's an errorperror("read");close(f);return2;}else{write(1, buffer, r);close(f);return0;}}}intmain(void){display("file.txt");return0;}
FILE *in_file =fopen("name_of_file","r");// read only
FILE *out_file =fopen("name_of_file","w");// write only // test for files not existing. if(in_file ==NULL|| out_file ==NULL){printf("Error! Could not open file
");exit(-1);// must include stdlib.h }// write to file vs write to screen fprintf(file, "this is a test %d
", integer);// write to file fprintf(stdout, "this is a test %d
", integer);// write to screen printf( "this is a test %d
", integer);// write to screen // read from file/keyboard. remember the ampersands! fscanf(file,"%d %d",&int_var_1,&int_var_2);fscanf(stdin,"%d %d",&int_var_1,&int_var_2);scanf("%d %d",&int_var_1,&int_var_2);