VIVEKANANDAREDDY P
Assistant Professor,
Department of computer science
$ cc pgm95.c $ a.out Enter the name raj Enter the age 40 Enter the salary 4000000
/* * C program to illustrate how a file stored on the disk is read */#include <stdio.h>#include <stdlib.h>void main()
{FILE *fptr;
char filename[15];
char ch;
printf("Enter the filename to be opened \n");
scanf("%s", filename);
/* open the file for reading */fptr = fopen(filename, "r");
if (fptr == NULL)
{printf("Cannot open file \n");
exit(0);
}ch = fgetc(fptr);
while (ch != EOF)
{printf ("%c", ch);
ch = fgetc(fptr);
}fclose(fptr);
}$ cc pgm96.c $ a.out Enter the filename to be opened pgm95.c /* * C program to create a file called emp.rec and store information * about a person, in terms of his name, age and salary. */ #include <stdio.h> void main() { FILE *fptr; char name[20]; int age; float salary; fptr = fopen ("emp.rec", "w"); /* open for writing*/ if (fptr == NULL) { printf("File does not exists \n"); return; } printf("Enter the name \n"); scanf("%s", name); fprintf(fptr, "Name = %s\n", name); printf("Enter the age \n"); scanf("%d", &age); fprintf(fptr, "Age = %d\n", age); printf("Enter the salary \n"); scanf("%f", &salary); fprintf(fptr, "Salary = %.2f\n", salary); fclose(fptr); }
/* * C Program Delete a specific Line from a Text File */#include <stdio.h>int main()
{FILE *fileptr1, *fileptr2;
char filename[40];
char ch;
int delete_line, temp = 1;
printf("Enter file name: ");
scanf("%s", filename);
//open file in read modefileptr1 = fopen(filename, "r");
ch = getc(fileptr1);
` while (ch != EOF)
{printf("%c", ch);
ch = getc(fileptr1);
} //rewindrewind(fileptr1);
printf(" \n Enter line number of the line to be deleted:");
scanf("%d", &delete_line);
//open new file in write modefileptr2 = fopen("replica.c", "w");
ch = getc(fileptr1);
while (ch != EOF)
{ch = getc(fileptr1);
if (ch == '\n')
temp++; //except the line to be deletedif (temp != delete_line)
{ //copy all lines in file replica.cputc(ch, fileptr2);
} }fclose(fileptr1);
fclose(fileptr2);
remove(filename);
//rename the file replica.c to original namerename("replica.c", filename);
printf("\n The contents of file after being modified are as follows:\n");
fileptr1 = fopen(filename, "r");
ch = getc(fileptr1);
while (ch != EOF)
{printf("%c", ch);
ch = getc(fileptr1);
}fclose(fileptr1);
return 0;
}
$ cc pgm47.c $ a.out Enter file name: pgm1.c /* * C PROGRAM TO CONVERSION FROM Decimal to hexadecimal */ #include<stdio.h> int main() { long int decimalnum, remainder, quotient; int i = 1, j, temp; char hexadecimalnum[100]; printf("Enter any decimal number: "); scanf("%ld", &decimalnum); quotient = decimalnum; while (quotient != 0) { temp = quotient % 16; //To convert integer into character if (temp < 10) temp = temp + 48; else temp = temp + 55; hexadecimalnum[i++] = temp; quotient = quotient / 16; } printf("Equivalent hexadecimal value of decimal number %d: ", decimalnum); for (j = i - 1; j > 0; j--) printf("%c", hexadecimalnum[j]); return 0; } Enter line number of the line to be deleted: 10 The contents of file after being modified are as follows: * * C PROGRAM TO CONVERSION FROM Decimal to hexadecimal */ #include<stdio.h> int main() { long int decimalnum, remainder, quotient; int i = 1, j, temp; printf("Enter any decimal number: "); scanf("%ld", &decimalnum); quotient = decimalnum; while (quotient != 0) { temp = quotient % 16; //To convert integer into character if (temp < 10) temp = temp + 48; else temp = temp + 55; hexadecimalnum[i++] = temp; quotient = quotient / 16; } printf("Equivalent hexadecimal value of decimal number %d: ", decimalnum); for (j = i - 1; j > 0; j--) printf("%c", hexadecimalnum[j]); return 0; }
/* * C Program to Replace a specified Line in a Text File */#include <stdio.h>int main(void)
{FILE *fileptr1, *fileptr2;
char filechar[40];
char c;
int delete_line, temp = 1;
printf("Enter file name: ");
scanf("%s", filechar);
fileptr1 = fopen(filechar, "r");
c = getc(fileptr1);
//print the contents of file .while (c != EOF)
{printf("%c", c);
c = getc(fileptr1);
}printf(" \n Enter line number to be deleted and replaced");
scanf("%d", &delete_line);
//take fileptr1 to start point.rewind(fileptr1);
//open replica.c in write modefileptr2 = fopen("replica.c", "w");
c = getc(fileptr1);
while (c != EOF)
{if (c == 'n')
{ temp++; } //till the line to be deleted comes,copy the content to otherif (temp != delete_line)
{putc(c, fileptr2);
} else {while ((c = getc(fileptr1)) != 'n')
{ } //read and skip the line ask for new textprintf("Enter new text");
//flush the input streamfflush(stdin);
putc('n', fileptr2);
//put 'n' in new filewhile ((c = getchar()) != 'n')
putc(c, fileptr2);
//take the data from user and place it in new filefputs("n", fileptr2);
temp++; } //continue this till EOF is encounteredc = getc(fileptr1);
}fclose(fileptr1);
fclose(fileptr2);
remove(filechar);
rename("replica.c", filechar);
fileptr1 = fopen(filechar, "r");
//reads the character from filec = getc(fileptr1);
//until last character of file is encounteredwhile (c != EOF)
{printf("%c", c);
//all characters are printedc = getc(fileptr1);
}fclose(fileptr1);
return 0;
}$ cc pgm48.c $ a.out Enter file name: pgm3.c /* * C Program to Convert Octal to Decimal */ #include <stdio.h> #include <math.h> int main() { long int octal, decimal = 0; int i = 0; printf("Enter any octal number: "); scanf("%ld", &octal); while (octal != 0) { decimal = decimal +(octal % 10)* pow(8, i++); octal = octal / 10; } printf("Equivalent decimal value: %ld",decimal); return 0; } Enter line number to be deleted and replaced 13 replaced Enter new text /* * C Program to Convert Octal to Decimal */ #include <stdio.h> #include <math.h> int main() { long int octal, decimal = 0; int i = 0; replaced printf("Enter any octal number: "); scanf("%ld", &octal); while (octal != 0) { decimal = decimal +(octal % 10)* pow(8, i++); octal = octal / 10; } printf("Equivalent decimal value: %ld",decimal); return 0; }
* * C Program to Find the Number of Lines in a Text File */#include <stdio.h>int main()
{FILE *fileptr;
int count_lines = 0;
char filechar[40], chr;
printf("Enter file name: ");
scanf("%s", filechar);
fileptr = fopen(filechar, "r");
//extract character from file and store in chrchr = getc(fileptr);
while (chr != EOF)
{ //Count whenever new line is encounteredif (chr == 'n')
{count_lines = count_lines + 1;
} //take next character from file.chr = getc(fileptr);
}fclose(fileptr); //close file.
printf("There are %d lines in %s in a file\n", count_lines, filechar);
return 0;
}$ cc pgm49.c $ a.out Enter file name: pgm2.c There are 43 lines in pgm2.c in a file
/* * C Program to Append the Content of File at the end of Another */#include <stdio.h>#include <stdlib.h>main()
{FILE *fsring1, *fsring2, *ftemp;
char ch, file1[20], file2[20], file3[20];
printf("Enter name of first file ");
gets(file1);
printf("Enter name of second file ");
gets(file2);
printf("Enter name to store merged file ");
gets(file3);
fsring1 = fopen(file1, "r");
fsring2 = fopen(file2, "r");
if (fsring1 == NULL || fsring2 == NULL)
{perror("Error has occured");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}ftemp = fopen(file3, "w");
if (ftemp == NULL)
{perror("Error has occures");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}while ((ch = fgetc(fsring1)) != EOF)
fputc(ch, ftemp);
while ((ch = fgetc(fsring2) ) != EOF)
fputc(ch, ftemp);
printf("Two files merged %s successfully.\n", file3);
fclose(fsring1);
fclose(fsring2);
fclose(ftemp);
return 0;
}$ cc pgm47.c $ a.out Enter name of first file a.txt Enter name of second file b.txt Enter name to store merged file merge.txt Two files merged merge.txt successfully.
/* * C Program that Merges Lines Alternatively from 2 Files & Print Result */#include<stdio.h>main()
{char file1[10], file2[10];
puts("enter the name of file 1"); /*getting the names of file to be concatenated*/
scanf("%s", file1);
puts("enter the name of file 2");
scanf("%s", file2);
FILE *fptr1, *fptr2, *fptr3;
fptr1=fopen(file1, "r"); /*opening the files in read only mode*/
fptr2=fopen(file2, "r");
fptr3=fopen("merge2.txt", "w+"); /*opening a new file in write,update mode*/
char str1[200];
char ch1, ch2;
int n = 0, w = 0;
while (((ch1=fgetc(fptr1)) != EOF) && ((ch2 = fgetc(fptr2)) != EOF))
{if (ch1 != EOF) /*getting lines in alternately from two files*/
{ungetc(ch1, fptr1);
fgets(str1, 199, fptr1);
fputs(str1, fptr3);
if (str1[0] != 'n')
n++; /*counting no. of lines*/
}if (ch2 != EOF)
{ungetc(ch2, fptr2);
fgets(str1, 199, fptr2);
fputs(str1, fptr3);
if (str1[0] != 'n')
n++; /*counting no.of lines*/
} }rewind(fptr3);
while ((ch1 = fgetc(fptr3)) != EOF) /*countig no.of words*/
{ungetc(ch1, fptr3);
fscanf(fptr3, "%s", str1);
if (str1[0] != ' ' || str1[0] != 'n')
w++; }fprintf(fptr3, "\n\n number of lines = %d n number of words is = %d\n", n, w - 1);
/*appendig comments in the concatenated file*/fclose(fptr1);
fclose(fptr2);
fclose(fptr3);
}$ cc pgm51.c $ a.out enter the name of file 1 c.txt enter the name of file 2 a.txt $ vi merge2.txt All participants will be provided with 1:1 Linux Systems for Lab work. If participants want, they can bring their own laptops with Linux in it. This enable them to do lot of quality assignments even Sanfoundry internship programs are great learning opportunities. Students with proven credentials only are enrolled for this program and the duration of these programs ranges from 2-6 months full t after the classes are over. If they have Windows, we install virtualization software and Ubuntu Linux virtual appliance on top of windows system. ime. Student must be passionate about Technology topics. As part of Sanfoundry's biggest open learning initiative, we are looking for interns (student or working professional) in following technolog number of lines = 4 number of words is = 114
/* * C Program to List Files in Directory */#include <dirent.h>#include <stdio.h>int main(void)
{DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{while ((dir = readdir(d)) != NULL)
{printf("%s\n", dir->d_name);
}closedir(d);
}return(0);
}$ cc pgm59.c $ a.out . .. b.txt pgm2.c pgm5.c 1 a.out a.txt b.txt pgm.c pgm1.c pgm10.c pgm11.c pgm12.c pgm13.c pgm14.c pgm15.c
/* * C Program to Find Sum of Numbers given in Command Line Arguments * Recursively */#include <stdio.h>int count, s = 0;
void sum(int *, int *);
void main(int argc, char *argv[])
{int i, ar[argc];
count = argc;
for (i = 1; i < argc; i++)
{ar[i - 1] = atoi(argv[i]);
}sum(ar, ar + 1);
printf("%d", s);
}/* computes sum of two numbers recursively */void sum(int *a, int * b)
{if (count == 1)
return;
s = s + *a + *b;
count -= 2;
sum(a + 2, b + 2);
}$ cc arg4.c $ a.out 1 2 3 4 sum is 10
/* * C Program to Find the Size of File using File Handling Function */#include <stdio.h>void main(int argc, char **argv)
{FILE *fp;
char ch;
int size = 0;
fp = fopen(argv[1], "r");
if (fp == NULL)
printf("\nFile unable to open ");
else printf("\nFile opened ");
fseek(fp, 0, 2); /* file pointer at the end of file */
size = ftell(fp); /* take a position of file pointer un size variable */
printf("The size of given file is : %d\n", size);
fclose(fp);
}vi file10.c $ cc file10.c $ a.out myvmlinux File opened The size of given file is : 3791744
/* * C Program to Capitalize First Letter of every Word in a File */#include <stdio.h>#include <fcntl.h>#include <stdlib.h>int to_initcap_file(FILE *);
void main(int argc, char * argv[])
{FILE *fp1;
char fp[10];
int p;
fp1 = fopen(argv[1], "r+");
if (fp1 == NULL)
{printf("cannot open the file ");
exit(0);
}p = to_initcap_file(fp1);
if (p == 1)
{ printf("success");
} else {printf("failure");
}fclose(fp1);
}/* capitalizes first letter of every word */int to_initcap_file(FILE *fp)
{char c;
c = fgetc(fp);
if (c >= 'a' && c <= 'z')
{fseek(fp, -1L, 1);
fputc(c - 32, fp);
}while(c != EOF)
{if (c == ' ' || c == '\n')
{c = fgetc(fp);
if (c >= 'a' && c <= 'z')
{fseek(fp, -1L, 1);
fputc(c - 32, fp);
} } else {c = fgetc(fp);
} }return 1;
}$ cc file5.c $ a.out sample success $ cat sample Wipro Technologies File Copy Function Successfully Read
/* * C Program to Print Environment variables */#include <stdio.h>void main(int argc, char *argv[], char * envp[])
{int i;
for (i = 0; envp[i] != NULL; i++)
{ printf("\n%s", envp[i]);
}}$ cc arg7.c $ a.out HOSTNAME=localhost.localdomain SELINUX_ROLE_REQUESTED= SHELL=/bin/bash TERM=xterm HISTSIZE=1000 SSH_CLIENT=192.168.7.43 49162 22 SELINUX_USE_CURRENT_RANGE= QTDIR=/usr/lib64/qt-3.3 QTINC=/usr/lib64/qt-3.3/include SSH_TTY=/dev/pts/8 USER=harika LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36: PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/harika/bin:. MAIL=/var/spool/mail/harika PWD=/home/harika KDE_IS_PRELINKED=1 LANG=en_US.UTF-8 KDEDIRS=/usr SELINUX_LEVEL_REQUESTED= HISTCONTROL=ignoredups SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass HOME=/home/harika SHLVL=2 LOGNAME=harika CVS_RSH=ssh QTLIB=/usr/lib64/qt-3.3/lib SSH_CONNECTION=192.168.7.43 49162 192.168.7.140 22 LESSOPEN=|/usr/bin/lesspipe.sh %s G_BROKEN_FILENAMES=1 _=./a.out OLDPWD=/home
/* * C Program to Copy a File into Another File */#include <stdio.h>void main(int argc,char **argv)
{FILE *fp1, *fp2;
char ch;
int pos;
if ((fp1 = fopen(argv[1],"r")) == NULL)
{ printf("\nFile cannot be opened");
return;
} else {printf("\nFile opened for copy...\n ");
}fp2 = fopen(argv[2], "w");
fseek(fp1, 0L, SEEK_END); // file pointer at end of file
pos = ftell(fp1);
fseek(fp1, 0L, SEEK_SET); // file pointer set at start
while (pos--)
{ch = fgetc(fp1); // copying file character by character
fputc(ch, fp2);
} fcloseall();
}$ cc file1.c $ a.out /tmp/vmlinux mylinux File opened for copy... $cmp /tmp/vmlinux mylinux $ ls -l mylinux -rw-rw-r--. 1 adi adi 3791744 Jul 27 19:57 mylinux
/* * C Program to Create Employee Record and Update it */#include <stdio.h>#include <stdlib.h>#include <string.h>#define size 200struct emp{int id;
char *name;
}*emp1, *emp3;
void display();
void create();
void update();
FILE *fp, *fp1;
int count = 0;
void main(int argc, char **argv)
{int i, n, ch;
printf("1] Create a Record\n");
printf("2] Display Records\n");
printf("3] Update Records\n");
printf("4] Exit");
while (1)
{printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{case 1:
fp = fopen(argv[1], "a");
create();
break;
case 2:
fp1 = fopen(argv[1],"rb");
display();
break;
case 3:
fp1 = fopen(argv[1], "r+");
update();
break;
case 4:
exit(0);
} }}/* To create an employee record */void create()
{int i;
char *p;
emp1 = (struct emp *)malloc(sizeof(struct emp));
emp1->name = (char *)malloc((size)*(sizeof(char)));
printf("Enter name of employee : ");
scanf(" %[^\n]s", emp1->name);
printf("Enter emp id : ");
scanf(" %d", &emp1->id);
fwrite(&emp1->id, sizeof(emp1->id), 1, fp);
fwrite(emp1->name, size, 1, fp);
count++; // count to number of entries of records
fclose(fp);
}/* Display the records in the file */void display()
{ emp3=(struct emp *)malloc(1*sizeof(struct emp));
emp3->name=(char *)malloc(size*sizeof(char));
int i = 1;
if (fp1 == NULL)
printf("\nFile not opened for reading");
while (i <= count)
{fread(&emp3->id, sizeof(emp3->id), 1, fp1);
fread(emp3->name, size, 1, fp1);
printf("\n%d %s",emp3->id,emp3->name);
i++; }fclose(fp1);
free(emp3->name);
free(emp3);
}void update()
{int id, flag = 0, i = 1;
char s[size];
if (fp1 == NULL)
{printf("File cant be opened");
return;
}printf("Enter employee id to update : ");
scanf("%d", &id);
emp3 = (struct emp *)malloc(1*sizeof(struct emp));
emp3->name=(char *)malloc(size*sizeof(char));
while(i<=count)
{ fread(&emp3->id, sizeof(emp3->id), 1, fp1);
fread(emp3->name,size,1,fp1);
if (id == emp3->id)
{printf("Enter new name of emplyee to update : ");
scanf(" %[^\n]s", s);
fseek(fp1, -204L, SEEK_CUR);
fwrite(&emp3->id, sizeof(emp3->id), 1, fp1);
fwrite(s, size, 1, fp1);
flag = 1;
break;
} i++; }if (flag != 1)
{printf("No employee record found");
flag = 0;
}fclose(fp1);
free(emp3->name); /* to free allocated memory */
free(emp3);
}$ a.out emprec1 1] Create a Record 2] Display Records 3] Update Records 4] Exit Enter your choice : 1 Enter name of employee : aaa Enter emp id : 100 Enter your choice : 1 Enter name of employee : bbb Enter emp id : 200 Enter your choice : 1 Enter name of employee : ccc Enter emp id : 300 Enter your choice : 1 Enter name of employee : ddd Enter emp id : 400 Enter your choice : 1 Enter name of employee : eee Enter emp id : 500 Enter your choice : 2 100 aaa 200 bbb 300 ccc 400 ddd 500 eee Enter your choice : 3 Enter employee id to update : 300 Enter new name of emplyee to update : cdefgh Enter your choice : 2 100 aaa 200 bbb 300 cdefgh 400 ddd 500 eee Enter your choice : 4
/* * C Program to Compare two Binary Files, Printing the First Byte * Position where they Differ */#include <stdio.h>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 <filename1> <filename2>\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);
} }}/* * compare two binary files character by character */void compare_two_binary_files(FILE *fp1, FILE *fp2)
{char ch1, ch2;
int flag = 0;
while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF))
{ /* * character by character comparision * if equal then continue by comparing till the end of files */if (ch1 == ch2)
{flag = 1;
continue;
} /* * If not equal then returns the byte position */ 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);
}}$ gcc file15.c $ a.out /bin/chgrp /bin/chown Two files are not equal : byte poistion at which two files differ is 25 /* * Verify using cmp command */ $ cmp /bin/chgrp /bin/chown /bin/chgrp /bin/chown differ: byte 25, line 1 $ a.out a.out a.out Two files are Equal /* * Verify using cmp command */ $ cmp a.out a.out
/* * C Program to Convert the Content of File to UpperCase */#include <stdio.h>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 the status returned is 0 then the coversion of file content was completed successfully */if (status == 0)
{printf("\n The content of \"%s\" file was successfully converted to upper case\n",argv[1]);
return;
} /* * If the status returnes is -1 then the conversion of file content was not done */if (status == -1)
{printf("\n Failed to convert");
return;
} }}/* * convert the file content to uppercase */int to_upper_file(FILE *fp)
{char ch;
if (fp == NULL)
{perror("Unable to open file");
return -1;
} else { /* * Read the file content and convert to uppercase */while (ch != EOF)
{ch = fgetc(fp);
if ((ch >= 'a') && (ch <= 'z'))
{ch = ch - 32;
fseek(fp,-1,SEEK_CUR);
fputc(ch,fp);
} }return 0;
}}/* Input file : mydata $ cat mydata This is Manish I had worked in Wipro and Cisco */ $ gcc file3.c $ a.out mydata The content of "mydata" file was successfully converted to upper case /* "mydata" after conversion $ cat mydata THIS IS MANISH I HAD WORKED IN WIPRO AND CISCO */
/* * C Program to replace first letter of every word with caps */#include <stdio.h>#include <stdlib.h>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);
} } elsech = fgetc(fp1);
}return 1;
}
$ vi file5test $ cat file5test chandana ravella chanikya ravella sree lakshmi ravella sree ramulu ravella $ cc file5.c $ a.out file5test success$ cat file5test Chandana Ravella Chanikya Ravella Sree Lakshmi Ravella Sree Ramulu Ravella
} }fseek(fp1, 0, 0);
while ((ch = fgetc(fp1))! = EOF)
{if (ch == '/')
{if ((ch = fgetc(fp1)) == '/')
{ n_o_c_l++; } } }printf("Total no of lines: %d\n", line_count);
printf("Total no of comment line: %d\n", n_o_c_l);
printf("Total no of blank lines: %d\n", n_o_b_l);
printf("Total no of non blank lines: %d\n", line_count-n_o_b_l);
printf("Total no of lines end with semicolon: %d\n", n_e_c);
}$ cc file8.c $ a.out stack_ll.c Total no of lines: 204 Total no of comment line: 19 Total no of blank lines: 11 Total no of non blank lines: 193 Total no of lines end with semicolon: 66
/* * C Program to Reverse the Contents of a File and Print it */#include <stdio.h>#include <errno.h>long count_characters(FILE *);
void main(int argc, char * argv[])
{int i;
long cnt;
char ch, ch1;
FILE *fp1, *fp2;
if (fp1 = fopen(argv[1], "r"))
{printf("The FILE has been opened...\n");
fp2 = fopen(argv[2], "w");
cnt = count_characters(fp1); // to count the total number of characters inside the source file
fseek(fp1, -1L, 2); // makes the pointer fp1 to point at the last character of the file
printf("Number of characters to be copied %d\n", ftell(fp1));
while (cnt)
{ch = fgetc(fp1);
fputc(ch, fp2);
fseek(fp1, -2L, 1); // shifts the pointer to the previous character
cnt--; }printf("\n**File copied successfully in reverse order**\n");
} else {perror("Error occured\n");
}fclose(fp1);
fclose(fp2);
}// count the total number of characters in the file that *f points tolong count_characters(FILE *f)
{fseek(f, -1L, 2);
long last_pos = ftell(f); // returns the position of the last element of the file
last_pos++;return last_pos;
}$ gcc file12.c $ cat test2 The function STRERROR returns a pointer to an ERROR MSG STRING whose contents are implementation defined. THE STRING is not MODIFIABLE and maybe overwritten by a SUBSEQUENT Call to the STRERROR function. $ a.out test2 test_new The FILE has been opened.. Number of characters to be copied 203 **File copied successfully in reverse order** $ cat test_new .noitcnuf RORRERTS eht ot llaC TNEUQESBUS a yb nettirwrevo ebyam dna ELBAIFIDOM ton si GNIRTS EHT .denifed noitatnemelpmi era stnetnoc esohw GNIRTS GSM RORRE na ot retniop a snruter RORRERTS noitcnuf ehT $ ./a.out test_new test_new_2 The FILE has been opened.. Number of characters to be copied 203 **File copied successfully in reverse order** $ cat test_new_2 The function STRERROR returns a pointer to an ERROR MSG STRING whose contents are implementation defined. THE STRING is not MODIFIABLE and maybe overwritten by a SUBSEQUENT Call to the STRERROR function. $ cmp test test_new_2