ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You can make sure it is null terminated by doing any of the following types of things:
Code:
Imonth[2] = '\0';
Imonth[2] = NULL;
memset(Imonth, 0, 3);
// the ANSI-C string functions NULL terminate for you so the
// following should even work.
strncpy(Imonth, itemDate, 2);
In this case, initializing the strings to "" probably isn't necessary since you set them right away. (Though, it still is good practice to initialize variables.)
I don't think initializing them to "" alone would help in this case. It would still likely leave garbage in the last array positions.
and with the strncpy, my issue is that i need to parse out certain char cells in the original array,
i need to go from MM/DD/YYYY
to array1 = MM
array2=DD
array3=YYYY
the strncpy, will only copy the FIRST n chars. and the way i have structured this with the for statemetns and a simply assignment, does not copy the chars correctly...
thanks for the help!
Take a look at man regex. A regular expression is a "pattern" that can be applied to a string, such as a date, and the regular-expression engine will isolate the substrings that match the pattern.
It should be NULL, not null. I believe NULL is defined in one of the standard headers so as long as you include something like iostream it should be there.
Not quite sure why you would get compiler errors with '\0'. Posts some of the exact errors you get.
Using a regular expression is probably a safer method. Better yet would be some sort of internationalized date/time library that can parse a date in a number of formats and give you back the month/day/year. I haven't had to do much with manipulating date/times in C/C++ though, so I can't recommend any off the top of my head.
However, if you know that you will always get a date back in that specific format, your methods should be ok.
hmmm interesting methods, yes that did work, however still the funny characters, when i assign each char to NULL the error is
an invalid initalizer. My headers are included correctly, i know that.
Something i saw, that looked like it would work is this:
Yeah, you could use a std::string as well. The std::string also has a substring method, so you could put the whole date into a std::string, then use substr to break it up. For example:
Note: You could also do something like the above with a stringstream too and easily get the date/month/year as integers if that's the form you ultimately want them in. For instance:
Code:
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream ss;
int nMonth;
int nDay;
int nYear;
ss << "12/20/2005";
ss >> nMonth;
ss.ignore(1); // ignore the '/' character
ss >> nDay;
ss.ignore(1); // ignore the '/' character
ss >> nYear;
cout << "Month: " << nMonth << endl
<< "Day: " << nDay << endl
<< "Year: " << nYear << endl;
return 0;
}
In the example in my previous post, the reference isn't needed because itemDate is essentially the same as &itemDate[0]. When using a pointer to an array like that, it is essentially a pointer to the first element of the array. In the other 2 cases, you are getting a pointer to a character later on in the array.
In other words:
itemDate is basically a char* pointing to element 0.
itemDate[3] is a char at array element 3.
&itemDate[3] is basically a char* pointing to element 3.
It's a handy little trick, but a bit dangerous if itemArray doesn't actually have that number of elements since you could go beyond the bounds of the array.
When you got the errors, were you trying to initialize your arrays like so?:
char Imonth[3] = '\0'; // or NULL
Because that wouldn't work. You need to set the specific character at the end to '\0' or NULL, most likely after you had already filled in the other characters. For instance, taking your code above:
Code:
for (i = 0; i < 2; i++)
{
Imonth[i]=itemDate[i];
}
Imonth[2] = '\0'; // or NULL
HAHAHA my bad, i love how i read things half way, get excited and forget about the rest, yep the rest of your post surley tells me how to get them as an int
but how does the stringstream know to read on 2 string chars for the first int, ignore the '/' , read the next 2 string chars, ignore the '/'????
thanks again
When streaming to an int, it will read until it finds a non-numeric character. And then I used the ignore method to ignore the / and streamed to another int...
And invalid initializer seems to indicate to me that you were using '\0' and NULL as the initializer when declaring the array, not just setting a single character of the array (which is usually an invalid conversion error when trying to convert the wrong type to a char).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.