I'm writing a program that reads strings from file, saves them to 'string buffer' and then concatenates those string and writes them to another file.
#define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <stdio.h>
int main() {
FILE *f = fopen("Read.txt", "r");
char line[20];
char buff[15][20];
int i = 0;
while (fgets(line, 18, f)) {
strcpy(buff[i], line);
i++;
}
FILE *h = fopen("Out.txt", "w+");
for (int j = 0; j < i; ++j) {
char ct[4] = "smt";
strcat(buff[j], ct);
fputs(buff[j], h);
}
return 0;
}
Contents of file Read.txt:
Lorem ipsum
dolor sit
amet
Expected output (File Out.txt):
Lorem ipsumsmt
dolor sitsmt
ametsmt
But what I get in Out.txt:
Lorem ipsum
smtdolor sit
smtamet
smt
So how to get expected result?
P.S. I think that the problem occurs when I use function fgets()
.