binary file creation

i have a problem with the program creating a file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct record{
           int id;
           double cost;
           double price;
           char descr[36];
    };
int processFile(const char *inFile, const char *outFile)
{
    
    int dat=0;
    char lox[10]={'P'};
    ifstream ifile;
    fstream ofile;
    ofile.open(outFile, ios::binary | ios::out | ios::trunc);
    ifile.open(inFile);
    ofile.write(lox,10);
    record data;
    int errorcode=0;
    if (!ifile.is_open()) 
    {
                  errorcode=-1;
                  
    }
    if(ifile.eof())
    {
                  errorcode=-2;
    }
    
    while(!ifile.eof()&&errorcode>=0){
     if(errorcode>99)
     {
                      errorcode=-3;
     }
     else
     {
       ifile>>data.id;
       ofile.write(reinterpret_cast<char *>(&data.id),sizeof(int));
       if(data.id<dat)
       {
                       errorcode=-4;
       }
       
       else
       { 
         
         dat=data.id;
         ifile>>data.cost;
         ofile.write(reinterpret_cast<char *>(&data.cost),sizeof(double));
         if(data.cost<0)
          {
                       errorcode=-5;
          }
         else
          {
           ifile>>data.price;
           ofile.write(reinterpret_cast<char *>(&data.price),sizeof(double));
           if(data.price<0)
           {
                        errorcode=-6;
           }
           else 
           {               
             ifile.ignore();
             ifile.getline(data.descr, 37);
             ofile.write((data.descr),37);
             if(!ifile.eof()){
             cout<<setw(8)<<right<<data.id;
             cout<<" "<<right<<setprecision(2)<<setw(10)<<data.cost;
             cout<<" "<<setprecision(2)<<setw(12)<<data.price<<" ";
             printf(data.descr,36);
             errorcode++;
           }
        }
      }
     }
    }  
       cout<<endl;
    }
     
    ifile.close();
    ofile.close();
    return errorcode;
}

its supposed to create a file and write the stuff to outFile that the main program gives it from inFile
but somewhy it does not create the file
Topic archived. No new replies allowed.