Compare two Binary Files,
Printing the First Byte Position where they Differ
#include
#include
void compare_two_binary_files(FILE *,FILE *);
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
if (argc < 3)
{
printf("\nInsufficient Arguments: \n");
printf("\nHelp:./executable \n");
return;
}
else
{
fp1 = fopen(argv[1], "r");
if (fp1 == NULL)
{
printf("\nError in opening file %s", argv[1]);
return;
}
fp2 = fopen(argv[2], "r");
if (fp2 == NULL)
{
printf("\nError in opening file %s", argv[2]);
return;
}
if ((fp1 != NULL) && (fp2 != NULL))
{
compare_two_binary_files(fp1, fp2);
}
}
}
void compare_two_binary_files(FILE *fp1, FILE *fp2)
{
char ch1, ch2;
int flag = 0;
while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF))
{
if (ch1 == ch2)
{
flag = 1;
continue;
}
else
{
fseek(fp1, -1, SEEK_CUR);
flag = 0;
break;
}
}
if (flag == 0)
{
printf("Two files are not equal : byte poistion at which two files differ is %d\n", ftell(fp1)+1);
}
else
{
printf("Two files are Equal\n ", ftell(fp1)+1);
}
}
Output
Two files are not equal : byte poistion at which two files differ is 25
$ cmp /bin/chgrp /bin/chown
/bin/chgrp /bin/chown differ: byte 25, line 1
Two files are Equal
Convert the Content of
File to UpperCase
#include
#include
int to_upper_file(FILE *);
int main(int argc,char *argv[])
{
FILE *fp;
int status;
if (argc == 1)
{
printf("Insufficient Arguments:");
printf("No File name is provided at command line");
return;
}
if (argc > 1)
{
fp = fopen(argv[1],"r+");
status = to_upper_file(fp);
if (status == 0)
{
printf("\n The content of \"%s\" file was successfully converted to upper case\n",argv[1]);
return;
}
if (status == -1)
{
printf("\n Failed to convert");
return;
}
}
}
int to_upper_file(FILE *fp)
{
char ch;
if (fp == NULL)
{
perror("Unable to open file");
return -1;
}
else
{
while (ch != EOF)
{
ch = fgetc(fp);
if ((ch >= 'a') && (ch <= 'z'))
{
ch = ch - 32;
fseek(fp,-1,SEEK_CUR);
fputc(ch,fp);
}
}
return 0;
}
}
Output
The content of "mydata" file was successfully converted to upper case
Replace First Letter of
every Word with Capital Letter
#include
#include
#include
void main(int argc, char *argv[])
{
FILE *fp1;
int return_val;
if ((fp1 = fopen(argv[1],"r+")) = = NULL)
{
printf("file cant be opened");
exit(0);
}
return_val = init_cap_file(fp1);
if (return_val == 1)
{
printf("\nsuccess");
}
else
{
printf("\n failure");
}
}
int init_cap_file(FILE *fp1)
{
char ch;
ch = fgetc(fp1);
if (ch >= 97 && ch <= 122)
{
fseek(fp1, -1L, 1);
fputc(ch - 32, fp1);
}
while (ch != EOF)
{
if (ch = = ' '|| ch == '\n')
{
ch = fgetc(fp1);
if (ch >= 97 && ch <= 122)
{
fseek(fp1, -1L, 1);
fputc(ch - 32, fp1);
}
}
else
ch = fgetc(fp1);
}
return 1;
}
Output
chandana ravella
chanikya ravella
sree lakshmi ravella
sree ramulu ravella
success$ cat file5test
Chandana Ravella
Chanikya Ravella
Sree Lakshmi Ravella
Sree Ramulu Ravella
No comments:
Post a Comment