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.

That function is kind of scary. What exactly is it supposed to accomplish? I'm reasonably sure I can show you a better way to solve the problem.

Well, as I said in my original post, the function is supposed to read data from a file and store it into three different arrays. The first string of each line goes into array 'a', the second string goes into array 'b' and the third into 'c'. That's all I need to get done at this point. Once the data in the file is stored in three different arrays, I can manipulate it any way I want.

>Well, as I said in my original post
Then you should name the function read_data_from_a_file_and_store_it_in_three_arrays rather than CompareIDs. I didn't ask you what your code does, I asked you what the code is meant to accomplish at a higher level. Somebody is going to call CompareIDs, why?

>Once the data in the file is stored in three different
>arrays, I can manipulate it any way I want.

At two million lines, the file is reasonably large. You're reaching the point where a braindead approach like allocating memory for all of the data isn't likely to be the best option.

I can tell you exactly how to do what you asked for, and it'll be both shorter and higher quality. But I get the strong impression that what you're asking for isn't really what you want. Any idiot can read strings into arrays. Your code is broken for a reason, and I'm assuming the reason isn't because you're an exceptional idiot.

Wow! Didn't mean to tick you off lady :)
Sorry if I offended you in any way.

Coming to the issue at hand, you are right. There could be shorter ways of doing it. I'm trying out some other stuff. Will get back to posting if that doesn't work either.

>Wow! Didn't mean to tick you off lady
You didn't tick me off. Apparently you're not used to my direct style of help.

>Sorry if I offended you in any way.
The only thing that offends me is that you're wasting my time. You ask for help, offer no useful information when prompted, then run off to try "other stuff" when somebody is clearly willing to help.

So you go try your "other stuff". Just don't be surprised if I'm not interested in helping you when you return.

andy is damned and determined to pound those square pegs into some round holes.

bummer. i was looking forward to some cool Narue code.

.

This question has already been answered. Start a new discussion instead.