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.
/* 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?
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.
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.