Pagini recente » Cod sursa (job #2846033) | Cod sursa (job #1743326) | Cod sursa (job #1329930) | Cod sursa (job #2328495) | Cod sursa (job #1359110)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 256
void write_in_file(char **words, int n, FILE *out) {
int i, count = 0;
for (i = 1; i < n; ++i) {
++count;
if (strcmp(words[i], words[0]) == 0) break;
}
for (i = 0; i < count; i = i+2) {
fprintf(out, "%s", words[i]);
fprintf(out, "%c", ',');
}
fprintf(out, "%c", '\n');
int k = 0;
for (i = 1; i < n; i = i + 2) {
fprintf(out, "%s", words[i]);
fprintf(out, "%c", ',');
++k;
if (k == count / 2 ) {
fprintf(out, "%c", '\n');
k = 0;
}
}
}
int main() {
FILE *in, *out;
char buffer[MAX];
if ((in = fopen("convertor.in", "rt")) == NULL) {
fprintf(stderr, "Nu pot deschide fisierul\n");
exit(-1);
}
if ((out = fopen("convertor.out", "wt")) == NULL) {
fprintf(stderr, "Nu pot deschide fisierul\n");
exit(-1);
}
int k = 0, i, j, count = 0, opened = 0, digit = 0;
int start;
char **words;
words = (char**) malloc (1000 * sizeof(char*));
while (fgets(buffer, MAX-1, in)) {
for (i = 0; i < strlen(buffer); ++i) {
if (buffer[i] == '"' && opened == 0) {
opened = 1;
start = i + 1;
continue;
}
if (buffer[i] == '"' && opened == 1) {
opened = 0;
words[k++] = (char *) malloc((count+1) * sizeof(char));
strncpy(words[k-1], buffer+start, count);
count = 0;
}
if (opened == 1) {
++count;
}
if (opened == 0 && isdigit(buffer[i])) {
++count;
digit = 1;
}
if (opened == 0 && !isdigit(buffer[i]) && digit == 1) {
words[k++] = (char*) malloc((count+1) * sizeof(char));
strncpy(words[k-1], buffer+(i-count), count);
count = 0;
digit = 0;
}
}
}
// for (i = 0; i < k; ++i)
// printf("%s\n", words[i]);
write_in_file(words, k, out);
for (i = 0; i < k; ++i)
free(words[i]);
free(words);
fclose(in);
fclose(out);
}