473,320 Members | 1,794 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,320 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 1955
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++){...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.