I am trying to printf a character from a file whose byte value in hex is CC. it is printing 'ffffffCC' I am expecting 'CC" only. how can I printf a anything above the ascii code?
In case you still want to learn about Unicode, U+00CC is the character 'Ì'
On a unicode-compatible system, such as Linux, it's just a matter of printing it:
1 2 3 4 5 6 7 8 9 10 11 12
#include <cstdio>
#include <clocale>
int main()
{
// different ways to assign it
wchar_t c1 = 0xcc;
wchar_t c2 = L'\u00cc';
wchar_t c3 = L'Ì';
// printf
std::setlocale(LC_ALL, "en_US.utf8");
std::printf("U+00cc is %lc or %lc or %lc\n", c1, c2, c3);
}
On Windows, you'll need to enable _O_WTEXT on your stdout.
(although I suspect you wanted to print the hexadecimal value of your byte, in which case Unicode is indeed irrelevant, since you're not dealing with characters at all)
I though I am done but one problem came up. below is my function. and also what is the memory that I dumped where my structure is pointing. the write went OK the read was OK until I read the byte '07' and it crashed immediately after executing . readCharFromFile = (char)fgetc(fp);
Are control characters dealt differently by fgetc? what am I doing wrong?
>db /count:32 0x27fa68
0x0027FA68 cd cc bc 41 cc cc cc cc 5f 07 ce 19 51 da 3b bf
0x0027FA78 45 23 00 00 cc cc cc cc cc cc cc cc cc cc cc cc
//Write structure to a file and return that same structure after writing
struct Test *WriteStructures(conststruct Test *ptr, size_t count, FILE *fp)
{
//Variable declaration and initialization
int loopCntr;
char *head = (char*)ptr;
unsignedchar charFromStructure;
//struct Test *beginPntr = (struct Test *)ptr;
//go to the entire structure in memory byte by byte and write it to a temp file
for (loopCntr = 0; loopCntr < (int)(count * sizeof(struct Test)); loopCntr++)
{
fputc(charFromStructure = *head++, fp);
printf("%3x",charFromStructure);
}
printf("\n");
rewind(fp);
return((struct Test *)ptr);
}
//read structure to a file and return that same structure after writing
struct Test *ReadStructures(struct Test *ptr, size_t count, FILE *fp)
{
//Variable declaration and initialization
unsignedchar readCharFromFile;
char *traverserPntr = (char*)&ptr;
int loopCntr;
// read a temp file byte by byte write the byte to the structure in memory
for (loopCntr = 0; loopCntr < (int)(count * sizeof(struct Test)); loopCntr++)
{
readCharFromFile = (char)fgetc(fp);
*traverserPntr = readCharFromFile;
traverserPntr++;
printf("%3x",readCharFromFile);
}
printf("\n");
return(ptr);
}
Thank 'cubbi' it is working.
I checked the addresses of (char*)&ptr and (char*)ptr and they are not the same. I looked back on the parameters and I saw my mistake. What threw me off is it worked halfway before it crashed when reading the '07'. If you don't mind can you tell me the reason why did it crashed on the '07', I am aware that this a a control character, should my function read it anyway?
It's not because of the character value, it's because it's the 10th byte in the struct
When writing to &ptr, you were writing to the stack frame of the function ReadStructures(), that is, to the memory area on stack where the parameters are stored.
I am guessing you're on a 32-bit system, so the first four bytes clobbered ptr, the next four bytes clobbered count, the next byte (5f) changed fp. On the next loop iteration, which would have returned 07, you attempted to call fgetc() on what used to be a FILE*, but it wasn't anymore, so you got a segfault.