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[16];
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[]
FILE* fPtr = fopen(argv[1], "r");
RECORD record[12]; //test file only has ~ 70 rows
static int i = 0; //initialize counter
while (fgets(line, 256, fPtr) != NULL) {
//if (!sscanf(line, "%[^\r\n]", name)) continue; //skip blank line. This line may have problem???
sscanf(line, "%[^:] : %s", name, str1); //scan in two parts delimited by ":",
//in the 2nd part, only take the first part delimited by space
if (strstr(name, "Sample Name") != NULL)
strcpy(record[i].ID, name);
else if (strstr(name, "SNPs") != NULL)
strcpy(record[i].SNPs, str1);
else if (strstr(name, "MNPs") != NULL)
strcpy(record[i].MNPs, str1);
else if (strstr(name, "Insertions") != NULL)
strcpy(record[i].Insertion, str1);
else if (strstr(line, "Deletions") != NULL)
strcpy(record[i].Deletion, str1);
else if (strstr(line, "Indels") != NULL)
strcpy(record[i].Indels, str1);
else if (strstr(line, "Same as reference") != NULL)
strcpy(record[i].SameRef, str1);
else if (strstr(line, "Missing Genotype") != NULL)
strcpy(record[i].MissingGT, str1);
else if (strstr(line, "SNP Transitions") != NULL)
strcpy(record[i].SNPTransTranv, str1);
else if (strstr(line, "Total Het/Hom") != NULL)
strcpy(record[i].TotalHetHomRatio, str1);
else if (strstr(line, "SNP Het/Hom ratio") != NULL)
strcpy(record[i].SNPHetHomRatio, str1);
else if (strstr(line, "MNP Het/Hom ratio") != NULL)
strcpy(record[i].MNPHetHomRatio, str1);
else if (strstr(line, "Insertion Het/Hom ratio") != NULL)
strcpy(record[i].InsertionHetHomRatio, str1);
else if (strstr(line, "Deletion Het/Hom ratio") != NULL)
strcpy(record[i].DeletionHetHomRatio, str1);
else if (strstr(line, "Indel Het/Hom ratio") != NULL)
strcpy(record[i].IndelHetHomRatio, str1);
else if (strstr(line, "Insertion/Deletion ratio") != NULL)
strcpy(record[i].InsertDeletionRatio, str1);
else if (strstr(line, "Inde/SNP+MNP ratio") != NULL)
strcpy(record[i].Indel_SNPMNPRatio, str1);
printf("%s: %s\n", name, str1); //puts() always adds newline at the end of the string
//printf("%d\n", i);
i++; //increment of record count
if (i > 8) exit (EXIT_FAILURE); //truncate the input file, need improved
}
fclose(fPtr);
return 0;
}