开发者

Why is this string with an outer declaration getting assigned inside the loop and reset outside it?

开发者 https://www.devze.com 2023-01-23 03:09 出处:网络
I\'m using this code inside a function to get the shortest and longest string in a file. The length variables and strings are declared outside the loop. The int variables are correctly updated inside

I'm using this code inside a function to get the shortest and longest string in a file. The length variables and strings are declared outside the loop. The int variables are correctly updated inside and outside the loop, but the char* variables are only updated correctly inside.

on the last printf statement I get:

the string Zulia
 is the longest in a2.txt and has 18 chars

the string Zulia
 is the shortest in a2.txt and has 5 chars

What's going on here?

fp1 = fopen(fileName, "r"); 

        if (fp1 == NULL)
        {
            printf("Error while opening file: %s\n",fileName); 
            exit (1);
        } 



            int lengthLongestString=1;
            int lengthShortestString=1000; 

            int lengthActualString=0;

            char *longestString; 
            char *shortestString; 
            char *currentString;



        while (fgets(fileLine,  SIZE_OF_LINE, fp1) != NULL)     
        {

            if(((strcmp(fileLine, "\n") != 0)) && (strcmp(fileLine, "\r\n") != 0)){     //Validates against storing empty lines

                lineas[numeroLineas++] = strdup(fileLine);          


                  开发者_JS百科           lengthActualString=strlen(fileLine); 
                             currentString=fileLine;


                             if (lengthActualString>lengthLongestString){



                                  lengthLongestString = lengthActualString;

                                  longestString=fileLine;
                                  printf("the longest string now is %s \n",longestString);

                 } 

                 else if (lengthActualString<lengthShortestString){

                     lengthShortestString = lengthActualString;


                                 shortestString=fileLine; 
                     printf("the shortest string now is %s \n",shortestString);         
                } // END IF


            }// END IF

          } //END WHILE 

          printf("the string %s is the longest in %s and has %d chars\n",longestString, fileName, lengthLongestString );
          printf("the string %s is the shortest in %s and has %d chars\n",shortestString, fileName, lengthShortestString);


longestString and shortestString are pointers. They point somewhere. If you change the contents of somewhere, of course, the stuff the pointers point to has changed :-)

You need to allocate memory for longestString and shortestString (or define them as arrays rather than pointers) and copy the chars there.


You duplicated the string but forgot to assign that duplicate to your shortest/longest string variable and assigned a pointer to the read buffer instead.


That's because you assign shortestString and longestString to fileLine. So you are always printing the value in fileLine, which content is that of the last line in you read with fgets.

You should read up on pointers for that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号