Click to See Complete Forum and Search --> : An infinite loop perhaps? (using C)


prince_kenshi
03-22-2001, 04:55 PM
I'm trying to create a program that will examine mpeg files and identify what layer of encoding it uses. Since mpegs have multiple segments with multiple headers, I'm making it to search for each header and identify the layering. It finds the headers by finding the 11 sync bits and the identification is masking. But when I run the program on a certain mp3, it prints a single "Layer 3" and does nothing afterward, although there is cpu activity. A simple ctrl c breaks out of it. Now I'm sure, as simple as the program is, it wouldn't take long enough for me to go to the kitchen and get something to drink before getting to the second header. I suspect it's hanging in an infinite loop somewhere. Here's the code:


#include <stdio.h>
#include <stdlib.h>

int main()
{
char filename[60];
FILE *filePtr;
short int character;
short int mask_char;
printf("Enter filename: ");
scanf("%s", filename);
filePtr = fopen(filename, "rb");
if(filePtr == NULL)
{
printf("Failed to open file.");
exit(0);
}
for(character = getc(filePtr); character != EOF ;)
{
if(character==255)
{
character = getc(filePtr);
if((character & 224) == 224)
{
character &= 6;
if(character == 6)
printf("Layer 1\n");
if(character == 4)
printf("Layer 2\n");
if(character == 2)
printf("Layer 3\n");
if (character == 0)
printf("Reserved");
}
}
}
fclose(filePtr);
return 0;
}


Gracias.

BrianDrozd
03-22-2001, 05:07 PM
Yeah, it looks like an infinite loop to me.



for(character = getc(filePtr); character != EOF ;)

[/b]<HR></BLOCKQUOTE>
What your missing here is the increment. It loads the character here, and then never updates it again. Try:

for(character = getc(filePtr); character != EOF ;character = getc(filePtr))


That should do it. (Though, personally, I'd switch the whole for loop to a while loop. Just a stylistic thing, but you really shouldn't modify the index of a for loop once you've entered it. It's too easy to screw up for loops by modifying their indexes like that. Better to use a while loop or repeat loop for this stuff.)

[ 22 March 2001: Message edited by: BrianDrozd ]

prince_kenshi
03-22-2001, 05:56 PM
Thanks man. It's little things like that... I haven't used C in about a year and I'm having to refresh myself of a lot of things. The program seems to work although it didn't give quite the results I had expected.