Hi,
I'm trying to read data from a text file and store it into an array. I know this issue has been dealt with earlier, but none of those threads seem to be helpful in this case. Sorry in advance if I missed a relevant post.
The data in the text file is in the following format:
23800688,00100001,10000000
23792560,00100001,10000001
761528061,00100001,10000002
37840534,00100001,10000003
It's about 2 million lines and the strings include alphabets at times like 1000034e, etc.
Declaring the arrays in the middle of the program's body after calculating their length is throwing an error. So, in order to declare it in the beginning, I am using calloc for dynamic memory allocation to the arrays. Then I'm using fgets and sscanf for reading the data from the file and storing them in three different arrays. The first number of every line goes into array, a, the second one goes into b and the third one into c.
PUBLIC Sint_t CompareIDs()
{
FILE * fp2;
char ch, ch2;
unsigned long r=0, m=0, p=0;
unsigned long filesiz = 0, n = 0, n2 = 0;
char * pdndcid, * pdbid1, * pdbid2;
char * pData;
fp2 = fopen("D:/share/textfile.txt", "r");
while(1)
{
ch = getc(fp2);
LABEL1: if (ch == ',' || ch == '\n')
filesiz = filesiz + 1;
else if (ch == EOF)
{
ch = getc(fp2);
if (ch == EOF)
goto LABEL2;
else
goto LABEL1;
}
}
LABEL2: rewind (fp2);
while(1)
{
ch2 = getc(fp2);
if (ch2 == EOF)
{
ch2 = getc(fp2);
if (ch2 == EOF)
goto LABEL3;
}
p++;
}
LABEL3: n2 = filesiz/3;
n = p;
pdndcid = (char *) calloc((n2+1), sizeof(char)*10);
pdbid1 = (char *) calloc((n2+1), sizeof(char)*10);
pdbid2 = (char *) calloc((n2+1), sizeof(char)*10);
pData = (char *) calloc((n+1), sizeof(char));
printf("The array length is %lu\n", n);
rewind (fp2);
if (pData != NULL && pdndcid != NULL && pdbid1 != NULL && pdbid2 != NULL)
while (fgets(pData, n, fp2))
if (sscanf(pData, "%s, %s, %s", &pdndcid[r], &pdbid1[r], &pdbid2[r]) == 3)
++r;
printf("r = %lu\n", r);
free(pData);
printf("The contents of the arrays are as follows.\n");
printf("Array dndcid[] = ");
for(m=0; m<6; m++)
printf("%s ", pdndcid[m]);
printf("\n");
free(pdndcid);
printf("Array dbid1[] = ");
for(m=0; m<6; m++)
printf("%s ", pdbid1[m]);
printf("\n");
free(pdbid1);
printf("Array dbid2[] = ");
for(m=0; m<6; m++)
printf("%s ", pdbid2[m]);
printf("\n");
free(pdbid2);
fclose (fp2);
return 0;
}
The function is called in main by a simple call: b = CompareIDs();
The Error:
I get an access violation and a popup asking me to break the execution of the program. Line by line debugging led me to believe that the issue may be with sscanf or fgets.
I'll appreciate any suggestions. Thanks in advance.