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.
Without libraries? yes and no. Yes, you can use pointers, but no it's all manually moved byte by byte, just with a loop or something.
With libraries? memmove(3) - Recommend you read the manual page thoroughly. It DOES allow for overlapping regions, but one has to be careful about src/dst and that you're not obliterating src as you go along.
Edit: And the manual page says that it does move bytes into a temporary array, so if you're moving left to right (array-wise) then it won't do the obliteration thing I feared. I'd test that though. (Me being shifty-eyed and not trusting all library calls. )
I'm not sure what you want to do exactly, but you can operate in groups of eight characters as an 64 bit integer, and do a right shift of 8 bits. NOTE that the right shift works on little endian architectures, on big endian architectures you would need to use a left shift.
So, on your example to shift a string of 9 characters plus '\0', the shift can be done as shown on the next code.
If you want to use this method with larger strings, you will need to be sure that the buffer (carr) has a size of a multiple of 8 (plus the null terminator). And your code will be less clear and perhaps more difficult to maintain.
If by "this" you just mean "shift bytes in an array", then I second rtmistler's suggestion of memmove. If the question is more about recognizing a string in a stream, then I would structure it like this:
Code:
const char* findme = "012345678";
const char* findme_end = findme + strlen(findme);
const char* found = findme;
while(c != EOF)
{
c = fgetc(fstream);
if (c == *found)
found++;
else
found = findme;
if (found == findme_end)
{
printf("hispanola");
found = findme;
}
}
For more sophisticated stuff, you could look at using a lexer or similar.
Quote:
Originally Posted by rtmistler
Edit: And the manual page says that it does move bytes into a temporary array, so if you're moving left to right (array-wise) then it won't do the obliteration thing I feared. I'd test that though. (Me being shifty-eyed and not trusting all library calls. )
memcpy is the one which does not work with overlapping src/dst, memmove should be fine.
A second look by the way, because one thought to me originally was to use a pointer and move back one character, decide what to do with the new first character, and call it a day.
What's missing is a proper definition of the carr[] array, as well as a definition as to how this is tied, or not tied to the variable c.
If your intention is to rotate new characters into an array, we don't know.
I mean I'd just fill the array in from the end, or second to last location, with each character and adjust the pointer backwards as it goes, and provide the pointer once it's all saved.
And, you know, overflow avoidance to not go beneath the zero element and set the last location to NULL so as to make it a string, ... verify if each input character is printable.
Without libraries? yes and no. Yes, you can use pointers, but no it's all manually moved byte by byte, just with a loop or something.
With libraries? memmove(3) - Recommend you read the manual page thoroughly. It DOES allow for overlapping regions, but one has to be careful about src/dst and that you're not obliterating src as you go along.
Edit: And the manual page says that it does move bytes into a temporary array, so if you're moving left to right (array-wise) then it won't do the obliteration thing I feared. I'd test that though. (Me being shifty-eyed and not trusting all library calls. )
big up. my searches kept pointing me to shift() but i think thats for shifting bits (instead of whole bytes).
A second look by the way, because one thought to me originally was to use a pointer and move back one character, decide what to do with the new first character, and call it a day.
What's missing is a proper definition of the carr[] array, as well as a definition as to how this is tied, or not tied to the variable c.
If your intention is to rotate new characters into an array, we don't know.
thanx for rechecking. for your curiosity, i wanted to copy an mvs-mainframe dataset substituting about a dozen org-id's into proper values (the dataset is mostly ascii but has random binaries and ascii-10 (lf)'s thrown in).
so sed, awk, cut would choke on it. weirdly, head was able to read the whole line. also fgets(), getline() wasnt able to read the whole line either.
so i decided to process the file one byte at a time instead.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.