c - String print out differently in Visual Studio and gcc -


i have created simple project using visual studio 2012, , code compiled successfully, ran, , printed out expected (the fruit name is: apple). when try compile same code gcc, print out looks different (the fruit name is: apple'). ' come from? below code:

#define _crt_secure_no_warnings #include <stdio.h> #include <string.h> #include <stdlib.h>   char * getcharvalue(char line[]){     char *pch = strchr(line, '='); //find equal sign in line     char *pch1 = strchr(line, '\0'); // find index of end of line     char *info = (char *) malloc(sizeof(char) * (pch1-pch-4));     info[pch1-pch-5] = '\0';     // copy value new array return new char;     memcpy(info, &line[pch-line+3], pch1-pch-5);     return info; }  int main() {        char namefilename[] = "name.txt";       //register file name;     char *namevalue;     file *fid = fopen(namefilename, "r");     if (fid != null){            char line[150];         while(fgets(line, sizeof(line), fid) != null){                //if line not empty or comment line             if ((strncmp(line, "//", 2) != 0) && (line[0] != '\n')) {                    if (strstr(line, "fruit_name") != null){                     namevalue = getcharvalue(line);                     printf("the fruit name is: %s\n", namevalue);                 }             }         }     }     else{         printf("can not find input file.");         exit(-1);     }     fclose(fid);     return 0; } 

the text file contains 1 line as:

fruit_name           = 'apple' 

is there wrong code? how come works visual studio no problem cannot produce right result using gcc?

you have delimited ' never check them, , never check badly formatted file. here revision. note don't cast malloc() , don't use sizeof(char).

#include <stdio.h> #include <string.h> #include <stdlib.h>   char * getcharvalue(char line[]){     char *pch, *pch1, *pch2, *info;     int len;     pch = strchr(line, '=');  // find equal sign in line     if (pch == null)         return null;     pch1 = strchr(pch+1, '\''); // find following quote mark     if (pch1 == null)         return null;     pch2 = strchr(pch1+1, '\''); // find following quote mark     if (pch2 == null)         return null;     // copy value new array return new char;     len = pch2 - pch1 - 1;     if (len <= 0)         return null;     info = malloc(len+1);     memcpy(info, pch1+1, len);     info[len] = '\0';     return info; }  int main() {        char namefilename[] = "name.txt";       //register file name;     char *namevalue = null;   //default     file *fid = fopen(namefilename, "r");     if (fid != null){            char line[150];         while(fgets(line, sizeof(line), fid) != null){                //if line not empty or comment line             if ((strncmp(line, "//", 2) != 0) && (line[0] != '\n')) {                    if (strstr(line, "fruit_name") != null){                     namevalue = getcharvalue(line);                     if (namevalue == null)                         printf("the file badly formatted\n");                     else                         printf("the fruit name is: %s\n", namevalue);                 }             }         }     }     else{         printf("can not find input file.");         exit(-1);     }     fclose(fid);     free(namevalue);    // won't matter if null     return 0; } 

program output

the fruit name is: apple 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -