Skip to content

Instantly share code, notes, and snippets.

@d630
Last active May 29, 2018 14:43
Show Gist options
  • Save d630/abfbb875d52536d73fcf8d1d15e248a2 to your computer and use it in GitHub Desktop.
Save d630/abfbb875d52536d73fcf8d1d15e248a2 to your computer and use it in GitHub Desktop.
C: struct, w/r into/from file, compare
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEBUG 0
#define MAX_ENT 2
#define MAX_LEN 80
#define FILE_ART "articles.txt"
#define DELIM "|"
#define TYPES_CNT 4
#define DESC 0
#define PRICE 1
#define AMOUNT 2
#define UNIT 3
struct article {
char desc[MAX_LEN];
double price;
int amount;
char unit[MAX_LEN];
};
typedef struct article Article;
void scan(Article *s)
{
for (int i = 0; i < MAX_ENT; i++) {
printf("desc (*char): ");
scanf("%s", s[i].desc);
printf("price (double): ");
scanf("%lf", &s[i].price);
// s[i].price = strtod(input, &c);
printf("amount (int): ");
scanf("%d", &s[i].amount);
// s[i].amount = strtol(input, &c, 10);
printf("unit (*char): ");
scanf("%s", s[i].unit);
}
}
void writef(Article *s)
{
FILE *file;
remove(FILE_ART);
file = fopen(FILE_ART, "a");
for (int i = 0; i < MAX_ENT; i++) {
if (DEBUG)
printf("%s" DELIM "%.2f" DELIM "%d" DELIM "%s\n",
s[i].desc,
s[i].price,
s[i].amount,
s[i].unit);
fprintf(file, "%s" DELIM "%.2f" DELIM "%d" DELIM "%s\n",
s[i].desc,
s[i].price,
s[i].amount,
s[i].unit);
}
fclose(file);
}
void read_struct(Article *s)
{
FILE *file;
file = fopen(FILE_ART, "r");
char line[MAX_LEN];
char *ptr;
char *c;
int i = 0;
int j;
while (fgets(line, sizeof(line), file) != NULL) {
line[strlen(line) - 1] = '\0';
ptr = strtok(line, "|");
j = 0;
while (ptr != NULL) {
if (DEBUG)
printf("%s\n", ptr);
switch (j) {
case DESC:
strcpy(s[i].desc, ptr);
break;
case PRICE:
s[i].price = strtod(ptr, &c);
break;
case AMOUNT:
s[i].amount = strtol(ptr, &c, 10);
break;
case UNIT:
strcpy(s[i].unit, ptr);
break;
default:
puts("Too many fields");
exit(128);
}
ptr = strtok(NULL, "|");
j++;
}
i++;
}
fclose(file);
}
void compare_struct(Article *s1, Article *s2, int ss1, int ss2)
{
// TODO(d630): too stupid
// int size_s1 = sizeof(s1)/sizeof(s1[0]);
// int size_s2 = sizeof(s2)/sizeof(s2[0]);
if (ss1 != ss2) {
printf("Different length: %d != %d", ss1, ss2);
exit(129);
}
int j;
for (int i = 0; i < ss1; i++) {
puts("");
for (j = 0; j < TYPES_CNT; j++) {
printf("%d,%d: ", i, j);
switch (j) {
case DESC:
printf("%d %s %s\n", strcmp(s1[i].desc, s2[i].desc),s1[i].desc, s2[i].desc );
break;
case PRICE:
printf("%d %.2f %.2f\n", !(s1[i].price == s2[i].price), s1[i].price, s2[i].price);
break;
case AMOUNT:
printf("%d %d %d\n", !(s1[i].amount == s2[i].amount), s1[i].amount, s2[i].amount);
break;
case UNIT:
printf("%d %s %s\n", strcmp(s1[i].unit, s2[i].unit), s1[i].unit, s2[i].unit);
break;
}
}
}
}
int main(void)
{
Article a[MAX_ENT];
Article b[MAX_ENT];
scan(a);
writef(a);
read_struct(b);
compare_struct(a, b, sizeof(a)/sizeof(a[0]), sizeof(b)/sizeof(b[0]));
return 0;
}
// runs with -std=gnu99
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct artikel {
char *desc;
double price;
int amount;
char *unit;
};
int main()
{
struct artikel art1 = {
"foo",
2.50,
12,
"liter"
};
struct artikel art2 = {
"bar",
1.50,
3,
"kg"
};
struct artikel *art3;
art3 = malloc(sizeof(art3));
art3->desc = strdup("lich");
art3->price = 5.00;
art3->amount = 222;
art3->unit = strdup("liter");
printf("%s\n", art3->desc);
printf("%s\n", art3->unit);
free(art3->desc);
free(art3->unit);
free(art3);
return 0;
}
// vim set ft=c :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment