how to pass hex value to fputc

S

Summu82

Hi i have to write some data in a binary file

fputc(0x22,f2);

this works fine

also
int data =0x22;

fputc(0x22,f2);
works fine

but
int data;
data =0x22;
fputc(0x22,f2);
gives error

i dont know the data before hand actually i have to comput timestamp
find the year and then put it into this like 06 for 2006 and then
similarly for month and date

so please suggest how can i achiev something like

int data;
timestamp1= getyear();

data =0xtimestamp1;

fputc(data,f2);


Thanks & Regards
sumeet
 
I

Ian Collins

Summu82 said:
Hi i have to write some data in a binary file

fputc(0x22,f2);

this works fine

also
int data =0x22;

fputc(0x22,f2);
works fine

but
int data;
data =0x22;
fputc(0x22,f2);
gives error
You'd better ask the question again with the correct code and the exact
error. You have used the exact same line for the working and not
working case.

Post a real example, with what you expect to see and what you get.
 
S

Summu82

oh big mistake from my side the catual code is as follows

fputc(0x22,f2);
this works fine
also
int data =0x22;
fputc(data,f2);
works fine
but
int data;
data =0x22;
fputc(data,f2);
gives error

my complete code is as follows

#include<stdio.h>

void main(){

int ret,ret2;



/*typedef unsigned char byte;
byte data1;
data1 = 0x22;*/
int data1;

//data1 =(int)(0x33);


FILE *f,*f2;

if((f=fopen("/user/gur11263/ccodes/MA_AMA20060427104636.ama","rb"))==NULL)
{
printf("could not open file"); // print
exit(1);
}

if((f2=fopen("/user/gur11263/ccodes/New.ama","wb"))==NULL) {
printf("could not open file"); // print
exit(1);
}

ret =getc(f);

//for(int i=0;i<46;i++)
while(ret!=EOF)
{
//printf("%x\n",ret);
fputc(ret,f2);
ret=getc(f);
}


fclose(f); // close the filepointer */

ret2 = fseek(f2,47,SEEK_SET);
printf("%d\n",ret2);
printf(" zero means seek successful\n");

fputc(0x22,f2);

ret2 = fseek(f2,78,SEEK_CUR); // 78 coz 1 ama is 79 in length and
while writing at 47 it autoamtically updates to 48
printf("%d\n",ret2);
printf(" zero means seek successful\n");

fputc(0x22,f2);

fclose(f2); // close the filepointer */


}
 
I

Ian Collins

Summu82 said:
oh big mistake from my side the catual code is as follows

fputc(0x22,f2);
this works fine
also
int data =0x22;
fputc(data,f2);
works fine
but
int data;
data =0x22;
fputc(data,f2);
gives error
What sort of error, compile or runtime?

The code looks OK.
my complete code is as follows

#include<stdio.h>
You should have said:
void main(){
should be int main(void)
int ret,ret2;



/*typedef unsigned char byte;
byte data1;
data1 = 0x22;*/
int data1;

//data1 =(int)(0x33);


FILE *f,*f2;

if((f=fopen("/user/gur11263/ccodes/MA_AMA20060427104636.ama","rb"))==NULL)
{
printf("could not open file"); // print
Why the superfluous comments?
 
B

Barry Schwarz

oh big mistake from my side the catual code is as follows

fputc(0x22,f2);
this works fine
also
int data =0x22;
fputc(data,f2);
works fine
but
int data;
data =0x22;
fputc(data,f2);
gives error

What does gives error mean?
my complete code is as follows

#include<stdio.h>

You also need stdlib.h
void main(){

int main(void){
int ret,ret2;

What is with all the vertical white space. Don't you want to read
your code on a single screen without scrolling all over the place?
/*typedef unsigned char byte;
byte data1;
data1 = 0x22;*/
int data1;

//data1 =(int)(0x33);
FILE *f,*f2;

if((f=fopen("/user/gur11263/ccodes/MA_AMA20060427104636.ama","rb"))==NULL)
{
printf("could not open file"); // print

It would be nice to differentiate between input file failure and
output file failure.

Use EXIT_FAILURE for portability.
}

if((f2=fopen("/user/gur11263/ccodes/New.ama","wb"))==NULL) {
printf("could not open file"); // print
exit(1);
}

ret =getc(f);

You read the first character from the input file.
//for(int i=0;i<46;i++)
while(ret!=EOF)

You loop until there are no more characters.
{
//printf("%x\n",ret);
fputc(ret,f2);

You write the character previously read.
ret=getc(f);

You read the next character.
}


fclose(f); // close the filepointer */

ret2 = fseek(f2,47,SEEK_SET);

You position to the "middle" of the output file. Did your input file
have at least 47 characters?
printf("%d\n",ret2);
printf(" zero means seek successful\n");

fputc(0x22,f2);

ret2 = fseek(f2,78,SEEK_CUR); // 78 coz 1 ama is 79 in length and
while writing at 47 it autoamtically updates to 48

Don't use // comments for usenet. When the line wraps, it creates
syntax errors.

What does your comment mean? There are two ama files. If the input
file contained 79 characters as you state, this fseek attempts to go
beyond the end of the output file. What updated to 48? If you wrote
at 47 the current position should be 48. Why are you not checking the
return from fputc for success?
printf("%d\n",ret2);
printf(" zero means seek successful\n");

fputc(0x22,f2);

fclose(f2); // close the filepointer */


}

You never tell us what result you are getting that is different from
what you expect or want. Did you look at the output file with a hex
editor or what?


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,514
Latest member
AdeleGelle

Latest Threads

Top