'Initial commit: reading line by line;'

This commit is contained in:
Arkadiusz Hypki 2023-03-24 10:34:26 +01:00
commit 7fcaa90b13
2 changed files with 55 additions and 0 deletions

3
gene.txt Normal file
View File

@ -0,0 +1,3 @@
# gene w
ABC 1.0
DED 2.0

52
main.c Normal file
View File

@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
//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 <stdio.h>
#include <stdlib.h>
#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);
}
}