0

I'm trying to read two records form a file, where one is hexadecimal formated number. Well I'm newcomer to C, before when I been reading hexadecimal, generated by ftok(), I just used printf("%x", key) and it worked fine. Now when I try to read it from the file, it does not work that way.

So my code looks like this:

int countRecords(FILE *f_p) {
  int tmp_key = 0;
  int tmp_msgqid = 0;
  int n = 0;

  while (!feof(f_p)) {
    if (fscanf(f_p, "%x %i", &tmp_key, &tmp_msgqid) != 2)
      break;  
    n = n + 1;
  }

  return n;
}

Later on i read this value in the code like:

printf("Records: %i \n", countRecords(f_msgList));

And this compiles with no warnings. Anyway when I run the program the value of countRecords(f_msgList) is 0, when the file have a bunch of data in it:

5a0203ff 360448
850203ff 393217
110203ff 425986

EDIT: Here is the code where the file is opened or created:

FILE *f_msgList;
f_msgList = fopen("../message_queues.list", "a");

// if file does not exist then create one and check for errors
if (f_msgList == NULL) {
  FILE *f_tmp;
  f_tmp = fopen("../message_queues.list", "w");
  if (f_msgList == NULL) {
    fprintf(stderr, "Error occurred while creating the file! \n");
    exit(1);
  } else
    f_msgList = f_tmp;
}
siery
  • 467
  • 3
  • 20
  • how are you opening the file? are you testing that the file could be opened properly? – Jean-François Fabre Nov 22 '17 at 20:17
  • 3
    Please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). Use the next line to control the loop: `while(fscanf(f_p, "%x %i", &tmp_key, &tmp_msgqid) == 2) { n++; }` – Weather Vane Nov 22 '17 at 20:20
  • Your second code don't make sense, please include a [mcve] to your question. – Stargateur Nov 22 '17 at 23:29
  • Did my answer help solving your problem.? – Jay Joshi Nov 24 '17 at 06:40
  • Yes, thank you for pointing me to redirect the file pointer. It's sometimes problematic to find even such trivial problem when you look at few hundreds lines of code. Finally, i write macros: `#define fread fopen("path", "r")`, and the same for append. And just initialize it in the code like: `fp = fread [...] fp = fappend`. – siery Nov 24 '17 at 06:51

1 Answers1

1

Problems

  • You opened the file in "append" mode. which does not let you read through the file.
  • If you want to write and then read the file, file pointer must be reset to the starting of the file.
  • feof(f_p) is worst way of checking whether file pointer is at end of the file.

Solution

  • Open File in "read" mode by 'r' or in append+read mode 'a+'.
  • if you are writing in to the file. reset it using rewind(f_p); after writing.
  • check out this way to read through the file :

     int ret, ans, key;
     while ((ret = fscanf(fp, "%x %i", &key, &ans))) {
        if (ret == EOF)
            break;
        else
            printf("%x %i \n",key, ans);
     }
    

    here integer ret is :

    • EOF, if the pointer is reached end of file.
    • 0, if no input matched with the variable
    • (greater than 0), that is, number of matched variables with the file input
Jay Joshi
  • 868
  • 8
  • 24
  • Using C++ to print and C to read is peculiar. It would be better to use `while ((ret = fscanf(fp, "%x %i", &key, &ans)) == 2)` in the loop; you might not need `ret` after that. – Jonathan Leffler Nov 23 '17 at 15:56
  • Thank you for the suggestions.! :) – Jay Joshi Nov 24 '17 at 06:39
  • I just wonder.. why are people sometimes using %d where there is no chance of getting an double-precision value? Is that just because of usability over optimization? – siery Nov 24 '17 at 09:08
  • well, My main motive was to show you the way to scan from file properly. hence i did not put much effort in printing because it was just for confirmation that it was scanned properly. So, You can take it as a mistake. Thanks for the edit btw :) – Jay Joshi Nov 24 '17 at 09:14