I'll explain my issue in this way.

I've use a structure to get some data of a byte stream as follows.

1. struct pac_cont
   2. {
   3.     unsigned int des_list ;
   4.     unsigned int mem_ID ;
   5.     unsigned char dm_con ;
   6.     unsigned char ser_ID ;
   7.     unsigned short act ;
   8.  };

Now I want to write some values finding by using above variables, to a text file, of a function. Try it as follows.

1. void dataExtract(int length, char *buffer)
   2. {
   3.      struct pac_cont* p = (struct pac_cont*)buffer;
   4.        
   5.      printf("dest %d member %d data %d ser %d act %d\n",p->des_list,p->mem_ID,p->dm_con,p->ser_ID,p->act) ;
   6.        
   7.      ofstream filedata ;
   8.      filedata.open( "RecordData.txt", ios::app ) ;
   9.        
  10.     if(filedata.is_open())
  11.     {
  12.              filedata << p->des_list << "\t" << p->mem_ID << "\t" << p->dm_con << "\t"<< p->ser_ID << "\t" << p->act << "\n";
  13.               
  14.              filedata.close() ;
  15.      }
  16.     else
  17.    {
  18.              filedata << "Wrong\n" ;
  19.     }
  20. } 

That printf gives the exact values I need on the command prompt. But the line filedata give some annoying output to the text file. I know that how format the output in printf, but no idea how to use in filedata

Someone can give a clue to me.
Thanks

Recommended Answers

All 9 Replies

%d will treat a variety of similar types as all being int, including chars.

When you << a char, that is exactly what you'll get, a char. If that happens to be a non-printable char, then your output file will appear funny.

Try casting your chars to ints before outputting them

You mean simple explicit conversion, like (int) on struct pointer?

I've try it and still checking the result with actual one.

By the time I've try to format the output in setbase() of <iomanip> for base 10(because typically it is integer value we use). But it not works. What can be issue there.

So you did this?

filedata << p->des_list << "\t" << p->mem_ID << "\t" 
         << (int)p->dm_con << "\t"<< (int)p->ser_ID << "\t" << p->act << "\n";

Yes, that is what I've try.

Dunno then, it works here

#include <iostream>

int main() {
    char c = 'A';
    std::cout << c << std::endl;
    std::cout << (int)c << std::endl;
    return 0;
}

$ g++ -W -Wall -ansi -pedantic  foo.cpp
$ ./a.exe
A
65

Bear in mind that you're appending to the file, so any new results as a result of the code change will be at the END of the file.
Don't keep staring at the start of the file :)

Dunno then, it works here

#include <iostream>

int main() {
    char c = 'A';
    std::cout << c << std::endl;
    std::cout << (int)c << std::endl;
    return 0;
}

$ g++ -W -Wall -ansi -pedantic  foo.cpp
$ ./a.exe
A
65

Thanks, actually why I'm confusing with this topic is that struct are really new for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.