>> 1) If your file fails to open, you still end up reading from it anyway. <<
This thread is like that puzzle, count the fs in the paragraph, that goes around periodically. Due to the spacing, our brains absolutely refuse to accept that the closing brace under fgets is matching the opening brace at the end of the if rather than closing the else.
So, sdogg45, here is your code with some semblence of decent formatting. (The problems are still there). Try to use it or something similar as example formatting and it will make your code much easier to read (and write).
Code:#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 50
struct data
{
int id;
char title [SIZE];
char gene [SIZE];
char express [SIZE];
char chrom [SIZE];
int scount;
};
int main ()
{
int i = 0;
int j = 0;
struct data genedata[SIZE];
FILE * dataPtr;
char temp [SIZE];
char line [SIZE];
if(( dataPtr = fopen("Hs.data.filtered", "r")) == NULL)
{
printf("File could not be opened.\n");
}
else
{
fscanf(dataPtr, "%s", temp);
if(strcmp(temp, "//") != 0)
{
fgets(line, SIZE, dataPtr);
}
while(!feof(dataPtr))
{
if (strcmp(temp, "ID") == 0)
{
genedata[i].id = atoi(line);
}
if (strcmp(temp, "TITLE") == 0)
{
strcpy(genedata[i].title, line);
}
if (strcmp(temp, "GENE") == 0)
{
strcpy(genedata[i].gene, line);
}
if (strcmp(temp, "EXPRESS") == 0)
{
strcpy(genedata[i].express, line);
}
if (strcmp(temp, "CHROMOSOME") == 0)
{
strcpy(genedata[i].chrom, line);
}
if (strcmp(temp, "SCOUNT") == 0)
{
genedata[i].scount = atoi(line);
}
i++;
fscanf (dataPtr, "%s", temp);
if (strcmp(temp, "//") != 0)
{
fgets (line, SIZE, dataPtr);
}
} /*close while*/
if(( dataPtr = fopen( "records.dat", "w")) == NULL)
{
printf("error");
}
else
{
for (j = 1;j <= i;j++)
{
fwrite(&genedata, sizeof(struct data), j, dataPtr);
}
}
fclose (dataPtr);
} /*close else*/
return 0;
}