I know this should be an easy function to implement. I need to parse a .txt file and put it into a 2D array. In order to initialize the 2D array, I will need to know the number of lines and the number of characters in the longest line. I believe I have a function that will give me the number of lines. I just can not seem to get my function to find the length of the longest line to work.
Here is my code so far. I know it is wrong(as it is not working).
int getrow(FILE *mapfile)
{
int chars =0 ;
int longest = 0;
int Hlen;
char *line;
char *buf;
line = malloc(1000); //1000 is set to be max chars in line
line = fgets(line, 1000, mapfile); //get first line of file
while(line)
{
chars = 0;
buf = strtok(line, " ");
while(buf)
{
chars++;
buf = strtok(NULL, " ");
}
(chars > longest)?(longest = chars):(longest==longest); //If the number of characters scanned is longer than previous long, save new longest line
line = fgets(line, 1000, mapfile);
}
Hlen = longest;
free(line);
return Hlen;
}
The file I am parsing is:
###################
# # F####################################
# # E# #
# ##### ################################## #
# # # #
############################ ################# # #
# # # #
# ################## # # #
# ##### # # #
# ############### # # # #
# # # # # #
################### ########################## # #
#L # # #
# # # ######################## #
# # # #
############ # # ##########################
# # # #
# ####### #
# # # #
# # # #
# # # S#
#########################