#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// ----------------------------------------------
// main function
// ----------------------------------------------
int main() {
	// input file
	FILE *in;
	// output file
	FILE *out;
	
	int i, value;
	const char *file_name = "data.txt";
	
	srand(19702013);
	
	// ------------------------------------------
	// generate file
	// ------------------------------------------
	out = fopen(file_name, "w");
	if (!out) {
		fprintf(stderr, "error: could not open file for writing: %s", file_name);
		exit(EXIT_FAILURE);
	}
	
	for (i = 0; i<10; ++i) {
		fprintf(out, "%d\n", rand() % 100);
	}
	
	for (i = 0; i<5; ++i) {
		int j, length = (rand() % 10) + 2;
		for (j = 0; j<length; ++j) {
			fprintf(out, "%c", (char) (rand() % 26) + 65);
		}
		fprintf(out, "\n");
	}
	fclose(out);
	
	// ------------------------------------------
	// read file
	// ------------------------------------------
	in = fopen(file_name, "r");
	if (!out) {
		fprintf(stderr, "error: could not open file for reading: %s", file_name);
		exit(EXIT_FAILURE);
	}
	for (i = 0; i<10; ++i) {
		fscanf(in, "%d\n", &value);
		printf("read %d\n", value);
	}
	for (i = 0; i<5; ++i) {
		char *str = NULL;
		size_t size;
		getline(&str, &size, in);
		printf("read %s", str);
		free(str);
	}
	fclose(in);
	
	return 0;
}
