简体   繁体   中英

Read one file and write to another C

I am trying to read from files and write to a temp file. However, I am stuck in an infinite loop right now. The function below is called multiple times by a recursive function that goes through directories to read files.

My approach is that I would read each word from one file, then those words to another file.

The function I have works fine if I just print out each word. It prints out each word in each file in all directories. However, when I try to start writing to a temp file (the code commented out), I am stuck in the while loop.

On the other hand, if I just call the function once in a test program where I just read from one file in the current directory, and write to a temp file, it's fine.

This is what I have ( fileName when passed in is actually the absolute path, and I do ../tmp so it does not get caught in the recursion function):

void fileReadWrite(char *pattern, char *before, char *replace, char *fileName) {
   FILE *file = fopen(fileName, "r");

   if (file != NULL) {
      int ch, word = 0;
      while ((ch = fgetc(file)) != EOF) {
         if (isspace(ch) || ispunct(ch)) {
            if (word) {
               word = 0;
               putchar('\n');
            }
         }
         else {
            word = 1;
            putchar(ch);

            /*
            FILE *f = fopen("../tmp", "wb"); // create and write
            if (f == NULL)
            {    
               printf("Error opening file!\n");       
               exit(1);
            }

            fprintf(f, "Some text"); // Or fprintf(f, ch);

            fclose(f);
            */
         }
      }
      fclose(file);
   }
}

There's nothing in your code that suggests an infinite loop. However, if fileName is very large, you could be opening and closing "..\\tmp" millions of times. As Joachim Pileborg points out in the comments, you should open that file just once at the beginning of your function, and close it again at the end.

If you want to convince yourself that you are not in an infinite loop, print out the value of ch on each iteration.

Okay so I did this and it worked. But I don't understand why though. Can someone explain it please?

void fileReadWrite(char *pattern, char *before, char *replace, char *fileName) {
   FILE *file = fopen(fileName, "r");
   FILE *f = fopen("../tmp", "wb"); // MOVE HERE

   if (file != NULL) {
      int ch, word = 0;
      while ((ch = fgetc(file)) != EOF) {
         if (isspace(ch) || ispunct(ch)) {
            if (word) {
               word = 0;
               putchar('\n');
            }
         }
         else {
            word = 1;
            putchar(ch);

            /*

            if (f == NULL)
            {    
               printf("Error opening file!\n");       
               exit(1);
            }

            fprintf(f, "Some text"); // Or fprintf(f, ch);


            */
         }
      }
      fclose(file);
      fclose(f); // MOVE HERE
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM