Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

Problem with Loops and File Handling in C

Can't figure out the solution to solve the infinite iteration
Thinking of having done wrong the application of fgetc() function but still don't know how to solve.

This program basically finds a word in a given file and then changes it with what the user/programmer wants.

Here are the main and the solving function:

int main(int argc, char **argv)
{
   FILE *fPtr = NULL;

   newFile(&fPtr, "ReadFile.txt");
   writeFile("ReadFile.txt", "This is not a Random Access File
Now just see what happens!");
   readFile("ReadFile.txt");

   replaceWord("ReadFile.txt", "happens!", "happened here!
 Did you see it?");
   readFile("ReadFile.txt");

   return 0;
}
//^ Problem -> Infinite Loop
int findWordPosition(const char *fileName, const char word[]) {
   FILE * fPtr = NULL;

   fPtr = fopen(fileName, "r");
   check(fPtr);
   int lenght = strlen(word);

   int position;
   int i, j = 0;
   char ch;

   //^ (- Problem Here -) Can't figure i get an infinite loop 
   for (i = 0; (ch = fgetc(fPtr)) != EOF || j < lenght; ++i) {
      puts("Hello");
      if (ch == word[j]) {
         if (j == 0) { 
            position = i;
         }
         else if (j == (lenght - 1)) {
            puts("
Word Found");
            return position;
         }
         ++j;
      }
      else {
         j = 0;
      }
   }
   puts("
Word not found!");
   return 0;
}


Here is the entire code

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

void check(FILE *fPtr);
int findWordPosition(const char *fileName, const char word[]);

void newFile(FILE **fPtr, const char *fileName);
void writeFile(const char *fileName, const char *string);
void readFile(const char *fileName);
void replaceWord(const char *fileName, const char *word, const char *newWord);

int main(int argc, char **argv)
{
   FILE *fPtr = NULL;

   newFile(&fPtr, "ReadFile.txt");
   writeFile("ReadFile.txt", "This is not a Random Access File
Now just see what happens!");
   readFile("ReadFile.txt");
   
   replaceWord("ReadFile.txt", "happens!", "happened here!
 Did you see it?");
   readFile("ReadFile.txt");

   return 0;
}

void check(FILE *fPtr) {
   if (!fPtr) { puts("File Insesistente"); exit(1); }
}

void newFile(FILE **fPtr, const char *fileName) {
   *fPtr = fopen(fileName, "w");
   check(*fPtr);

   fclose(*fPtr);
}

void writeFile(const char *fileName, const char *string) {  
   char *s;
   FILE *fPtr = NULL;

   fPtr = fopen(fileName, "a");
   check(fPtr);
   
   fprintf(fPtr, "%s", string);

   fclose(fPtr);
}

void readFile(const char *fileName) {
   FILE * fPtr = NULL;
   char ch;
   
   fPtr = fopen(fileName, "r");
   check(fPtr);

   while ((ch = fgetc(fPtr)) != EOF) {
      printf("%c", ch);
   }

   fclose(fPtr);
}

//^ Problem -> Infinite Loop
int findWordPosition(const char *fileName, const char word[]) {
   FILE * fPtr = NULL;
   
   fPtr = fopen(fileName, "r");
   check(fPtr);
   int lenght = strlen(word);
   
   int position;
   int i, j = 0;
   char ch;

   //^ (- Problem Here -) Can't figure i get an infinite loop 
   for (i = 0; (ch = fgetc(fPtr)) != EOF || j < lenght; ++i) {
      puts("Hello");
      if (ch == word[j]) {
         if (j == 0) { 
            position = i;
         }
         else if (j == (lenght - 1)) {
            puts("Word found");
            return position;
         }
         ++j;
      }
      else {
         j = 0;
      }
   }
   puts("
Word not found");
   return 0;
}

void replaceWord(const char *fileName, const char *word, const char *newWord) {
   FILE *fPtr = NULL;
   int position;
   
   fPtr = fopen(fileName, "w+");
   check(fPtr);
   
   position = findWordPosition(fileName, word);

   fseek(fPtr, position, SEEK_SET);
   fputs(newWord, fPtr);
   fprintf(fPtr, "%s", newWord);
}
question from:https://stackoverflow.com/questions/65835913/problem-with-loops-and-file-handling-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...