Najlepiej wklej cały kod.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <assert.h>
struct file_t
{
FILE* f;
int size;
};
int open_file(struct file_t* f, const char* filename);
struct file_t* find_min(const struct file_t* files, int size);
void close_file(struct file_t* f);
int open_file(struct file_t* f, const char* filename) {
char bufor[50];
if (filename[0] == '\n' || filename[0] == EOF)
return 0;
if (f->f = fopen(filename, "r")) {
long curpos, length;
curpos = ftell(f->f);
fseek(f->f, 0L, SEEK_END);
length = ftell(f->f);
fseek(f->f, curpos, SEEK_SET);
printf("%d\n", length);
f->size = length;
fread(bufor, sizeof(char), f->size, f->f);
printf("%s\n", bufor);
return 1;
}
else
return 2;
}
struct file_t* find_min(const struct file_t* files, int size) {
struct file_t min_file;
min_file = files[size];
while (size >= 0) {
if (files[size].size < min_file.size)
{
min_file = files[size];
}
size--;
}
return &min_file;
}
int main() {
int size = 0, i = 0;
struct file_t* file;
struct file_t* files = (struct file_t*)malloc(10 * sizeof(struct file_t));
size_t length;
char* t, * input;
char c;
printf("Enter files names: ");
do {
input = (char*)malloc(sizeof(char));
length = 0;
while ((c = getchar()) != '\n' && c != EOF) {
t = (char*)realloc(input, length + 1);
assert(t);
input = t;
input[length++] = c;
}
if (length >= 1) {
t = (char*)realloc(input, length + 1);
assert(t);
input = t;
input[length] = '\0';
}
printf("%s\n", input);
if (open_file(&files[size], input) == 1) {
size++;
printf("udalo sie\n");
}
else
printf("nie udalo sie\n");
} while (length != 0);
size_t x;
file = &files[0];
char bufor[100];
x = fread(bufor, sizeof(char), file->size, file->f);
printf("tekst[0] - %s\n", bufor);
printf("size[0] - %d\n", files[0].size);
printf("x[0] - %d\n", x);
printf("wprowadz teksty: ");
size -= 1;
do {
length = 0;
input = (char*)malloc(sizeof(char));
while ((c = getchar()) != '\n' && c != EOF) {
t = (char*)realloc(input, length + 1);
assert(t);
input = t;
input[length++] = c;
}
if (length >= 1) {
t = (char*)realloc(input, length + 1);
assert(t);
input = t;
input[length] = '\0';
}
printf("input - %s\n", input);
printf("length - %d\n", length);
file = find_min(files, size);
fread(bufor, sizeof(char), file->size, file->f);
printf("%s\n", bufor);
fwrite(input, length, 1, file->f);
printf("size before: %d\n", file->size);
file->size += length;
printf("size after: %d\n", file->size);
free(input);
} while (length != 0);
}
Ogólnie program wczytuje nazwy plików, aż do napotkania znaku nowej linii. Uchwyty tych plików są przechowywane w strukturach file_t razem z ich rozmiarami, a te struktury w tablicy files. Następnie wczytujemy tekst i dopisujemy go do pliku, który ma najmniejszy rozmiar. Powtarzamy to, dopóki nie wpiszemy znaku nowej linii.
Zdaję sobie sprawę, że jest więcej błedów, ale ten wspomniany na początku był pierwszym, z którym nie mogłem sobie poradzić. Z góry dziękuję za poświecony czas.