M
mdefoor
I've written the following sample to split a string into chunks. Is
this the right approach or is there perhaps a better way to accomplish
getting chunks of a string?
#include <stdio.h>
#include <string.h>
#define BUFSIZE 10
int main(int argc, char *argv[]) {
int i;
int nbr;
char buff[256];
char tmpChr[1];
char *buffer;
sprintf(buff, "%s", "this is a longer buffer");
buffer = strdup(buff);
char tmpBuff[strlen(buffer) + 1];
*tmpBuff = '\0';
i = 0;
nbr = 0;
printf("buffer = %s len = %d\n", buffer, strlen(buffer));
do {
if (i < BUFSIZE) {
sprintf(tmpChr, "%c", buffer[nbr]);
strcat(tmpBuff, tmpChr);
i++;
nbr++;
if (nbr == strlen(buffer)) {
printf("--%s-- i = %d nbr = %d\n", tmpBuff, i, nbr);
}
}
else {
printf("--%s-- i = %d nbr = %d\n", tmpBuff, i, nbr);
i = 0;
*tmpBuff = '\0';
}
} while (buffer[nbr] != '\0');
}
this the right approach or is there perhaps a better way to accomplish
getting chunks of a string?
#include <stdio.h>
#include <string.h>
#define BUFSIZE 10
int main(int argc, char *argv[]) {
int i;
int nbr;
char buff[256];
char tmpChr[1];
char *buffer;
sprintf(buff, "%s", "this is a longer buffer");
buffer = strdup(buff);
char tmpBuff[strlen(buffer) + 1];
*tmpBuff = '\0';
i = 0;
nbr = 0;
printf("buffer = %s len = %d\n", buffer, strlen(buffer));
do {
if (i < BUFSIZE) {
sprintf(tmpChr, "%c", buffer[nbr]);
strcat(tmpBuff, tmpChr);
i++;
nbr++;
if (nbr == strlen(buffer)) {
printf("--%s-- i = %d nbr = %d\n", tmpBuff, i, nbr);
}
}
else {
printf("--%s-- i = %d nbr = %d\n", tmpBuff, i, nbr);
i = 0;
*tmpBuff = '\0';
}
} while (buffer[nbr] != '\0');
}