Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // output file with the following characters converted to be html safe
- // " "
- // ' '
- // & &
- // < <
- // > >
- #pragma warning(disable:4996)
- #include <stdio.h>
- #include <stdlib.h>
- #define FALSE 0
- #define TRUE 1
- int main(int argc, char* argv[])
- {
- char ch;
- FILE* read;
- // optional append file, else just pipe the stream in cmd: reader.exe read.txt > output.txt
- int appending = FALSE;
- FILE* append;
- if (argc == 2)
- {
- read = fopen(argv[1], "r");
- append = NULL;
- }
- else if (argc > 2) {
- read = fopen(argv[1], "r");
- append = fopen(argv[2], "a");
- appending = TRUE;
- }
- else {
- perror("Expected read file.\n");
- exit(EXIT_FAILURE);
- }
- if (read == NULL)
- {
- perror("Error while opening the file.\n");
- exit(EXIT_FAILURE);
- }
- if (appending == TRUE)
- {
- if (append == NULL)
- {
- perror("Error while opening the file.\n");
- exit(EXIT_FAILURE);
- }
- }
- while((ch = fgetc(read)) != EOF)
- {
- switch(ch){
- case '<':
- if(appending == TRUE)
- fputs("<", append);
- else
- printf("<");
- break;
- case '>':
- if (appending == TRUE)
- fputs(">", append);
- else
- printf(">");
- break;
- case '&':
- if (appending == TRUE)
- fputs("&", append);
- else
- printf("&");
- break;
- case '"':
- if (appending == TRUE)
- fputs(""", append);
- else
- printf(""");
- break;
- case '\'':
- if (appending == TRUE)
- fputs("'", append);
- else
- printf("'");
- break;
- default:
- if (appending == TRUE)
- fputc(ch, append);
- else
- printf("%c", ch);
- }
- }
- fclose(read);
- if (appending == TRUE)
- fclose(append);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment