binary output with c++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: binary output with c++

  1. #1
    Join Date
    Oct 2003
    Posts
    3

    binary output with c++

    I've just begun programming in c++ a couple of weeks ago, and i'm trying different things.
    But I'm having problems getting a good output in the binary output files, when there is a binary and a non-binary input.
    The programm stays in a loop when I execute it.
    I don't understand why, so I could realy use some help.

    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstring>
    using namespace std;

    struct klbstd
    {
    int klnr;
    char klsrt[1];
    char klnaam[25];
    char kladr[25];
    char klpstcd[6];
    char klplts[15];
    long klbnr;
    long klgiro;
    char mutcode[1];
    char tariefaf[1];
    };

    const int HIGHVALUE = 99999;


    void writeKlantbestand(ofstream& outputFile2, int klnr, char klsrt[], char klnaam[], char kladr[],
    char klpstcd[], char klplts[], long klbnr, long klgiro, char mutcode[], char tariefaf[]);
    void writeRecord(char best1[]);
    void uitvoer(char best1[], char best2[], char best3[], char best4[], char best5[], char best6[]);
    void klbnieuw(char best1[], char best4[]);
    void controleerdatum(char best7[]);

    int main()
    {
    writeRecord("B0100.dat");
    controleerdatum("B0001.csv");
    uitvoer("B0100.dat", "B1000.txt", "V0100.txt", "klantb-nieuw.dat", "B0101.dat", "constant.txt");
    return 0;
    }


    void writeKlantbestand(ofstream& outputFile2, int klnr, char klsrt[], char klnaam[], char kladr[],
    char klpstcd[], char klplts[], long klbnr, long klgiro, char mutcode[], char tariefaf[])
    {
    klbstd bes;
    bes.klnr = klnr;
    strcpy(bes.klsrt, klsrt);
    strcpy(bes.klnaam, klnaam);
    strcpy(bes.kladr, kladr);
    strcpy(bes.klpstcd, klpstcd);
    strcpy(bes.klplts, klplts);
    bes.klbnr = klbnr;
    bes.klgiro = klgiro;
    strcpy(bes.mutcode, mutcode);
    strcpy(bes.tariefaf, tariefaf);
    outputFile2.write( (char *) &bes, sizeof(klbstd));
    }

    void writeRecord(char best1[])
    {
    ofstream outputFile;

    outputFile.open(best1, ios::binary);
    if (outputFile.fail())
    {
    cout << "Het bestand: " << best1 << " bestaat niet\n";
    exit(3);
    }

    writeKlantbestand(outputFile, 03451, "A", "Jan", "Weg 13", "4232fd", "Niemendal", 2468567805, 4466706, " ", " ");
    writeKlantbestand(outputFile, 03736, "B", "Piet", "Straat 15", "4674jj", "Niemendal", 4746364853, 6566848, " ", " ");
    writeKlantbestand(outputFile, 04045, "A", "Tina", "Baan 17", "4577gf", "Niemendal", 6736747342, 6646868, " ", " ");
    writeKlantbestand(outputFile, 04356, "C", "Nico", "Allee 19", "2355hj", "Niemendal", 6764575883, 64686486, " ", " ");
    writeKlantbestand(outputFile, 04555, "A", "Klaas", "Laan 21", "8796ff", "Niemendal", 8468486489, 14644302, " ", " ");
    writeKlantbestand(outputFile, 04657, "C", "Hans", "Weg 23", "5443ty", "Niemendal", 1468644634, 2443665, " ", " ");
    writeKlantbestand(outputFile, 05745, "E", "Mike", "Straat 75", "4578fy", "Niemendal", 1246846468, 9036362, " ", " ");
    writeKlantbestand(outputFile, 05777, "B", "Erik", "Baan 3", "2345se", "Niemendal", 4674675652, 6376230, " "," ");
    writeKlantbestand(outputFile, 06534, "D", "Johan", "Allee 9", "4678fg", "Niemendal", 6468466845, 1353566, " "," ");
    writeKlantbestand(outputFile, 07510, "A", "Klaas", "Straatje 51", "2346gh", "Niemendal", 1468468643, 4143243, " ", " ");
    writeKlantbestand(outputFile, 07635, "D", "Henk", "Laaantje 23", "3467rt", "Niemendal", 1468864644, 2235294, " ", " ");
    writeKlantbestand(outputFile, HIGHVALUE, "", "", "", "", "", 1, 1, "", "");

    outputFile.close();
    }

    void controleerdatum(char best7[])
    {
    ifstream invoerdatum;
    invoerdatum.open(best7);
    if (invoerdatum.fail())
    {
    cout << best7 << "kon niet worden geopend" <<endl;
    exit(1);
    }
    char lees;
    invoerdatum.get(lees);
    int komma = 0;
    while (komma < 6)
    {

    if(lees == ';')
    {
    komma++;
    }
    invoerdatum.get(lees);
    }
    char zaterdag = '6';
    char zondag = '7';
    if (lees == zaterdag || lees == zondag)
    {
    cout << "Het is weekend dus party time" << endl;
    exit(2);
    }
    invoerdatum.close();
    }

    void leesinput1(ifstream& input1, klbstd& klbstdOud, int& klnrinput1)
    {
    input1.read( (char *) &klbstdOud, sizeof(klbstd));
    klnrinput1 = klbstdOud.klnr;
    if(input1.eof())
    klnrinput1 = HIGHVALUE;
    }

    void leesinput2(ifstream& input2, int klnr, char klsrt[], char klnaam[], char kladr[], char klpstcd[], char klplts[], long klbnr, long klgiro, char mutcode[], char tariefaf[])
    {
    input2.ignore(1,';');

    input2 >> klnr;
    input2.ignore(100,';');


    input2 >> klsrt;
    input2.ignore(100,';');

    input2 >> klnaam;
    input2.ignore(100,';');

    input2 >> kladr;
    input2.ignore(100,';');

    input2 >> klpstcd;
    input2.ignore(100,';');

    input2 >> klplts;
    input2.ignore(100,';');

    input2 >> klbnr;
    input2.ignore(100,';');

    input2 >> klgiro;
    input2.ignore(100,';');

    input2 >> mutcode;
    input2.ignore(100,';');

    input2 >> tariefaf;
    input2.ignore(100,';');

    if (input2.eof())
    klnr = HIGHVALUE;
    }

    void uitvoer(char best1[], char best2[], char best3[], char best4[], char best5[], char best6[])
    {
    ifstream input1;
    ifstream input2;
    ifstream input3;
    ofstream output1;
    ofstream output2;
    ofstream output3;
    klbstd bes1, bes2;

    int klnrinput1=0;
    int klnrinput2=0;
    klbstd klbstdOud;


    int komma = 0;

    input1.open(best1, ios::in | ios::binary);
    if (input1.fail())
    {
    cout << best1 << " kon NIET worden geopend.\n";
    exit(1);
    }

    input2.open(best2);
    if (input2.fail())
    {
    cout << best2 << " kon NIET worden geopend.\n";
    exit(1);
    }

    input3.open(best6);
    if (input3.fail())
    {
    cout << best6 << " kon NIET worden geopend.\n";
    exit(1);
    }

    output1.open(best3);

    if (output1.fail())
    {
    cout << best3 << " kon NIET worden geopend.\n";
    exit(1);
    }

    output2.open(best4, ios:ut | ios::binary);
    if (output2.fail())
    {
    cout << best4 << " kon NIET worden geopend.\n";
    exit(1);
    }

    output3.open(best5, ios:ut | ios::binary);
    if (output3.fail())
    {
    cout << best5 << " kon NIET worden geopend.\n";
    exit(1);
    }


    output1 << setw(3) << "Kantoor: Arnhem FOUTVERSLAG datum: 13-10-2003 blad: 1\n";
    output1 << "\n";
    output1 << setw(3) << "Klantnummer" << " Mutatie"<<" Foutmelding\n";
    output1 << "----------- ---------- --------------------------------\n";

    input1.read((char *) &bes1.klnr , sizeof(bes1.klnr));
    input2.read((char *) &bes2.klnr , sizeof(bes2.klnr));
    leesinput1(input1, klbstdOud, klnrinput1);
    leesinput2(input2, bes2.klnr, bes2.klsrt, bes2.klnaam, bes2.kladr, bes2.klpstcd, bes2.klplts, bes2.klbnr, bes2.klgiro, bes2.mutcode, bes2.tariefaf);


    while (klnrinput1 != HIGHVALUE || klnrinput2 != HIGHVALUE)
    {


    if (bes2.klnr == bes1.klnr)
    {
    output2 << setiosflags(ios::left)<<setw(8)<<bes1.klnr <<setw(8) <<bes1.klsrt<< setw(8) <<bes1.klnaam << setw(8) <<bes1.kladr <<
    setw(8)<<bes1.klpstcd <<setw(8) << bes1.klplts << setw(8) <<bes1.klbnr << setw(8) <<bes1.klgiro << setw(8) <<bes1.mutcode<<setw(8)<<bes1.tariefaf<<endl;
    output3 << setiosflags(ios::left)<<setw(8)<<bes1.klnr <<setw(8) <<bes1.klsrt<< setw(8) <<bes1.klnaam << setw(8) <<bes1.kladr <<
    setw(8)<<bes1.klpstcd <<setw(8) << bes1.klplts << setw(8) <<bes1.klbnr << setw(8) <<bes1.klgiro << setw(8) <<bes1.mutcode<<setw(8)<<bes1.tariefaf<<endl;
    leesinput2(input2, bes2.klnr, bes2.klsrt, bes2.klnaam, bes2.kladr, bes2.klpstcd, bes2.klplts, bes2.klbnr, bes2.klgiro, bes2.mutcode, bes2.tariefaf);
    }

    else

    if (bes2.klnr > bes1.klnr)
    {
    output2 << setiosflags(ios::left)<<setw(8)<<bes1.klnr <<setw(8) <<bes1.klsrt<< setw(8) <<bes1.klnaam << setw(8) <<bes1.kladr <<
    setw(8)<<bes1.klpstcd <<setw(8) << bes1.klplts << setw(8) <<bes1.klbnr << setw(8) <<bes1.klgiro << setw(8) <<bes1.mutcode<<setw(8)<<bes1.tariefaf<<endl;
    leesinput2(input2, bes2.klnr, bes2.klsrt, bes2.klnaam, bes2.kladr, bes2.klpstcd, bes2.klplts, bes2.klbnr, bes2.klgiro, bes2.mutcode, bes2.tariefaf);

    }
    else

    if (bes2.klnr < bes1.klnr)
    {
    cout <<"Dit is geen goede record"<<endl;
    exit(1);
    }

    }

    input1.close();
    input2.close();
    input3.close();
    output1.close();
    output2.close();
    output3.close();

    }

  2. #2
    Join Date
    Feb 2002
    Posts
    5,758
    Post the loop only.

    Kuphryn

  3. #3
    Join Date
    Jan 2000
    Location
    Sweden
    Posts
    197
    I don't see any way for the loop condition (klnrinput1 & 2) to change inside the loop.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center