Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- static char *build_format(int bufsiz) {
- char *format = calloc(1, bufsiz);
- if (!format) {
- exit(1);
- }
- sprintf(format, "%%%d[^\n]%%*c", bufsiz - 1);
- return format;
- }
- static char *read_to(char **buf, int bufsiz) {
- char *format = build_format(bufsiz);
- *buf = calloc(1, bufsiz);
- int n = scanf(format, *buf);
- free(format);
- if (n != 1) {
- free(*buf);
- return NULL;
- }
- return *buf;
- }
- static char *append_to(char **acc, char *s) {
- *acc = realloc(*acc, strlen(*acc) + strlen(s) + 1);
- if (!*acc) {
- exit(1);
- }
- strcat(*acc, s);
- return *acc;
- }
- int main(void) {
- char *acc = calloc(1, 1);
- *acc = '\0';
- for (char *buf = NULL; read_to(&buf, BUFSIZ); free(buf)) {
- if (*buf == ',') {
- free(buf);
- break;
- }
- append_to(&acc, buf);
- }
- printf("%s\n", acc);
- free(acc);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement