commit 7fcaa90b13db741028ca0268337bc535f5c47e13 Author: Arkadiusz Hypki Date: Fri Mar 24 10:34:26 2023 +0100 'Initial commit: reading line by line;' diff --git a/gene.txt b/gene.txt new file mode 100644 index 0000000..1eab733 --- /dev/null +++ b/gene.txt @@ -0,0 +1,3 @@ +# gene w +ABC 1.0 +DED 2.0 \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..63aca10 --- /dev/null +++ b/main.c @@ -0,0 +1,52 @@ + +#include +#include + +//int main(void) +//{ +// FILE * fp; +// char * line = NULL; +// size_t len = 0; +// ssize_t read; +// +// fp = fopen("/etc/fstab", "r"); +// if (fp == NULL) +// exit(EXIT_FAILURE); +// +// while ((read = getline(&line, &len, fp)) != -1) { +// printf("Retrieved line of length %zu:\n", read); +// printf("%s", line); +// } +// +// fclose(fp); +// if (line) +// free(line); +// exit(EXIT_SUCCESS); +//} + +#include +#include +#define MAX_LINE_LENGTH 1000 + +int main() { + int num; + FILE *fptr; + char line[MAX_LINE_LENGTH]; + + fptr = fopen("../gene.txt", "r"); + + if (fptr == NULL) { + printf("File is empty!"); + exit(1); + } + + while (fgets(line, MAX_LINE_LENGTH, fptr)!=NULL) { + printf("%s", line); + + char gene[MAX_LINE_LENGTH]; + double weight; + sscanf(line,"%s %f", gene, &weight); + + printf("\nGene: %s has weight %lf\n", gene, weight); + } +} \ No newline at end of file