LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-01-2003, 02:46 AM   #1
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Rep: Reputation: 15
int, long in char *


Sorry i,ve got probably stupid question, but i learn C and C++ by myself and i've got some wholes in it. So, how can i change my int, long or else value in common char value. I know that static_char doesn't work. How can i write int value in file?? Could you help me??
 
Old 09-01-2003, 03:25 AM   #2
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
If you want to change int into char
Use type casting i.e ,
suppose

main()
{
int i=97;
char ch;
ch=(char )i;// This is type-casting...
printf("%c",ch);
}

Then the output will be 'a' ;

To write an int or string onto a file u can write as it is ....

fprintf("%s %d ",some_string,some_int);
while reading
fscanf("%s %d",read_string,read_int) ;

will work fine .....

P.S :- Check out man pages for fprintf and fscanf then u'll know

Last edited by SaTaN; 09-01-2003 at 03:35 AM.
 
Old 09-01-2003, 03:26 AM   #3
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
Re: int, long in char *

Quote:
Originally posted by marek
Sorry i,ve got probably stupid question, but i learn C and C++ by myself and i've got some wholes in it. So, how can i change my int, long or else value in common char value. I know that static_char doesn't work. How can i write int value in file?? Could you help me??
writing int to a file...
Code:
fprintf(fp,"%d",&n);
fp is the file pointer to the file you want to write...i guess you are comfortable with file handling..if not, this is how you do it

Code:
int n;
FILE *fp = fopen("somefile.txt","w+");

scanf("%d",&n);
fprintf(fp,"%d\n",&n);
fclose(fp);
read a no. from standard input and write it to file `somefile.txt'. Don't try changing int/long to char( for that matter, any type to char) 'cos char being the smallest in size, may result in losing data...int is 4bytes long and char is just 1 byte...so, u lose 3 bytes...fprintf can be used for formatted output to a file...

if you got any other probs, u are welcome.
 
Old 09-01-2003, 03:38 AM   #4
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
Quote:
Originally posted by SaTaN
I
main()
{
int i=97;
char ch;
ch=(char )i;// This is type-casting...
printf("%c",ch);
}

Then the output will be 'a' ;
It works fine till for i between 0 and 255 but what if i is 1000? We lose 3 bytes...It's the rule of thumb. You dont typecast higher types into lower ones...say, you don't typecast a double to an int...and you don't typecast ANY type to char...just check it out with i=1000.
 
Old 09-01-2003, 08:28 AM   #5
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Original Poster
Rep: Reputation: 15
Hmm, I wrote up your program Vanqiusher and that's i received in the file: -1073743164, so it doesn't want to work
Another question, what about writing to shared memory, in example i mapped file or i want to send data from one process to second through shared memory
This data is of course some int, long or double not a string
How can I do that???
 
Old 09-01-2003, 08:52 AM   #6
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
Quote:
Originally posted by marek
Hmm, I wrote up your program Vanqiusher and that's i received in the file: -1073743164, so it doesn't want to work

U mean my program is not working? I didn't get ya..

Quote:

Another question, what about writing to shared memory, in example i mapped file or i want to send data from one process to second through shared memory
This data is of course some int, long or double not a string
How can I do that???
well, that's out of my league...may b we should ask some real C gurus out there...
 
Old 09-01-2003, 09:29 AM   #7
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Re: Re: int, long in char *

Quote:
Originally posted by vanquisher

Code:
fprintf(fp,"%d\n",&n);

No wonder the code didn't work .....

It should be

fprintf(fp,"%d\n",n);

"&n" means the address of n
 
Old 09-01-2003, 09:39 AM   #8
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
stupid typo...
yeah..it should b fprintf(fp,"%d\n",n);

sorry for that
 
Old 09-01-2003, 09:55 AM   #9
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Original Poster
Rep: Reputation: 15
Now it of course works great Great Thanks

Do you know somebody is really keen on programming in linux at C, some guru who i can send my question via mail?? (i am expecially interested at work with file descriptors (you know write, open, close functions) instead file pointers, altough on other hand your program will we be very usefull for me, because i can exchange file pointer by file descriptor and work on file with written int number). But it might take me to much time...
 
Old 09-01-2003, 10:10 AM   #10
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
Quote:
Originally posted by marek
Now it of course works great Great Thanks

Do you know somebody is really keen on programming in linux at C, some guru who i can send my question via mail?? (i am expecially interested at work with file descriptors (you know write, open, close functions) instead file pointers, altough on other hand your program will we be very usefull for me, because i can exchange file pointer by file descriptor and work on file with written int number). But it might take me to much time...
I won't call myself a guru( no, not yet ). But I'm polishing my Linux programming skills...'cos I need them for my course on Operating Systems, which is largely GNU/Linux based. You can mail me your doubts and I'll mail you mine. That way, we both will benefit. May be we both will be gurus one day
 
Old 09-01-2003, 10:43 AM   #11
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Original Poster
Rep: Reputation: 15
No trouble, I can probably speek better than you polish so i can be useful, send me your questions on this mail:
spartanie@interia.pl
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printf unsigned long long int? blackzone Programming 9 03-04-2008 12:41 PM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
char to int: a twist Guru3 Programming 5 12-05-2004 08:49 AM
Converting int value to char liguorir Programming 8 05-23-2004 07:21 PM
int(char(254)) becomes kalleanka Programming 2 02-17-2004 03:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration