?
Solved

Can't output numerical values to a file

Posted on 2005-03-15
4
Medium Priority
?
234 Views
Last Modified: 2010-04-01
I am trying to output the values of an array of integer variables to a file using the following code:

ofstream outf;
outf.open(argv[4],ios::out|ios::binary);
if(outf){

      long numRecords;
      char recordType[2200];
      int val[13][2200];

               recordType[1999] = 'D';
               val[0][0] = 251658240;
               val[1][0] = 2;
               val[2][0] = 2;
               val[3][0] = 0;
               val[4][0] = 0;

               //there are additional values is this area

               recordType[1999] = 'D';
               val[0][1999] = 251658240;
               val[1][1999] = 2;
               val[2][1999] = 2;
               val[3][1999] = 0;
               val[4][1999] = 0;
               val[5][1999] = -2000;
               val[6][1999] = -4000;
               val[7][1999] = -6000;
               val[8][1999] = -8000;
               val[9][1999] = -10000;
               val[10][1999] = -12000;
               val[11][1999] = -14000;
               val[12][1999] = -16000;
               numRecords = 2000;

      for(long i = 0; i < numRecords; i++){
            outf << recordType[i] << val[0][i];
            for(int j = 1; j < 13;  j++)
                  outf << val[j][i];
      }
      outf.close();
}

But the output file ends up containing the ascii characters corresponding to these values rather than their numeric binary values.  What am I doing wrong!!!
0
Comment
Question by:DaveStahl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:greggpb
ID: 13544621
dont use the ios::binary flag
0
 
LVL 86

Accepted Solution

by:
jkr earned 750 total points
ID: 13544655
If you want to use ofstream to write binary data, you should use 'write()' instead of teh overloaded operators, e.g.

     for(long i = 0; i < numRecords; i++){
         outf << recordType[i];
         outf.write( (char*)val[0][i], sizeof(int));
         for(int j = 1; j < 13;  j++)
              outf.write( (char*)val[j][i], sizeof(int));
    }
    outf.close();
}
0
 

Author Comment

by:DaveStahl
ID: 13546683
Thanks alot!  Actually in order to work this needed to be revised slightly to:

char* valOut = new char();
for(long i = 0; i < numRecords; i++){
     outf << recordType[i];
     for(int j = 0; j < 13; j++){
         valOut = (char*)&val[j][i];
         outf.write(valOut, sizeof(int));
     }
}

Using (char*)val[i][j] saved the VALUE at val[i][j] as a pointer which caused an access violation runtime error.  &val[i][j] gave the right value.  Also I found that in order to get it to work I needed to allocate memory for the char* with the new operator.  Just coding outf.write((char*)&val[i][j],sizeof(int)); also made it crash.  But definitely thanks for pointing me in the right direction.  I was starting to go nuts trying to figure this out.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13546698
You're most welcome :o)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question