Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// pratice with fgets() + sscanf() to read in multiple lines into struct
typedef struct {
char ID[32];
char SNPs[8];
char MNPs[8];
char Insertion[8];
char Deletion[8];
char Indels[8];
char SameRef[8];
char MissingGT[8];
char SNPTransTranv[8];
char TotalHetHomRatio[8];
char SNPHetHomRatio[8];
char MNPHetHomRatio[8];
char InsertionHetHomRatio[8];
char DeletionHetHomRatio[8];
char IndelHetHomRatio[8];
char InsertDeletionRatio[8];
char Indel_SNPMNPRatio[8];
} RECORD;
int main (int argc, char *argv[])
{
char line[256]; //for row read from file
char name[32]; //1st part (key part) parsed from each line[]
char str1[8]; //2nd part (value part) parsed from each line[]
// char tail[128]; //rest behind 2nd part
FILE* fPtr = fopen(argv[1], "r");
RECORD record[16]; //test file may have 288 ~ 306 rows including blank lines for 16 RECORD
static int i = -1; //initialize counter
while (fgets(line, sizeof(line), fPtr) != NULL) {
str1[0]='0';
if ( line[0] == '\n' ) continue; //skip "blank" lines with e.g. empty or invisible spaces(to be improved!).
//scan in two parts delimited by ":", in the 2nd part, only take the first part delimited by space
sscanf(line, "%[^:] : %s", name, str1);
if (strstr(name, "Sample Name") != NULL) {
i++;
strcpy(record[i].ID, str1);
printf("%s ", str1);
}
else if (strstr(name, "SNPs") != NULL) {
strcpy(record[i].SNPs, str1);
printf("%s ", str1);}
else if (strstr(name, "MNPs") != NULL) {
strcpy(record[i].MNPs, str1);
printf("%s ", str1);}
else if (strstr(name, "Insertions") != NULL) {
strcpy(record[i].Insertion, str1);
printf("%s ", str1);}
else if (strstr(name, "Deletions") != NULL) { //changed the variable line -> name in the original post, and all the rest after this line
strcpy(record[i].Deletion, str1);
printf("%s ", str1);}
else if (strstr(name, "Indels") != NULL) {
strcpy(record[i].Indels, str1);
printf("%s ", str1);}
else if (strstr(name, "Same as reference") != NULL) {
strcpy(record[i].SameRef, str1);
printf("%s ", str1);}
else if (strstr(name, "Missing Genotype") != NULL) {
strcpy(record[i].MissingGT, str1);
printf("%s ", str1);}
else if (strstr(name, "SNP Transitions") != NULL) {
strcpy(record[i].SNPTransTranv, str1);
printf("%s ", str1);}
else if (strstr(name, "Total Het/Hom") != NULL) {
strcpy(record[i].TotalHetHomRatio, str1);
printf("%s ", str1);}
else if (strstr(name, "SNP Het/Hom ratio") != NULL) {
strcpy(record[i].SNPHetHomRatio, str1);
printf("%s ", str1);}
else if (strstr(name, "MNP Het/Hom ratio") != NULL) {
strcpy(record[i].MNPHetHomRatio, str1);
printf("%s ", str1);}
else if (strstr(name, "Insertion Het/Hom ratio") != NULL) {
strcpy(record[i].InsertionHetHomRatio, str1);
printf("%s ", str1);}
else if (strstr(name, "Deletion Het/Hom ratio") != NULL) {
strcpy(record[i].DeletionHetHomRatio, str1);
printf("%s ", str1);}
else if (strstr(name, "Indel Het/Hom ratio") != NULL) {
strcpy(record[i].IndelHetHomRatio, str1);
printf("%s ", str1);}
else if (strstr(name, "Insertion/Deletion ratio") != NULL) {
strcpy(record[i].InsertDeletionRatio, str1);
printf("%s ", str1);}
else if (strstr(name, "Inde/SNP+MNP ratio") != NULL) {
strcpy(record[i].Indel_SNPMNPRatio, str1);
printf("%s ", str1); }
else printf("Sthwrong!\n");
// printf("%s: %s\n", name, str1);
}
puts("END"); //For debug. puts() always adds newline at the end of the string
fclose(fPtr);
return 0;
}
./prog infile