I am trying to access a string.

wchar_t *str;
char *Pname;
FILE *fp;

str = somefunction();

Pname = str;

fp = fopen ("/tmp/test","a+");

fprintf(fp,"%s\n",Pname);

fclose(fp);

str actually contains "ABigName", but the file simply contains the first letter, "A".

If char could represent the same thing as wchar_t then there wouldn't be two different types of characters. You have a wide string, and thus you need to either convert it to a narrow string iff you're sure that the narrow string can represent it, or properly display the wide string as a wide string:

fwprintf(fp, L"%s\n", str);

I'm afraid it doesn't work. :(

Breakpoint 1, read_sections_map (dat=0x7fffffffd570, size_comp=1396, size_uncomp=3092, correction=4)
    at decode_r2007.c:732
732       Pname=section->name;
(gdb) n
733       logptr = fopen("/tmp/test.csv", "a+");
(gdb) n
734       fwprintf(logptr,L"%s\n",section->name);
(gdb) print Pname
$1 = 0x6f2710 "A"
(gdb) print *Pname
$2 = 65 'A'
(gdb) print *section->name
$3 = 65 L'A'
(gdb) print section->name
$4 = 0x6f2710 L"AcDb:FileDepList"

I'm afraid it doesn't work. :(

Pname will not work. That's a given, so remove that variable entirely; you can't expect it to represent a wide string. Does your log file still contain just "A" when you use fwprintf() to write section->name?

Edited 3 Years Ago by deceptikon

Okay, that makes sense.

Yes, the log file just contains the first letter of the section->name variable.

cat /tmp/test.csv 
A
A
A
A
A

Edited 3 Years Ago by davecoventry: Adding contents of test.csv

Problem sorted.

Using %ls correctly printed the wchar_t to file: fprintf(logptr,"%ls\n",section->name);

As a side issue, you cannot apparently mix fprintf with fwprintf using the same FILE pointer.

Using %ls correctly printed the wchar_t to file: fprintf(logptr,"%ls\n",section->name);

Yes, you're correct. I forgot that fwprintf() doesn't default to wide orientation for %s. My bad.

This question has already been answered. Start a new discussion instead.