473,434 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

string to pointer

15
Hi,
I'm writing an address into a file and later i need to read it and delete the memory at this 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.
Mar 29 '09 #1
4 1960
JosAH
11,448 Expert 8TB
@vsachar
Cast your void pointer to a long (unsigned) int and write that number to a file. You can later read it back again and cast it to a void*.

kind regards,

Jos
Mar 29 '09 #2
vsachar
15
Hi,
I tried this. i don't know if thats what you meant but it didn't give me the output i was looking for

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3. #include<stdio.h>
  4. #include<fstream>
  5. using namespace std;
  6. void * operator new(size_t size)
  7. {
  8.     cout<<"\nOVERLOADED NEW";
  9.     void * MemHandle;
  10.     MemHandle=malloc(size);
  11.     if(!MemHandle)
  12.     {
  13.         bad_alloc ba;
  14.         throw ba;
  15.     }
  16.     ofstream out;
  17.     out.open("D:/Project/abc.txt",ios::app);
  18.     out<<"\n"<<(unsigned long int*)MemHandle;
  19.     out.close();
  20.     cout<<"\nmem handle:"<<MemHandle<<endl;
  21.     return MemHandle;
  22. }
  23. void main()
  24. {
  25.     fstream in("D:/Project/abc.txt");
  26.     int *p=new int;
  27.     char buf[20];
  28.     in.getline(buf,8);
  29.     buf[8]='\0';
  30.     cout<<"buf:"<<(unsigned long int*)buf;
  31. }
  32.  
  33.  
  34.  
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.
Mar 29 '09 #3
JosAH
11,448 Expert 8TB
Don't do it like that; do something like this instead for your output:

Expand|Select|Wrap|Line Numbers
  1. void* memory_pointer= ...;
  2. cout << (unsigned long)memory_pointer;
  3.  
and for input do something like this:
Expand|Select|Wrap|Line Numbers
  1. unsigned long tmp;
  2. void* memory_pointer;
  3. cin >> tmp;
  4. memory_pointer= (void*)tmp;
  5.  
kind regards,

Jos
Mar 29 '09 #4
vsachar
15
@JosAH

Thank you so much!!
It worked!
:)
Mar 30 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
15
by: Andrew Brampton | last post by:
Hi, I'm new to using std classes, but I was wondering how I could do the following efficiently with the string class: Say I had a string with delimited values such as:...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
13
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
10
by: jt | last post by:
I'm needing to take a binary string start at a certain position and return a pointer from that postion to the end of the binary stirng. something like this: char bstr; char *pos; ...
5
by: Divick | last post by:
Hi all, I want to use std::string instead of char * 's as suggested at many places in this newsgroup as well as else where, but I am confused with the way to use it. For example I have a...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
6
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.