Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- #include<string.h>
- FILE * createNewFile(char fname[20]){
- FILE *fpt;
- fpt = fopen(fname,"w");
- if(fpt==NULL){
- printf("Cannot open file!\n");
- exit(0);
- }
- return fpt;
- }
- FILE * openToAddFile(char fname[20]){
- FILE *fpt;
- fpt = fopen(fname,"a");
- if(fpt==NULL){
- printf("Cannot open file!\n");
- exit(0);
- }
- return fpt;
- }
- FILE * openToReadFile(char fname[20]){
- FILE *fpt;
- fpt = fopen(fname,"r");
- if(fpt==NULL){
- printf("Cannot open file!\n");
- exit(0);
- }
- return fpt;
- }
- void printAll(FILE *fpt){
- char ch;
- ch = getc(fpt);
- while(ch!=EOF){
- printf("%c",ch);
- ch = getc(fpt);
- }
- }
- int countChar(FILE *fpt){
- char ch;
- int num =0;
- ch = getc(fpt);
- while(ch!=EOF){
- num++;
- ch = getc(fpt);
- }
- return num;
- }
- void writeLineToFile(FILE *fpt, char str[30]){
- fprintf(fpt,"%s\n",str);
- }
- void readLineFromFile(FILE *fpt){
- char str[30];
- fscanf(fpt,"%s",str);
- if(feof(fpt)){
- printf("Empty\n");
- }else{
- printf("%s\n",str);
- }
- }
- void printWordFromFile(FILE *fpt){
- char str[30];
- do{
- fscanf(fpt,"%s",str);
- if(!feof(fpt)) printf("%s\n",str);
- }while(!feof(fpt));
- }
- int main(){
- FILE *fpt;
- fpt = createNewFile("test.txt");
- writeLineToFile(fpt,"12345 aa ccc ssss");
- writeLineToFile(fpt,"987565 \t XXX ZZ\nZ");
- fclose(fpt);
- fpt = openToReadFile("test.txt");
- printWordFromFile(fpt);
- fclose(fpt);
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement