sydneyguy
asked on
c++ getting the first 10 characters of a char* string
char * StartofNamedata; // StartofNamedat; will hold some thing like "this is the day to go to town"
string ActuallyFound ... will hold the data "this is the day" when it is finished it will start at [0] and then get the first 15 chars
any ideas woudl be appreciated have already thought of [0] to [15] but must be a better way
thanks in advance
string ActuallyFound ... will hold the data "this is the day" when it is finished it will start at [0] and then get the first 15 chars
any ideas woudl be appreciated have already thought of [0] to [15] but must be a better way
thanks in advance
ASKER
temp = StartofNamedata[15]; // save the character
StartofNamedata[15] = temp;
both throw up error C2440: '=' : cannot convert from 'char *' to 'char'
its a clean build to test the code
any ideas
b
StartofNamedata[15] = temp;
both throw up error C2440: '=' : cannot convert from 'char *' to 'char'
its a clean build to test the code
any ideas
b
Oops. :)
Line 3 should be
char temp;
Line 3 should be
char temp;
ASKER
dont know why this is doing this at this line
StartofNamedata[15] = 0;
but the abov changes got it to compile
An unhandled exception of type 'System.AccessViolationExc eption' occurred in test sting disection.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
StartofNamedata[15] = 0;
but the abov changes got it to compile
An unhandled exception of type 'System.AccessViolationExc
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
ASKER
dont know why this is doing this at this line
StartofNamedata[15] = 0;
but the abov changes got it to compile
An unhandled exception of type 'System.AccessViolationExc eption' occurred in test sting disection.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
StartofNamedata[15] = 0;
but the abov changes got it to compile
An unhandled exception of type 'System.AccessViolationExc
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for that its so simple it right before my eyes but needed your input to see it
i know how many characters it is from the start of the name to the end of the name
so got the numbers of characters needed
then created a string and then as in your code terminated the string to get what i wanted
simple.
as Einstein once said "every challenge has an elegant solution"
so thank you for my elegant solution
// logfound is the number of characters needed
string abc; // this will hold a copy of the data to pharse out
abcd = (string)StartofNamedata; // add it to abcd from a * to the string
abcd.resize (logFound); // then cull out the characters that i dont need this terminating it correctly at the right place
bloody brilliant thank you
b
i know how many characters it is from the start of the name to the end of the name
so got the numbers of characters needed
then created a string and then as in your code terminated the string to get what i wanted
simple.
as Einstein once said "every challenge has an elegant solution"
so thank you for my elegant solution
// logfound is the number of characters needed
string abc; // this will hold a copy of the data to pharse out
abcd = (string)StartofNamedata; // add it to abcd from a * to the string
abcd.resize (logFound); // then cull out the characters that i dont need this terminating it correctly at the right place
bloody brilliant thank you
b
ASKER
thanks to both for the solution and to the reading that i can now go and do it is all slowly coming together thanks for the heads up
b
b
>> string abc; // this will hold a copy of the data to pharse out
>> abcd = (string)StartofNamedata; // add it to abcd from a * to the string
You can simply this to just
string abc = StartofNamedata;
Also, you don't need the cast since std::string already has an overloaded assignment operator that will automatically take a zero terminated c-style string and assign it to a std::string.
Note that in the simplification I post, that is NOT an assignment, that is a construction (it doesn't call the assignment operator, it calls std::strings constructor, which also has an overload for c-style strings).
Good luck.
>> abcd = (string)StartofNamedata; // add it to abcd from a * to the string
You can simply this to just
string abc = StartofNamedata;
Also, you don't need the cast since std::string already has an overloaded assignment operator that will automatically take a zero terminated c-style string and assign it to a std::string.
Note that in the simplification I post, that is NOT an assignment, that is a construction (it doesn't call the assignment operator, it calls std::strings constructor, which also has an overload for c-style strings).
Good luck.
ASKER
thanks fo the extra input, you sound pretty switched on, is there any good reading matter on this subjest i can get my teeth into, most reading are usually fairly light when it comes to the nitty gritty of these bits
b
b
char * StartofNamedata = "this is the day to go to town";
string ActuallyFound ;
char *temp;
// You should have sanity checks to make sure that the string is longer that 15 characters.
temp = StartofNamedata[15]; // save the character
StartofNamedata[15] = 0; // terminate the string after 15 characters
ActuallyFound = (string)StartofNamedata;
StartofNamedata[15] = temp;
Using the string class natively results in:
ActuallyFound = (string)StartofNamedata;
ActuallyFound.resize (15);
Good Luck!
Kent