Solved

c++ getting the first 10 characters of a char* string

Posted on 2016-08-09
11
83 Views
Last Modified: 2016-08-10
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
0
Comment
Question by:sydneyguy
  • 6
  • 3
  • 2
11 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
This would probably be easier if you were using one of the c++ string types, but that kind of operation has been used in C for a very long time.  :)

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
0
 

Author Comment

by:sydneyguy
Comment Utility
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
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
Oops.  :)

Line 3 should be

char temp;
0
 

Author Comment

by:sydneyguy
Comment Utility
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.AccessViolationException' 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.
0
 

Author Comment

by:sydneyguy
Comment Utility
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.AccessViolationException' 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.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
Comment Utility
That's got to do with how the constants string is allocated at compile/linkage time.  It's put into a block that is identified to the hardware as read only.  I probably need to apologize here.  The code was meant as an example and not a complete "how to".  Once working, you might even want to make this a function.  The changes below will help with that.

And the code is really two different solutions.  The first one is a C style solution.  The last two lines is a C++ style method.


The constant will need to be copied to a writable location;

char  *StartofNamedata;
char  *MyData = "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 than 15 characters.

StartofNamedata = strdup (MyData);   // Copy to constant
StartofNamedata[15] = 0;       // terminate the string after 15 characters
ActuallyFound = (string)StartofNamedata;
free (StartofNamedata);


Using the string class natively results in:

ActuallyFound = (string)MyData;
ActuallyFound.resize (15);
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 250 total points
Comment Utility
Firstly, I think it's necessary to clear up something (and expand on what Kdo has alluded)...

>> char  * StartofNamedata;   // StartofNamedat;  will hold some thing like "this is the day to go to town"

StartofNamedata holds the address of a zero terminated array of chars, it does not hold that string!

If it were defined as...

char StartofNamedata[]

...then StartofNamedata would be an array that would, indeed, hold the string.

Whilst the difference may seem semantic, it's really not and I'll explain why:

The C programming language (not C++) allows you to do this...

char * StartofNamedata = "this is the day to go to town";

Now, the problem here is that you DO NOT own that string, it is stored read-only in memory. C++ won't allow this as you have to declare it to be a const pointer, but C will allow this. The problem is that if you try to modify this string as it is defined you will get undefined behaviour.

If, on the other hand, you define it like this:

char StartofNamedata[] = "this is the day to go to town";

The string is assigned to the array StartofNamedata and you own it and can safely manipulate it.

So, why am I telling you this? Quite simply, because if you own the string and all you want are the first X number of chars and you are happy to modify the string you can just insert a NULL at the X+1 position and that's all you need do.

StartofNamedata[X+1] = '\0';

From now on all your standard C string manipulation functions will quite happily work this that array at its truncated length as all you've done is reset the point where it is zero terminated.

Now, if you can't modify the string you'll need to make a copy and to simplest way to do that and to limit the length of the new copy is to just use strncpy.

http://www.cplusplus.com/reference/cstring/strncpy/

That all said, if you are using C++ just use std::string (as Kdo also alluded) and forget about raw C style strings. They are evil and should be avoided at all costs in C++.

http://www.cplusplus.com/reference/string/string/
0
 

Author Comment

by:sydneyguy
Comment Utility
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
0
 

Author Closing Comment

by:sydneyguy
Comment Utility
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
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> 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.
0
 

Author Comment

by:sydneyguy
Comment Utility
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now