Professional Documents
Culture Documents
curr = getc(fptr1);
if (curr != EOF)
{
line_number = 1;
}
while (1)
{
if (del != line_number)
putc(curr, fptr2);
curr = getc(fptr1);
if (curr == '\n')
line_number++;
if (curr == EOF)
break;
}
fclose(fptr1);
fclose(fptr2);
}
Output:
Please enter the line number you want to delete : 2
2) C Program to Append the Content of File at the end of Another
#include <stdlib.h>
int main()
{
FILE *fsring1, *fsring2, *ftemp;
char ch, new1[20], new2[20], new3[20];
Output:
Enter name of first file new1.txt
Enter name of second file new2.txt
Enter name to store merged file merge.txt
Two files merged new3.txt successfully.
PROGRAM:
#include <stdio.h>
#include <string.h>
fp = fopen("includehelp.txt", "w");
if (fp == NULL)
return;
char ch = 0;
int cnt = 0;
fp = fopen("includehelp.txt", "r");
if (fp == NULL)
return;
ch = fgetc(fp);
while (ch != EOF)
{
str[cnt++] = ch;
ch = fgetc(fp);
}
str[cnt] = 0;
fclose(fp);
}
int main()
{
char wrtStr[32] = "this is india";
char readStr[32];
int flg = 1;
int cnt = 0;
writeData(wrtStr);
readData(readStr);
while (readStr[cnt] != 0)
{
if (flg == 1 && readStr[cnt] != 0x20)
{
readStr[cnt] = readStr[cnt] - 32;
flg = 0;
}
cnt++;
}
writeData(readStr);
return 0;
}
Output:
Original data from file: i am Harsh Kumar
Update data from file: I Am Harsh Kumar
#include <stdio.h>
int main()
{
FILE *fpoint1, *fpoint2;
char fchar1, fchar2;
char name1[20], name2[20];
printf("\n Give the Existing file name to Read:");
gets(name1);
printf("\n Give the file name for the lower case of existing file
contents :");
gets(name2);
fpoint1 = fopen(name1, "r");
fpoint2 = fopen(name2, "w");
if (fpoint1 == NULL)
printf("\n The file %s has no content !", name1);
else
{
do
{
fchar1 = getc(fpoint1);
fchar2 = tolower(fchar1);
fputc(fchar2, fpoint2);
} while (fchar1 != EOF);
fcloseall();
}
return (0);
}
Output:
Give the Existing file name to Read:new1
Give the file name for the lower case of existing file contents :new2
The file new1 has no content !
5) C Program to Join Lines of Two given Files and Store them in a New
file.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
char ch;
int flag;
if (file1 == NULL)
{
perror("Error Occured!");
}
if (file2 == NULL)
{
perror("Error Occured!");
}
if (target == NULL)
{
perror("Error Occured!");
}
joinfiles(file1, file2, target); /* Calling Function */
if (flag == 1)
{
{
{
if (ch == EOF)
{
break;
}
else
{
fputc(ch, target);
}
}
{
if (ch == EOF)
{
break;
}
else
{
fputc(ch, target);
}
}
fputc('\n', target);
}
fclose(file1);
fclose(file2);
fclose(target);
return flag = 1;
}
Ouput:
Error Occured!: Bad address
Error Occured!: No such file or directory
Error Occured!: No such file or directory
Output:
Enter file path: desktop/new.txt
File contents before removing all empty lines.
I am Aditya Mishra
I am learning C programming
Programming with files is fun.
Learning C programming is interesting
7) C program to input numbers from file and write even, * odd and
prime numbers to separate file.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fPtrIn,
*fPtrEven,
*fPtrOdd,
*fPtrPrime;
fclose(fPtrIn);
fclose(fPtrEven);
fclose(fPtrOdd);
fclose(fPtrPrime);
return 0;
}
if (NUM % i == 0)
{
return 0;
}
}
return 1;
}
Output:
File opened successfully. Reading integers from file.