0

I'm reopening and reading a file, SORTED.txt, after closing it from its first time use - copying all the contents of another file, UNSORTED.txt. After copying from UNSORTED.txt, I wanted to count the number of lines of what I've copied (as a seperate process, and not during the copy process). It seems that fegtc() does not point at the beginning of the file (SORTED.txt) the second time over, hence the value of lines remains as what it was initialized as, 0. Also, in general, can I get the repointing of fgetc() done without closing and reopening the file in consideration?

Grateful for any help.

Cheers!

  f = fopen("./TEXTFILES/UNSORTED.txt", "w");
  if (f == NULL){
      printf("ERROR opening file\n");
      return 100;
  }

  for (i=0; i<1000000; i++){
    fprintf(f, "%d\n", (23*rand()-rand()/13));
  }
  fclose(f);

  f = fopen("./TEXTFILES/UNSORTED.txt", "r");
  if (f == NULL){
    return 100;
  }
  s = fopen("./TEXTFILES/SORTED.txt", "w");
  if (s == NULL){
    return 101;
  }

  while(1){
    j = getc(f);
    if (j == EOF) break;
    fputc(j, s);
  }
  fclose(f);
  //Closed source file. Read number of lines in target file.
  fclose(s);
  s = fopen("./TEXTFILES/SORTED.txt", "w");
  j = 0;

  while(1){
    j = fgetc(s);
    if (j == EOF) break;
    if (j == '\n') lines++;
  }

  fclose(s);
  printf("\n%d\n", lines);
bomp
  • 309
  • 1
  • 4
  • 12
  • use `rewind(s)` to go back to the beginning of the file [reference link](http://www.cplusplus.com/reference/clibrary/cstdio/rewind/) – rkyser Jul 25 '12 at 13:54

2 Answers2

3

You are opening the file in "w" (write) mode:

s = fopen("./TEXTFILES/SORTED.txt", "w");

but reading from it:

    j = fgetc(s);

You probably meant to open it in read mode:

s = fopen("./TEXTFILES/SORTED.txt", "r");
                                    ^^^
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • Ah. It works with "r". I had it in "w" because I wanted to write/overwrite in the same stage. Do i have to close it after "r" mode and then reopen with "w". Any other way of going about it? – bomp Jul 25 '12 at 13:56
  • Take a look at the 'parameters' section [here](http://www.cplusplus.com/reference/clibrary/cstdio/fopen/). You should be able to use "w+" for read and writing to a new file. But if the file already exists, it will overwrite it. – rkyser Jul 25 '12 at 13:59
  • After you write to the file, are you `rewind()`ing? [This](http://www.cplusplus.com/reference/clibrary/cstdio/rewind/) contains an example of opening a file for writing, writing, rewinding, then using fread to read it back. – rkyser Jul 25 '12 at 14:06
  • @resonant_fractal, I don't know what you mean, in your code, you are only counting the number of lines and not writing anything. If you want to read and write at the same time, you may want to look at `"a+"`. I haven't ever used it though, so do some experiment to see how it works. – Shahbaz Jul 25 '12 at 14:12
  • @rkyser Sorry for goofing around. Committed a mistake when I tried your suggestion the first time. It's working mate! This pretty much clears the whole thing about the closing and reopening effort! Thanks a megaton! Much love! – bomp Jul 25 '12 at 14:17
  • @Shahbaz Haven't included the bit where I change the file contents after counting the lines, thinking it would be unnecessary. I've tried rkyser's approach and it works nicely. Thanks for the help mate! – bomp Jul 25 '12 at 14:20
2

It sounds like you've got it figured out! But since I went through the effort of putting this example together, I thought I'd post it anyways.

#include <stdio.h> 

int main()
{
    FILE * f;
    FILE * s;
    int i, j;
    int lines = 0;

    f = fopen("./TEXTFILES/UNSORTED.txt", "w+");
    if (f == NULL){
        printf("ERROR opening file\n");
        return 100;
    }

    for (i=0; i<1000000; i++){
        fprintf(f, "%d\n", (23*rand()-rand()/13));
    }

    s = fopen("./TEXTFILES/SORTED.txt", "w+");
    if (s == NULL){
        fclose(f); // cleanup and close UNSORTED.txt
        return 101;
    }

    // rewind UNSORTED.txt here for reading back
    rewind( f );

    while(1){
        j = getc(f);
        if (j == EOF) break;
        fputc(j, s);
    }

    // done with UNSORTED.txt. Close it.
    fclose(f);

    // rewind SORTED.txt here for reading back
    rewind( s );
    j = 0;

    while(1){
        j = fgetc(s);
        if (j == EOF) break;
        if (j == '\n') lines++;
    }

    fclose(s);

    printf("\n%d\n", lines);

    return 0;
}
rkyser
  • 3,241
  • 1
  • 19
  • 28