1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

string to pointer

Discussion in 'C++' started by vsachar, Mar 29, 2009.

  1. vsachar

    vsachar New Member

    Joined:
    Feb 19, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I'm writing an address into a file and later i need to read it as an address. I've copied this address into a character array and am having a lot of trouble trying to convert it back into a pointer.
    The pointer initially (when writing to the file) was a void pointer.
    Please help.
    here's what i tried:

    Code:
    #include<iostream> 
    #include<string> 
    #include<stdio.h> 
    #include<fstream> 
    using namespace std; 
    void * operator new(size_t size) 
    { 
        cout<<"\nOVERLOADED NEW"; 
        void * MemHandle; 
        MemHandle=malloc(size); 
        if(!MemHandle) 
        { 
            bad_alloc ba; 
            throw ba; 
        } 
        ofstream out; 
        out.open("D:/Project/abc.txt",ios::app); 
        out<<"\n"<<(unsigned long int*)MemHandle; 
        out.close(); 
        cout<<"\nmem handle:"<<MemHandle<<endl; 
        return MemHandle; 
    } 
    void main() 
    { 
        fstream in("D:/Project/abc.txt"); 
        int *p=new int; 
        char buf[20]; 
        in.getline(buf,8); 
        buf[8]='\0'; 
        cout<<"buf:"<<(unsigned long int*)buf; 
    } 
      
    
     
  2. vsachar

    vsachar New Member

    Joined:
    Feb 19, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    The output was :

    OVERLOADED NEW
    mem handle:003552A0
    buf:0012FE9


    i needed the buf to hold the initial value.
    Please help and Thanks in advance.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What does the file contain?
    (If you open it in a hex editor then you'll know exactly. Try xvi32)
     
  4. vsachar

    vsachar New Member

    Joined:
    Feb 19, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Thank you but i got it. I wrote it as an unsigned long and read it back into a void * and it worked
    :)
     

Share This Page