I don't think it's the printf's fault. For the sake of clarity I will just paste the whole code, because I think the problem could be quite hidden in with all the pointers and what not. So here is the whole code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int size_of_token = 30;
char* ZeroString(char *str, int size)
{
int i;
for (i = 0; i < size; i++)
{
str[i] = 0;
}
return str;
}
int main(int argc, char *argv[])
{
FILE *f;
char *buf;
char filename[] = "test.txt";
int size = 0, len = 0;
int i = 0, j = 0, k = 0;
char **token = NULL;
char *tmp_token = calloc(size_of_token, sizeof(char));
f = fopen(filename, "r");
if (f == NULL)
{
printf("Error opening file %s\n", filename);
return -1;
}
fseek(f, 0, SEEK_END);
size = ftell(f);
rewind(f);
buf = calloc(size + 1, sizeof(char));
while (fgets(&buf[strlen(buf)], size, f) != NULL)
;
fclose(f);
len = strlen(buf);
token = calloc(len / 2, sizeof(char *));
for (i = 0; i < len/2; i++)
token[i] = calloc(size_of_token, sizeof(char));
for (i = 0; i < len; i++)
{
if (buf[i] != ' ' && buf[i] != '\n' && buf[i] != '\t')
tmp_token[j++] = buf[i];
else if (tmp_token[0] != 0)
{
j = 0;
strcpy(token[k++], tmp_token);
ZeroString(tmp_token, size_of_token);
}
}
strcpy(token[k++], tmp_token);
f = fopen("token.txt", "w");
//printf("", &buf);
ZeroString(buf, strlen(buf)+1);
for (i = 0; i < k; i++)
{
strcpy(&buf[strlen(buf)], token[i]);
strcpy(&buf[strlen(buf)], "\n");
}
printf("%s", buf);
fputs(buf, f);
fclose(f);
return 0;
}