[SOLVED] C - Function only returns value if an irrelevant printf is in the function.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C - Function only returns value if an irrelevant printf is in the function.
Code:
char * file_search_func(FILE * file, const char * searchstr,
const char * delimiter, const int field)
{
// declare and initialize
char * token = NULL;
char * retval = NULL;
char buffer[256];
int counter = 0;
int keepgoing = 0;
// loop until find identifier in string. typically first field
// tokenize once found
while (keepgoing == 0) {
if (fgets(buffer, 256, file)) {
// printf("indeed\n");
if (strstr(buffer, searchstr)) {
// tokenize and begin processing
token = strtok(buffer, delimiter);
while (token) {
// if proper value copy to pointer
if (counter == field) {
retval = token;
keepgoing = 1;
break;
} else {
// else continue to next token until found
counter++;
token = strtok(NULL, delimiter);
}
}
}
}
}
// reset file to beginning
rewind(file);
return(retval);
}
I am so lost right now. I get my expected return value but only if the printf("indeed\n"); is uncommented. If it's commented nothing comes up on the terminal... I have a feeling its going to be staring me in the face but I simply cannot comprehend how a printf statement changes how everything else works :/
Honestly, your returning of a char pointer just smells unsafe to me. My concern is: does the memory address pointed to by the pointer still contain the same thing after the function returns? It looks like the answer is "not necessarily", because it points to a stack-allocated buffer that gets destroyed at the return statement. I changed it to allocate a string for the return value and then return that, and that works without the stray printf.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *file_search_func(FILE *file, const char *searchstr,
const char *delimiter, const int field)
{
// declare and initialize
char *token = NULL;
char *retval = NULL;
char buffer[256];
int counter = 0;
int keepgoing = 0;
// loop until find identifier in string. typically first field
// tokenize once found
while (keepgoing == 0)
{
if (fgets(buffer, 256, file))
{
if (strstr(buffer, searchstr))
{
// tokenize and begin processing
token = strtok(buffer, delimiter);
while (token)
{
// if proper value copy to pointer
if (counter == field)
{
retval = token;
keepgoing = 1;
break;
}
else
{
// else continue to next token until found
counter++;
token = strtok(NULL, delimiter);
}
}
}
}
}
// reset file to beginning
rewind(file);
char *s = (char *)malloc(strlen(retval) * sizeof(char) + 1);
strcpy(s, retval);
return s;
}
int main()
{
// a.txt contains one line:
// b;
FILE *f = fopen("a.txt", "r");
char *s = file_search_func(f, "b", ";", 0);
printf("%s\n", s);
free(s);
fclose(f);
}
Thank you much. I was afraid of that. I had it working a similar way and was trying to eliminate having to call free outside the function. At least it now doesn't rely on a pass by reference.
Additional minor suggestion: You can use while (retval == NULL) instead of while (keepgoing == 0), and then the keepgoing variable is redundant.
By the way, since 0 is usually considered false, your use of while (keepgoing == 0) looks very confusing to me (i.e., it reads like "if not keepgoing then do keep going, today is opposite day!").
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.