ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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??
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...
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.
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???
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...
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...
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.