Bharathiar University
int main(int argc,char * argv[])
{
FILE *ptr1, *ptr2;
char ch;
int letters=0,words=0,lines=0;
// Opening file1 in reading mode
ptr1 = fopen(argv[1], "r");
if (ptr1 == NULL) {
printf("file1 can't be opened \n");
}
// Opening file2 in write mode
ptr2 = fopen("C:\\Users\\Admin\\Desktop\\sample.txt", "w");
if (ptr2 == NULL) {
printf("file2 can't be opened \n");
}
// copying contents from one to another file
while((ch= fgetc(ptr1)) != EOF){
//ch = fgetc(ptr1);
fputc(ch,ptr2);
//count characters
letters++;
/* Check new line */
if (ch == '\n' || ch == '\0')
lines++;
/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
if(letters>0)
{
words++;
lines++;
}
fprintf(ptr2,"\nTotal Number of Characters:");
fprintf(ptr2,"%d",letters);
fprintf(ptr2,"\nTotal Number of Words:");
fprintf(ptr2,"%d",words);
fprintf(ptr2,"\nTotal Number of Lines:");
fprintf(ptr2,"%d",lines);
printf("Contents written in sample.txt file successfully");
// Closing the file
fclose(ptr1);
fclose(ptr2);
getch();
return 0;
}
To set Command line arguments
Run menu --> Arguments
Command line Argument with file location
Source Code
//Program without File Location
#include <stdio.h>int main(int argc,char * argv[])
{
FILE *ptr1, *ptr2;
char ch;
int letters=0,words=0,lines=0;
// Opening file1 in reading mode
ptr1 = fopen(argv[1], "r");
if (ptr1 == NULL) {
printf("file1 can't be opened \n");
}
// Opening file2 in write mode
ptr2 = fopen("sample.txt", "w");
if (ptr2 == NULL) {
printf("file2 can't be opened \n");
}
// copying contents from one to another file
while((ch= fgetc(ptr1)) != EOF){
//ch = fgetc(ptr1);
fputc(ch,ptr2);
//count characters
letters++;
/* Check new line */
if (ch == '\n' || ch == '\0')
lines++;
/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
if(letters>0)
{
words++;
lines++;
}
fprintf(ptr2,"\nTotal Number of Characters:");
fprintf(ptr2,"%d",letters);
fprintf(ptr2,"\nTotal Number of Words:");
fprintf(ptr2,"%d",words);
fprintf(ptr2,"\nTotal Number of Lines:");
fprintf(ptr2,"%d",lines);
printf("Contents written in sample.txt file successfully");
// Closing the file
fclose(ptr1);
fclose(ptr2);
getch();
return 0;
}
int main(int argc,char * argv[])
{
FILE *ptr1, *ptr2;
char ch;
int letters=0,words=0,lines=0;
// Opening file1 in reading mode
ptr1 = fopen(argv[1], "r");
if (ptr1 == NULL) {
printf("file1 can't be opened \n");
}
// Opening file2 in write mode
ptr2 = fopen("sample.txt", "w");
if (ptr2 == NULL) {
printf("file2 can't be opened \n");
}
// copying contents from one to another file
while((ch= fgetc(ptr1)) != EOF){
//ch = fgetc(ptr1);
fputc(ch,ptr2);
//count characters
letters++;
/* Check new line */
if (ch == '\n' || ch == '\0')
lines++;
/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
if(letters>0)
{
words++;
lines++;
}
fprintf(ptr2,"\nTotal Number of Characters:");
fprintf(ptr2,"%d",letters);
fprintf(ptr2,"\nTotal Number of Words:");
fprintf(ptr2,"%d",words);
fprintf(ptr2,"\nTotal Number of Lines:");
fprintf(ptr2,"%d",lines);
printf("Contents written in sample.txt file successfully");
// Closing the file
fclose(ptr1);
fclose(ptr2);
getch();
return 0;
}
No comments:
Post a Comment