Very Simple code why is it not working

I have a text file TestFile1.txt and a veru simple code that came came as an example at cPlusPlus.com

I tried fscanf to get the data out of my file
but I cant get it to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* fscanf example */
#include <stdio.h>

int main ()
{
    int x;
  char str [4];
  long f;
  FILE * pFile;

  pFile = fopen ("TestFile1.txt","r");
  rewind (pFile); 
  for(x = 0 ; x < 20; x++)
  {
      fscanf (pFile, "%l", &f);
      fscanf (pFile, "%s", str);
  }
  fclose (pFile);
  printf ("I have read: %f and %s \n",f,str);
  return 0;
}
What's the problem? What about it isn't working?
1. This is the content of the text file "TestFile1.txt.
Text File Example:  Each record is one line of arbi,t,rary length (ends with '\n'):
What!  Another useless, stupid, and unnecessary program?
Yes;  What else?:  Try input r.e,d:i;r?e!c=t*i/o\n.  /*.*/  /*.!?,;:=+*/
/* Using C */
#define REC_NO 9		/* number of record to read */

char temp[BIG_ENOUGH?]; /* array for hol;ding any record */
int i;

for (i = 0; i < REC_NO; ++i)	/* for first unneeded records */
	if (fsc?anf(fp, "%*[^\n]%*c") == EOF) {   /* read and throw away */
		fputs("Unexpected EOF\n", stderr);   /* there is no rec.ord REC_NO */
		exit(EXIT_FAILURE);		/* error exit */
	}
if (fg:ets(temp, sizeof(temp), fp) == NULL) {	/* read record REC_NO */
	fputs("Unexp!ected EOF\n", stderr);   /* there is no rec.ord REC_NO */
	exit(EXIT_FAILURE);   /* error exit */
}
/* At this point (?) record # REC_NO is in temp. */


2. And this is the equivalent of the TestFile1.txt in Hexadecimal.
>db /count:256 0x00573fe8
0x00573FE8  54 65 78 74 20 46 69 6c 65 20 45 78 61 6d 70 6c  
0x00573FF8  65 3a 20 20 45 61 63 68 20 72 65 63 6f 72 64 20  
0x00574008  69 73 20 6f 6e 65 20 6c 69 6e 65 20 6f 66 20 61  
0x00574018  72 62 69 2c 74 2c 72 61 72 79 20 6c 65 6e 67 74  
0x00574028  68 20 28 65 6e 64 73 20 77 69 74 68 20 27 5c 6e  
0x00574038  27 29 3a 0a 57 68 61 74 21 20 20 41 6e 6f 74 68  
0x00574048  65 72 20 75 73 65 6c 65 73 73 2c 20 73 74 75 70  
0x00574058  69 64 2c 20 61 6e 64 20 75 6e 6e 65 63 65 73 73  
0x00574068  61 72 79 20 70 72 6f 67 72 61 6d 3f 0a 59 65 73  
0x00574078  3b 20 20 57 68 61 74 20 65 6c 73 65 3f 3a 20 20  
0x00574088  54 72 79 20 69 6e 70 75 74 20 72 2e 65 2c 64 3a  
0x00574098  69 3b 72 3f 65 21 63 3d 74 2a 69 2f 6f 5c 6e 2e  
0x005740A8  20 20 2f 2a 2e 2a 2f 20 20 2f 2a 2e 21 3f 2c 3b  
0x005740B8  3a 3d 2b 2a 2f 0a 2f 2a 20 55 73 69 6e 67 20 43  
0x005740C8  20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 43 5f  
0x005740D8  4e 4f 20 39 09 09 2f 2a 20 6e 75 6d 62 65 72 20  

>

3. This is what is on the WATCH1 WINDOW when I debug the program.
Variable  has a value of      -858993460 and is type long 
		f = 	-858993460	long

note that the equivalent of  -858993460 in hexadecimal is  FFFFFFFFCCCCCCCC


4. I Modified the Program and put "printf" so I can display the variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* fscanf example */
#include <stdio.h>

int main ()
{
    int x;
  char str [4];
  long f;
  FILE * pFile;

  pFile = fopen ("TestFile2.txt","r");
  rewind (pFile); 
  for(x = 0 ; x < 20; x++)
  {
      fscanf (pFile, "%l", &f);
      printf ("LONG VALUE IS: %lx\n", f);
      fscanf (pFile, "%s\n", str);
      printf ("ARRAY VALUE IS: %s\n", str);
  }
  fclose (pFile);
  printf ("I have read: %f and %s \n",f,str);
  return 0;
}



5. When I ran the program I get this result:

LONG VALUE IS: cccccccc
ARRAY VALUE IS: Text
LONG VALUE IS: cccccccc
ARRAY VALUE IS: FILE
LONG VALUE IS: cccccccc
ARRAY VALUE IS: Example
LONG VALUE IS: cccccccc
ARRAY VALUE IS: Each
LONG VALUE IS: cccccccc
ARRAY VALUE IS: record 


6. If you look the the TestFile1.txt you will see that only the fscanf of the character is working and the fscanf of the Long is not.

Why is the fscanf of the LONG not working, as far as the compiler is concerned the content of the File is just a bunch of hexadecimal values. I tried the program on a .txt but the original file is to be used is a .bin.

Anyway, I change the file to a .bin add "r+b" on the mode and remove the fscanf of char because it is causinga problem and the result is the same

LONG VALUE IS: cccccccc


The homework requires me to read every 4 bytes of a binary file, which is equivalent to 32 bits and then display the long equivalent when the LSB 22 bits is the exponent and the MSB last 9 bits is the mantisa and the 32nd bit is the sign bit.

I got the program running, on a hardcoded values of the data. now all I need is read the actual data from the binary file and it is kicking my butt.

7. Is there anybody who has dealt with this problem before. How to read a type Long from a binary file?
http://www.cplusplus.com/reference/clibrary/cstdio/fopen/


Reference material:
With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").


Does this help? I am not sure whether you are wanting to reading in text mode or binary - you seem to be doing both.

What are you going to do with the long value of 4 chars - that is 4 bytes? It looks like a long is 4 chars on your system.


Also fscanf returns a value which is the number of items successfully scanned. You should make use of that to see if it worked.

HTH
Last edited on
To The IdeasMan:

Before i go further, let me thank you for the help.

1. I am not sure whether you are wanting to reading in text mode or binary - you seem to be doing both.

Actually, the actual data that the professor wants is coming from a binary file. The reason I used a text file to test the code is because, I can always use "Notepad" to look on the content of the text file, I can't do that in binary. So at least I have a way of confirming if I am reading the file correctly.

2. What are you going to do with the long value of 4 chars - that is 4 bytes? It looks like a long is 4 chars on your system.

A   real number that is 32 bits long use the first 23 least Significant bit(LSB) as exponent and the last 8 Most significant bit(MSB) as mantiza and of course the last MSB, bit 32 as the sign bit.  
The assignment requires us to change the specification, 22 LSB for exponent and 9 for mantiza and still bit 32 for sign bit.
I have the main body written and tested using hardcoded data. when I am satisfied with the result all I needed was get the test data from a binary file. 



3.fscanf returns a value which is the number of items successfully scanned. You should make use of that to see if it worked.

somebody suggested fread which is almost the same as fscanf. I tested both and discovered another problem. I need 32 bits for a data. but there is no guarantee that when I reached the EOF I can get 4 bytes. It just so happen that my professor knows it and intentionally leave 2 bytes to be read at the end.  Anyway, I have to execute fread or fscanf before I get a return on how many bytes I read. when I find that I did not read enought bytes, I execute an exit procedure, that is when I get"STACK IS CORRUPTED". It is obvious to me that it will happen, my problem now is how to exit gracefully when I read data short of 4 bytes.
You're using fscanf. fscanf is for formatted extraction from text files, in other words it treats all data read in as if it were text. This is true whether you open the file in binary or text mode.

So, you need to do two things. Open it in binary, and use fread.

my problem now is how to exit gracefully when I read data short of 4 bytes.


fread has a return value. Make use of it.
Topic archived. No new replies allowed.