Adding characters to strings in C++

OneOfTheseDays

Diamond Member
Jan 15, 2000
7,052
0
0
For part of my programming project, we need to grab a filename out of physical memory and test that filename for accuracy. Basically, we are given a virtual address where the filename starts and must use an already specified function to read the filename byte by byte. Therefore we are reading the filename in 1 character at a time.

I'm having trouble figuring out how to build up this filename, because i will need to use that filename later on in the project. the specified function that reads from virtual memory will return what it read in the form of an integer pointer (*value) and basically you can typecast that value as (char)value to read the character that was read. how would you guys suggest building up a string of these individual chars. thanks.
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
If I understand you correctly, you want to create a filename byte by byte from an integer function return?

Assuming you know the length of the string (you really kind of have to):

Really, you'd need to flesh it out more, but that should work for you somewhat

(Edited because I did not put code in correctly)
 

itachi

Senior member
Aug 17, 2004
390
0
0
you're making it sound a lot more complex than it actually is.. basically, you're given a pointer to a filename and you have to read it.

there should be a limitation on how big the file name can be.. allocate an array to that size plus 1. since you're reading from memory, a null character will say where the string ends.

char fn[MAX_FSIZE + 1];
int *ptr;
int i;

for (i=0; i < MAX_FSIZE; i++) {
ptr = function(...);
assert(ptr != NULL);
fn[ i ] = (char) (*ptr & 0x0ff);
}
fn[i+1] = 0;
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
if it's c++, use ostringstream instead of character arrays. is it correct that you're getting one character at a time?

edit: used code tag
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
i have to admit, i'm a little confused about what you're given. if you're given a pointer to the start of a series of characters and you know that the series of characters ends with a NUL, then you don't really need to do anything. just cast what you're given to a char * and possibly construct a string out of it. my previous post was assuming that for some strange reason the function you're given gives you one character at a time as a pointer.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: oog
i have to admit, i'm a little confused about what you're given. if you're given a pointer to the start of a series of characters and you know that the series of characters ends with a NUL, then you don't really need to do anything. just cast what you're given to a char * and possibly construct a string out of it. my previous post was assuming that for some strange reason the function you're given gives you one character at a time as a pointer.

I'm confused on that too. Why do you need to read it one at a time?

What do you mean by string? Array of chars? CString? Array of tchars??

So let me get this straight:
[*]You're given a memory address.
[*]You need to build a string starting from that address.

Are you already given a null? What is the type of data that pointer points to? Is it a pointer to 8-bit data (usually chars) or a pointer to 32-bit data (mostly integers)?

Either you need a null or you need a maximum length of the path or you can't do this.
 

OneOfTheseDays

Diamond Member
Jan 15, 2000
7,052
0
0
yes I have to impose my own maximum length.

here is what I do now that works:

char a[MAX_SIZE];
int i;

for(int j = 0; j < MAX_SIZE; ++j)
a[j] = NULL;

Loop{
i = (return value from memRead(readfunction) which reads in 1 byte at a time);
a[counter] = (char)i;

this builds the filename correctly and displays the right output.

 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Originally posted by: Sudheer Anne
yes I have to impose my own maximum length.

here is what I do now that works:

char a[MAX_SIZE];
int i;

for(int j = 0; j < MAX_SIZE; ++j)
a[j] = NULL;

Loop{
i = (return value from memRead(readfunction) which reads in 1 byte at a time);
a[counter] = (char)i;

this builds the filename correctly and displays the right output.


It's not clear how the loop that you have above will end. Does the memRead function give you some indication that it has no more data? Your code deals with strings in a way that reflects the use of C rather than C++. You should be using a stringstream to accumulate the characters and then extract an std::string out of it.
 

OneOfTheseDays

Diamond Member
Jan 15, 2000
7,052
0
0
the loop will basically die as soon as either a NULL character is reached or if ReadMem fails or if the filename exceeds the maximum character length.