LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   int, long in char * (https://www.linuxquestions.org/questions/programming-9/int-long-in-char-%2A-87866/)

marek 09-01-2003 02:46 AM

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??

SaTaN 09-01-2003 03:25 AM

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

vanquisher 09-01-2003 03:26 AM

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.

vanquisher 09-01-2003 03:38 AM

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.

marek 09-01-2003 08:28 AM

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???

vanquisher 09-01-2003 08:52 AM

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...

SaTaN 09-01-2003 09:29 AM

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

vanquisher 09-01-2003 09:39 AM

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

sorry for that

marek 09-01-2003 09:55 AM

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...

vanquisher 09-01-2003 10:10 AM

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 ;)

marek 09-01-2003 10:43 AM

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


All times are GMT -5. The time now is 07:10 AM.