Hi, I've written a function that scans text in an open file and returns the number of lines, as well as the largest line width. I want to use this information to create a two dimensional array to store the text. However, my program behaves strangley and I don't know why. When I run the program it just freazes. I put a printf() line in the loop of the function to see if anything was happening, and the iCharCount variable just adds continuosly without reseting. I'm pretty new at C, but as far as I can tell this should work. I think the problem is with reading the EOF, since if I replace iCharCount in the printf() function with the character that was just read(ch), it actualy does print the text on the screen, but then gets scrolled up as printf() prints nothing.
heres the function code:
At this point the file is already opened using:
Code:
FILE *file ;
if((file = fopen("test.txt","r")) == NULL)
return(1) ;
Code:
int Scan_File(FILE *file,int *iNumLines, int *iLineWidth)
{
int iCharCount = 0 ;
unsigned char ch = ' ' ;
if(file == NULL)
return 0;
*iNumLines = 0 ;
*iLineWidth = 0 ;
while((ch = fgetc(file)) != EOF)
{
if(ch == '\n')
{
iLineCount++ ;
if(iCharCount > *iLineWidth)
*iLineWidth = iCharCount ;
iCharCount = 0 ;
*iNumLines++ ;
}
else
iCharCount++ ;
printf("\n%d",iCharCount) ;// I added this to see if something was actualy
// happening.
}
printf("Got this far") ;
I would appreciate any info on this thanks.