LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-08-2010, 06:25 AM   #1
aa.bb.cc
LQ Newbie
 
Registered: Jul 2010
Posts: 10

Rep: Reputation: 0
Unhappy type casting in c++


hello
i have just joined this group.SO sorry if i am posting in the wrong forum.

well i have a function FtpWrite(void *string_to_be_writteninfile, int , netbuf*)

i want to write the size of a file inside a file. so basically i have an interger lets say int size=5;
so now i have to write 5 in my file.
so how can i convert the int size into void * as this is wat is passed as a parameter to the function.
 
Old 07-08-2010, 06:38 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Depending on how you will be using the file, the simplest choice is probably to write the int as raw (not text, not formatted) data in the file

Code:
FtpWrite( (void*)(&size), sizeof(size) , whatever );
BTW, a programmer of my experience level ought to remember which of C vs. C++ or both and under what conditions, you could write simply &size where a void* is needed and the compiler will cast implicitly. I intentionally don't memorize such details, because I think this kind of code is more maintainable if you explicitly code (void*)(&size) even if the compiler would have understood less.

Last edited by johnsfine; 07-08-2010 at 06:44 AM.
 
Old 07-08-2010, 06:49 AM   #3
aa.bb.cc
LQ Newbie
 
Registered: Jul 2010
Posts: 10

Original Poster
Rep: Reputation: 0
thankyou so much jonsfine for the help......
i tried it...but instead of the filesize some strange charracters are written in the file......
this is a piece of my code::

m_wrt = FtpWrite((void*)(&m_ulSize), sizeof(m_ulSize), p_nhandler);
 
Old 07-08-2010, 07:02 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Quote:
Originally Posted by johnsfine View Post
Depending on how you will be using the file, the simplest choice is probably to write the int as raw (not text, not formatted) data in the file
So if you want to read the file as text, this method obviously won't work.

It would work if you want another program to read the raw data back with a operation that properly corresponds to the one used to write the data out.

Quote:
Originally Posted by aa.bb.cc View Post
instead of the filesize some strange charracters are written in the file.
Exactly as I would expect. The size is written as raw data, not as text.

If you need the size written as text, you'll need to use some function (there are many choices) that converts an integer to text. You'll also need to select either a fixed number of digits or a delimiter to put in the file, so whatever reads the file knows where the number ends.
 
Old 07-08-2010, 07:05 AM   #5
aa.bb.cc
LQ Newbie
 
Registered: Jul 2010
Posts: 10

Original Poster
Rep: Reputation: 0
thanks a lot...
can you please suggest me some functions which can convert int to text??
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Casting to non-scalar type in C mvr853 Programming 5 06-11-2007 06:04 PM
Type Casting in Lisp indienick Programming 2 01-17-2007 12:51 PM
Type Casting problem C EmtYskY Programming 3 12-29-2006 07:01 PM
How to do type casting in Sparc Assembly Language foxele Programming 1 10-09-2004 05:40 PM
casting cynthia Programming 6 10-05-2004 04:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration