473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to pass hex value to fputc

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

May 5 '06 #1
4 4294
Summu82 wrote:
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.

--
Ian Collins.
May 5 '06 #2
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 */
}

May 5 '06 #3
Summu82 wrote:
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 <stdlib.h> for exit();
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?

--
Ian Collins.
May 5 '06 #4
On 5 May 2006 00:40:00 -0700, "Summu82" <su***********@gmail.com>
wrote:
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.
exit(1);
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
May 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
40
by: Matt | last post by:
Alright, this is puzzling me. Here's what it says: Exercise 1-7. Write a program to print the value of EOF. Which is from the book "The C Programming Language". How would I go about writing...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
3
by: Vivienne | last post by:
I am using VS 2005. In a project when I was trying to write some unsigned char into a file, I used fwrite() and fputc(). But I found whenever I tried to write 0x0a, fwrite() function automaticly...
2
by: Shafik | last post by:
Hello folks, I am having a really weird problem. Under windows XP, a simple C program designed to write to a file has been adding EXTRA characters to the output file. First I used "fwrite",...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
3
by: =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?= | last post by:
When I execute the following code #include <stdio.h> #include <stdlib.h> int main(void) { FILE *bmp; bmp = fopen("deneme.bmp","w"); fputc(10,bmp);
23
by: Bill Cunningham | last post by:
I have been using fgetc and fputc with a while loop construct to copy files. It seems to work fine but the thing is I always check for fgetc's RV and it's always -1. If the program doesn't work...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.