Trying to create file but get weird symbol

Hello, I am trying to create a method to save some channel information from channels to a file. I am just doing some testing and ran into an issue I was wondering if anyone had any insights on. For my testing I have a function test_print() added to manager.c that I am calling from the action_setvar function, in the same file. Here is the code I have.

void test_print() { char fileName[200]; strcat(fileName, "TEST_FILE.txt"); FILE *fp; ast_log(LOG_WARNING, "Opening file %s\n\n", fileName); fp = fopen(fileName, "w"); if (fp == NULL) { ast_log(LOG_ERROR, "Failed to open output file\n"); return; } struct ast_channel *c; if (!(iter = ast_channel_iterator_all_new())) { ast_log(LOG_ERROR, "Failed to allocate the iterator\n"); return; }
    for (; c; c = ast_channel_iterator_next(iter)) {
            ast_channel_lock(c);
	fprintf(fp, "in the loop with channelName : %s\n", c->name);
            ast_channel_unlock();
            c = ast_channel_unref(c);
    }
    
    if (iter) {
            ast_channel_iterator_destroy(iter);
    }
    
    fclose(fp);
}

When I run this I see that it creates a file where the name is “TEST_FILE.txt” with some strange box symbol with a circle in each corner in front of the “TEST_FILE.txt”. If I comment out the for loop the file created is named “TEST_FILE.txt” without the weird symbol. Any idea why this symbol shows up? Looking at my logs the “Opening file” warning shown above also prints out “TEST_FILE.txt” with the symbol in front if I have the for loop, but if I comment out the for loop it prints out “TEST_FILE.txt” only.

Thanks for all the help.

You should probably ask this on the asterisk dev mailing list.

http://lists.digium.com/mailman/listinfo/asterisk-dev

You are declaring a variable like so:

char fileName[200];

And then using strcat to concat it with your “TEST_FILE.txt”. There is no guarantee that fileName will be cleared/empty in this case. It may contain random data. You can initialize it using:

char fileName[200] = "";

Or copy data into it using:

strcpy(fileName, "TEST_FILE.txt");

instead to solve it.

1 Like