I have created a simple project using Visual Studio 2012, and the code was compiled successfully, ran, and printed out as I expected (The fruit name is: apple). When I try to compile the same code with gcc, the print out looks different (The fruit name is: apple'). Where does the ' come from? Below is the code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * getCharValue(char line[]){
char *pch = strchr(line, '='); //find where the equal sign is in the line
char *pch1 = strchr(line, '\0'); // find the index of the end of the line
char *info = (char *) malloc(sizeof(char) * (pch1-pch-4));
info[pch1-pch-5] = '\0';
// copy the value to a new array then return the 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 this line is not empty or a 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 the input file.");
exit(-1);
}
fclose(fid);
return 0;
}
The text file just contains 1 line as:
fruit_name = 'apple'
Is there anything wrong with my code? How come it works with Visual Studio with no problem but cannot produce the right result using gcc?
You have delimited with ' but never check for them, and never check for a badly formatted file. Here is my revision. Note I don't cast malloc() and I 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 where the equal sign is in the line
if (pch == NULL)
return NULL;
pch1 = strchr(pch+1, '\''); // find the following quote mark
if (pch1 == NULL)
return NULL;
pch2 = strchr(pch1+1, '\''); // find the following quote mark
if (pch2 == NULL)
return NULL;
// copy the value to a new array then return the 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 this line is not empty or a 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 was badly formatted\n");
else
printf("The fruit name is: %s\n", nameValue);
}
}
}
}
else{
printf("Can not find the input file.");
exit(-1);
}
fclose(fid);
free(nameValue); // won't matter if it is NULL
return 0;
}
Program output
The fruit name is: apple
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With