Você está na página 1de 5

Week-12: Console I/O and File Handling 1.

Write a program to read a text file as input and count number of characters, words and lines in the file. #include <stdio.h> int main() { FILE *ptr; char line[81], ctr; int i, c, end = 0, characters = 0, words = 0, lines = 0; ptr = fopen("abc.c", "w"); printf("Enter text :"); while(end == 0) { c=0; while((ctr=getchar()) != '\n') { line[c++] = ctr; line[c] = '\0'; if(line[0] == '\0') break; else{ words++; for(i=0; line[i] != '\0'; i++) if(line[i] == ' ' || line[i] == '\t') words++; } lines = lines+1; characters = characters + strlen(line); } fclose(ptr); ptr = fopen("abc.c", "r"); printf("\n"); printf("Number of lines = %d\n", lines); printf("Number of words = %d\n", words); printf("Number of characters = %d\n", characters); fclose(ptr); return 0; }

2.

Write a program to copy a source text file into a target text file.

#include <stdio.h> #include <stdlib.h> int main(void) { char str[80] = "This is just some random text.\n"; FILE *fp; char *p; int i; /* open file3.txt for output */ if((fp = fopen("file3.txt", "w"))==NULL) {

printf("Cannot open file.\n"); exit(1);

/* write str to disk */ p = str; while(*p) { if(fputc(*p, fp)==EOF) { printf("Error writing file.\n"); exit(1); } p++; } fclose(fp); /* open file3.txt for input */ if((fp = fopen("file3.txt", "r"))==NULL) { printf("Cannot open file.\n"); exit(1); } /* read back the file */ for(;;) { i = fgetc(fp); if(i == EOF) break; putchar(i); } fclose(fp); return 0; }

3.

Modify the file copy program to avoid over writing the existing target file, instead if target file has some contents, then target file is appended by contents of source file. #include <stdio.h> #include <stdlib.h> int main(void) { char str[80] = "This is just some random text.\n"; FILE *fp; char *p; int i; /* open file3.txt for output */ if((fp = fopen("file3.txt", "w"))==NULL) { printf("Cannot open file.\n"); exit(1); }

/* write str to disk */ p = str; while(*p) { if(fputc(*p, fp)==EOF) { printf("Error writing file.\n"); exit(1); } p++; fclose(fp);

/* open file3.txt for input */ if((fp = fopen("file3.txt", "r"))==NULL) { printf("Cannot open file.\n"); exit(1); } /* read back the file */ for(;;) { i = fgetc(fp); if(i == EOF) break; putchar(i); } fclose(fp); return 0; }

5. What a program that copies an executable file into another duplicate file, test the copy by running duplicate file. To read a file in c #include<stdio.h> int main(){ FILE *ptr; //It is file pointer char c; //opening the file in read mode in Linux os ptr = fopen("/root/Test/abc.c","r"); //Opening the file in read mode in window os // ptr = fopen("C:\\Test\\abc.c","r"); //Checking the path or file is valid or not and printing message if(ptr == NULL) printf("Error in opening the file"); else printf("File has opened successfully in read mode"); //Reading the each characters from file and printing it.

do{ c = getc(ptr); //getting one character from file. printf("%c",c); //printing the one character in console window } while(c != EOF); //loop will terminate when end of file will reach. //closing the opened file fclose(ptr); return 0; } Open a file #include<stdio.h> int main(){ FILE *ptr; //It is file pointer //opening the file in read mode in Linux os ptr = fopen("/root/Test/abc.c","r"); //Opening the file in read mode in window os // ptr = fopen("C:\\Test\\abc.c","r"); //Checking the status and printing message if(ptr == NULL) printf("Error in opening the file"); else printf("File has opened successfully in read mode"); return 0; } //write a file include<stdio.h> int main(){ FILE *ptr; //It is file pointer char c; //opening the file in write mode in Linux os ptr = fopen("/root/Test/abc.txt","w"); //Opening the file in write mode in window os // ptr = fopen("C:\\Test\\abc.txt","r"); //Checking the path is valid or not and printing message if(ptr == NULL) printf("Error in opening the file"); else printf("File has opened successfully in read mode"); //Write text in the file printf("\nEnter text to write in the file and press Esc key to end : "); c = getchar(); while(c!= 27){ //27 is ASCII value of Esc key.

putc(c,ptr); //Printing one character in file. c = getchar(); //getting one character from keybord. //Closing the opened file fclose(ptr); return 0;

Você também pode gostar