0

I want to concatenate lots of strings together to produce a unique string which will be long enough. Here is an example code:

 char *s;
 s= strconcat("A big-length string",NULL);
 fprintf(stdout,"%s",s);`

When I try to print it, the string is less that is printed is smaller than the actual string. Assume that the strconcat() function works well. Do I have to malloc() some space for s??

Also when I try strlen(s) it shows me the right number like 1078. just the printing is the matter.

EDIT The problem solved when after all strings I try to inject \n like this

s= strconcat("A big-length string","\n",NULL);

Shafik Yaghmour
  • 143,425
  • 33
  • 399
  • 682
Antifa
  • 229
  • 1
  • 3
  • 13
  • 5
    If we can assume that the `strconcat` function works well, we can also assume that it does the memory allocation (and that you have to `free(s)` after printing it). How can we solve your problem if you (a) don't show us the crucial code and (b) don't provide the real data for the code? – M Oehm Dec 12 '13 at 13:27
  • 4
    `strconcat()` is not a standard function. We do **not** know what it does. Do you have the source for it, to show it here? Do you probably mean `strcat()`. – alk Dec 12 '13 at 13:28
  • What does the `strconcat` function do, exactly? – interjay Dec 12 '13 at 13:28
  • [The strconcat() returns a string with dynamically allocated memory](http://manned.org/strconcat.1). So you don't have to use `malloc()` here. – jmlemetayer Dec 12 '13 at 13:29
  • `char *strconcat(char *first,char *second, ..., NULL)` The concat just joins strings together... – Antifa Dec 12 '13 at 13:29
  • 2
    If `strlen` shows the right length, then `fprintf` is the cause of the error. Try adding `\n` after the string, that could help – Thomas Ruiz Dec 12 '13 at 13:31
  • Agreed with @alk you should use `strcat()` instead, but this way you will have to manage your memory. – jmlemetayer Dec 12 '13 at 13:33
  • @JML The function you linked doesn't seem to require a `NULL` at the end (making it impossible to be implemented portably), so I don't think it's the same one. – interjay Dec 12 '13 at 13:34
  • @interjay Perhaps not the good manual page, but it's the first Google result when I'm searching for `man strconcat`. – jmlemetayer Dec 12 '13 at 13:38
  • 1
    You need to post a **complete**, compilable example that demonstrates the problem, including a `main` function and the `strconcat` function. This question cannot be answered in its current form. – interjay Dec 12 '13 at 13:38
  • @JML What I'm saying is that OP probably has his own implementation of `strconcat` that you won't find on Google, and we won't know what it does without seeing it. What you linked seems to be a FreeBSD-only function. – interjay Dec 12 '13 at 13:39
  • @ThomasRuiz That was the problem!!! Solved. Do you know why is this happening? – Antifa Dec 12 '13 at 13:39
  • Or to not spoil the "string", just so: `fprintf(stdout,"%s\n",s);` – alk Dec 12 '13 at 13:48
  • @alk this does not work for me – Antifa Dec 12 '13 at 14:39
  • possible duplicate of [Why does printf not flush after the call unless a newline is in the format string?](http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – Klas Lindbäck Dec 12 '13 at 14:51
  • @KonKost: You want to say appending a `"\n"` to the buffer and printing it shows all characters, but **not** appending a `'\n'` and doing `fprintf(stdout, "%s\n", s)`does **not** show all characters? – alk Dec 12 '13 at 15:07
  • @KonKost, Klas Lindbäck answered it nicely – Thomas Ruiz Dec 12 '13 at 15:13

1 Answers1

4

Output using printf/fprintf is usually buffered.

If the output file is connected to a terminal, output is line buffered, so the buffer will be flushed wfter each \n.

If the output file is not connected to a terminal, output is fully buffered, so the buffer will be flushed when it is full. Typical buffer sizes is 1kb-8kb.

to ensure that the buffer is flushed call:

fflush(stdout);

You can flush all file buffers with:

fflush(NULL);
Klas Lindbäck
  • 32,158
  • 4
  • 51
  • 77