Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define WORD_SIZE 256
- #define WORD_MASK "%255s"
- #define INPUT_FILE "input.txt"
- #define OUTPUT_FILE "output.txt"
- void Write(char* path){
- // строка для записи
- char * message = "text file text file";
- // файл для записи
- // запись в файл
- FILE *fp = fopen(path, "w");
- if(!fp)
- {
- printf("Error occured while opening file\n");
- return 1;
- }
- // записываем строку
- fputs(message, fp);
- fclose(fp);
- }
- void Read(char* path){
- // буфер для считавания данных из файла
- char buffer[256];
- // чтение из файла
- FILE *fp = fopen(path, "r");
- if ( ! ( fp = fopen(path, "r") ) ) {
- fprintf(stderr, "Can't open %s for output!\n", path);
- exit(1);
- }
- // пока не дойдем до конца, считываем по 256 байт
- while((fgets(buffer, 256, fp))!=NULL)
- {
- printf("%s", buffer);
- }
- fclose(fp);
- }
- void Replace(){
- FILE *fin, *fout;
- if ( ! ( fin = fopen(INPUT_FILE, "r") ) ) {
- fprintf(stderr, "Can't open %s for output!\n", INPUT_FILE);
- exit(1);
- }
- if ( ! ( fout = fopen(OUTPUT_FILE, "w") ) ) {
- fprintf(stderr, "Can't open %s for output!\n", OUTPUT_FILE);
- exit(1);
- }
- char wordToRemove[WORD_SIZE], wordToInsert[WORD_SIZE], currentWord[WORD_SIZE];
- printf("Word to remove: ");
- scanf(WORD_MASK, wordToRemove);
- printf("Word to insert: ");
- scanf(WORD_MASK, wordToInsert);
- while ( fscanf(fin, WORD_MASK, currentWord) == 1 ) {
- if ( fprintf(fout, "%s ", ( strcmp(currentWord, wordToRemove) == 0) ? wordToInsert : currentWord) < 1 ) {
- fprintf(stderr, "Can't write to output file!\n");
- exit(1);
- }
- }
- fclose(fin);
- fclose(fout);
- }
- int main(void){
- Read(INPUT_FILE);
- printf("\n");
- Write(INPUT_FILE);
- Replace();
- Read(OUTPUT_FILE);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment