Write a C program to remove comments from a file(C program file).
#include <stdio.h>
#include <string.h>
#define MAX 256
int main() {
int ch, i = 0, flag = 0, prev = '\0';
FILE *fp1, *fp2;
char fname[MAX], temp[] = "temp.txt";
/* get the input file name from the user */
printf("Enter your file name:");
scanf("%s", fname);
/* open the input file in read mode */
fp1 = fopen(fname, "r");
/* error handling */
if (!fp1) {
printf("Unable to open file name!!\n");
return 0;
}
/* open the temporary file in write mode */
fp2 = fopen(temp, "w");
/* error handling */
if (!fp2) {
printf("Unable to open temporary file!!\n");
return 0;
}
#include <string.h>
#define MAX 256
int main() {
int ch, i = 0, flag = 0, prev = '\0';
FILE *fp1, *fp2;
char fname[MAX], temp[] = "temp.txt";
/* get the input file name from the user */
printf("Enter your file name:");
scanf("%s", fname);
/* open the input file in read mode */
fp1 = fopen(fname, "r");
/* error handling */
if (!fp1) {
printf("Unable to open file name!!\n");
return 0;
}
/* open the temporary file in write mode */
fp2 = fopen(temp, "w");
/* error handling */
if (!fp2) {
printf("Unable to open temporary file!!\n");
return 0;
}
/* removes comments from the given input file */
prev = fgetc(fp1);
while ((ch = fgetc(fp1)) != EOF) {
/* flag is 1 - double slash comment */
if (flag == 1) {
/* skip the contents until you detect \n */
if (ch == '\n') {
flag = 0;
prev = fgetc(fp1);
}
continue;
}
/* flag is 2 - slash arsterix comment */
if (flag == 2) {
/* skip the contents until you detect asterix slash */
if (ch == '/' && prev == '*') {
flag = 0;
prev = fgetc(fp1);
}
continue;
}
/* checking for double slash comment */
if (ch == '/' && prev == '/') {
flag = 1;
} else if (prev == '/' && ch == '*') {
/* slash asterix comment */
flag = 2;
} else {
/* contents outside of comments */
fputc(prev, fp2);
}
prev = ch;
}
/* closing the input file */
fclose(fp1);
fclose(fp2);
/* removing the input file */
remove(fname);
/* rename the temporary file */
rename(temp, fname);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ cat pgm.c
/* program to add two numbers */
#include <stdio.h>
int main() {
// variable declaration
int a, b, res;
/* sum of two numbers */
res = a + b;
// printing the result
printf("%d", res);
/*
* finding subtraction and division
* of two numbers here
*/
// subtraction value
printf("Sub value: %d\n", a - b);
// division value
printf("Div value: %d\n", a / b);
return 0;
}
jp@jp-VirtualBox:~/$ ./a.out
Enter your file name:pgm.c
jp@jp-VirtualBox:~/$ cat pgm.c
#include <stdio.h>
int main() {
int a, b, res;
res = a + b;
printf("%d", res);
printf("Sub value: %d\n", a - b);
printf("Div value: %d\n", a / b);
return 0;
}
/* program to add two numbers */
#include <stdio.h>
int main() {
// variable declaration
int a, b, res;
/* sum of two numbers */
res = a + b;
// printing the result
printf("%d", res);
/*
* finding subtraction and division
* of two numbers here
*/
// subtraction value
printf("Sub value: %d\n", a - b);
// division value
printf("Div value: %d\n", a / b);
return 0;
}
jp@jp-VirtualBox:~/$ ./a.out
Enter your file name:pgm.c
jp@jp-VirtualBox:~/$ cat pgm.c
#include <stdio.h>
int main() {
int a, b, res;
res = a + b;
printf("%d", res);
printf("Sub value: %d\n", a - b);
printf("Div value: %d\n", a / b);
return 0;
}
SEE ALSO
- c program to write a string into a file
- c program to read numbers from a file and write even, odd and prime numbers in separate files
- c program to create a file and store "hello world" in it
- c program to read the contents of the given file
- c program to delete all blank lines in a file
- c program to check whether a directory exists or not
- c program to check whether a file exists or not
- c program to copy a file from one location to another
- c program to create, read, edit and close a file
- c program to create a file with input content
- c program to read a file line by line
- c program to compare two files character by character
- c program to concatenate two files
- c program to convert lowercase characters in a file to uppercase
- c program to convert uppercase characters in a file to lowercase
- c program to list all files in a directory
- c program to list all files in a directory recursively
- c program to append data into a file
- c program to count number of lines in a file
- c program to delete a file or directory
- c program to print the source code of itself as output
- c program to convert the contents in a file from lowercase to uppercase and vice versa
- c program to merge two files
- c program to move a file to different location
- c program to replace articles with space in a text file
- c program to print the words in a file starting with the given character
- c program to delete given word in a file
- c program to replace a word in a file
- c program to merge alternate lines from two files
- c program to remove numbers in a file
- c program to check end of file
- c program to compare two files line by line
- c program to delete specific line from a file
- c program to replace specific line in a file
- c program to take input from a file
- c program to delete a record from a file
- c program to find the number of character, words and lines in a file
- c program to sort characters in each words of a file
- c program to convert text file to binary
- c program to split a file to multiple files
- c program to find the file type, permission, size and last modification date of the given file
- c program to encrypt and decrypt contents of a file
- c program to eliminate comments from a file
No comments:
Post a Comment