0

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#
                #########################
  • 3
    that ternary just...stinks. `longest = (chars > longest) ? chars : longest`. don't "execute code" or do assignments inside the ternary itself. – Marc B Dec 01 '15 at 21:55
  • 4
    use `chars = strlen(line)` instead of yours strtok loop – Iłya Bursov Dec 01 '15 at 22:01
  • So I removed the " buf = strtok(line, " "); " and the while loop below it and replaced it with chars = strlen(line) – Keith Compton Dec 01 '15 at 22:07
  • I also changed longest = (chars > longest) ? chars : longest With a IF function! – Keith Compton Dec 01 '15 at 22:07
  • `(chars > longest)?(longest = chars):(longest==longest);` ? "if chars is greater than longest, assign value of chars to longest; else compare longest with itself and throw away the result" – Erik Nyquist Dec 01 '15 at 23:16

1 Answers1

1
  1. as pointed out by Lashane, you can use strlen to get the numbers of
    characters in the line.

  2. remove that ternary statement. All you need to do is check whether chars is greater than longest, and if so, update longest.

  3. No need for the Hlen variable. just return longest

  4. BONUS: There's also no need to allocate memory. Think about it, you don't actually need to save a copy of the line, you just need to go through each character of the file and count the characters between newlines. You can use fgetc for this.

Community
  • 1
  • 1
Erik Nyquist
  • 1,267
  • 2
  • 12
  • 26