Standard I/O streams....fopen fprintf fscanf fclose

  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

Hi ppl,

I got a little problem of reading data from the file...just a little problem I guess but I really don't know where to start fixing it
I use this to write into a file
Code: [ Select ]
void Send_field1(filename, field)
char *filename;
FIELD2N *field;
{
INDEX i;
FILE *fptr;
if ((fptr = fopen(filename, "a")) == NULL)
printf("Error opening file\n");
printf("before printing\n");
SUMLOOP(i) fprintf(fptr, "%8x ", field->e[i]);
if (fclose(fptr) == EOF)
printf("Error closing file\n");
}
  1. void Send_field1(filename, field)
  2. char *filename;
  3. FIELD2N *field;
  4. {
  5. INDEX i;
  6. FILE *fptr;
  7. if ((fptr = fopen(filename, "a")) == NULL)
  8. printf("Error opening file\n");
  9. printf("before printing\n");
  10. SUMLOOP(i) fprintf(fptr, "%8x ", field->e[i]);
  11. if (fclose(fptr) == EOF)
  12. printf("Error closing file\n");
  13. }

where SUMLOOP is a loop from i=0 to the number of entries in the array, don't worry about that one, it's clean. :wink:

and I wrote sth into the file, yes prefectly fine and I opened it and see this
Code: [ Select ]
  547d baf7930d 3c21e169 a860ceee   547d baf7930d 3c21e169 a860ceee

yes hexademcial...
and I wrote this to read it back
Code: [ Select ]
void Receive_field1(filename, field)
char *filename;
FIELD2N *field;
{
INDEX i;
FILE *fptr;
if ((fptr = fopen(filename, "r")) == NULL)
printf("Error opening file\n");
printf("before reading\n");
SUMLOOP(i) {fscanf(fptr, "%8x ", &field->e[i]); printf("field->e[i] %8x\n", &field->e[i]);}
if (fclose(fptr) == EOF)
printf("Error closing file\n");
}
  1. void Receive_field1(filename, field)
  2. char *filename;
  3. FIELD2N *field;
  4. {
  5. INDEX i;
  6. FILE *fptr;
  7. if ((fptr = fopen(filename, "r")) == NULL)
  8. printf("Error opening file\n");
  9. printf("before reading\n");
  10. SUMLOOP(i) {fscanf(fptr, "%8x ", &field->e[i]); printf("field->e[i] %8x\n", &field->e[i]);}
  11. if (fclose(fptr) == EOF)
  12. printf("Error closing file\n");
  13. }


u can see I tried to print out the thing that I got....and all I got is this......
Code: [ Select ]
field->e[i] 241ff00
field->e[i] 241ff04
field->e[i] 241ff08
field->e[i] 241ff0c
  1. field->e[i] 241ff00
  2. field->e[i] 241ff04
  3. field->e[i] 241ff08
  4. field->e[i] 241ff0c


Anyone knows what's gonig on here? 8O
Thanz!! :D
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I am not sure what is going on yet, but I do notice that

Code: [ Select ]
field->e[i] 241ff00
field->e[i] 241ff04
field->e[i] 241ff08
field->e[i] 241ff0c
  1. field->e[i] 241ff00
  2. field->e[i] 241ff04
  3. field->e[i] 241ff08
  4. field->e[i] 241ff0c

is increasing each time by exactly 0x4
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

O hey !! good point!!
so.....is this....simply an address or what?
my codes r hard to understand huh? haha
but i really don't know where is the problem.....it's just a smiple fscanf... :shock:
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

OK now i get this
Code: [ Select ]
field->e[i] 77c39348
field->e[i]  223b20
field->e[i]  223b08
field->e[i]    8
  1. field->e[i] 77c39348
  2. field->e[i]  223b20
  3. field->e[i]  223b08
  4. field->e[i]    8

after changing the &field to field coz &field is simply the address...but still the data read from the file is not the same as that write into it
what is wrong here? :shock:
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

o...I think I didnlt make it very clear hehe
actually I want a array with four entries to input the data read from the feil
so the array with have the first entry as
Code: [ Select ]
547d

the second being
Code: [ Select ]
baf7930d
the third being
Code: [ Select ]
3c21e169
and the last being
Code: [ Select ]
a860ceee


The number of entries vary so I can't really define it....
so........any idea?
Thanz!! :D
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

O....I think it might be due to the pointer problems....
ok here's the new version of the functions, actually they're about the same
the writing function is like this
Code: [ Select ]
void Send_field1(filename, field)
char *filename;
FIELD2N *field;
{
INDEX i;
FILE *fptr;
if ((fptr = fopen(filename, "a")) == NULL)
printf("Error opening file\n");
printf("before printing\n");
SUMLOOP(i) fprintf(fptr, "%04x ", field->e[i]);
if (fclose(fptr) == EOF)
printf("Error closing file\n");
}
  1. void Send_field1(filename, field)
  2. char *filename;
  3. FIELD2N *field;
  4. {
  5. INDEX i;
  6. FILE *fptr;
  7. if ((fptr = fopen(filename, "a")) == NULL)
  8. printf("Error opening file\n");
  9. printf("before printing\n");
  10. SUMLOOP(i) fprintf(fptr, "%04x ", field->e[i]);
  11. if (fclose(fptr) == EOF)
  12. printf("Error closing file\n");
  13. }

and the reading part is like this
Code: [ Select ]
void Receive_field1(filename, field)
char *filename;
FIELD2N *field;
{
INDEX i;
FILE *fptr;
if ((fptr = fopen(filename, "r")) == NULL)
printf("Error opening file\n");
printf("before reading\n");
SUMLOOP(i) {fscanf(fptr, "%x ", &field->e[i]); printf("field->e[i] %04x\n", field->e[i]);}
if (fclose(fptr) == EOF)
printf("Error closing file\n");
}
  1. void Receive_field1(filename, field)
  2. char *filename;
  3. FIELD2N *field;
  4. {
  5. INDEX i;
  6. FILE *fptr;
  7. if ((fptr = fopen(filename, "r")) == NULL)
  8. printf("Error opening file\n");
  9. printf("before reading\n");
  10. SUMLOOP(i) {fscanf(fptr, "%x ", &field->e[i]); printf("field->e[i] %04x\n", field->e[i]);}
  11. if (fclose(fptr) == EOF)
  12. printf("Error closing file\n");
  13. }

I wrote sth into the file and the wriing part is perfectly ok as before
I wrote
Code: [ Select ]
685 43c6fffc e02152ec 87fd5959

yes a bit messy, it's a random hexadecimal

But when I tried to see what the readinf function read from the file...I got this
Code: [ Select ]
77c39348  223b20  223b08    8

so it is still not reading the right thing

And here's some of the declarations I made beforehand
Code: [ Select ]
typedef struct {
  ELEMENT  e[MAXLONG];
} FIELD2N;
  1. typedef struct {
  2.   ELEMENT  e[MAXLONG];
  3. } FIELD2N;

where MAXLONG = 4
and that SUMLOOP is defined as
Code: [ Select ]
#define SUMLOOP(i)  for(i=0; i<MAXLONG; i++)

simply a way to define the loop which will be frequenylt used in the program :D

So anyone got any idea about where it went wrong? :shock:
Thanz!! :D
  • frankly
  • Beginner
  • Beginner
  • frankly
  • Posts: 52

Post 3+ Months Ago

O.......ok hehe.........I think I finally solved that....and I'm teribly sorry...coz that was just a typo of the "filename" that I set. So yes u guyz shouldn't have noticed that coz I didn't post that part here.....took me long to realise that too

Thanz anywayz!! :D

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 59 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.