This is a short summary of my project

make two text files with random stuff in it and then make them into bin files and then merge them into one and u take two text files and combine into one text file

the code is pretty damn long

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#define READ_MODE "rb"
#define WRITE_MODE "wb"

typedef struct inv_rec
{
    char partNo[5];
    char partName[15];
    int qtyonHand;
} INV_REC;
void Binaryfunc(FILE *BinFile1, FILE *BinFile2);

char *getData(FILE* textFile, INV_REC *txt);   
void writeBinaryFile(INV_REC* aStudent, FILE* binFile);

void main()
{
    FILE* spM1;
    FILE* spM2;
    FILE* spM3;

    int recM1;
    int recM2;
    int sentinel = INT_MAX;
    int mergeCnt = 0;

    char* textFileID = "InvFile1.txt";
    char* binFileID = "BinFile1.bin";
    char* textFileID2 = "InvFile2.txt";
    char* binFileID2 = "BinFile2.bin";

    INV_REC aStudent;

    FILE* textFile;
    FILE* binFile;

    char file1ID[] = "BinFile1.bin";
    char file2ID[] = "BinFile2.bin";
    char fileOutID[] = "BinFile3.bin";

    printf("\nBegin Binary File Creation\n");

    //INVFILE 1
    if (!(textFile = fopen(textFileID, "r")))
    {
        printf("\n\nCannot Open %s\n", textFileID);
        exit(100);
    }
    if (!(binFile = fopen(binFileID, "wb")))
    {
        printf("\n\nCannot Open %s\n", binFileID);
        exit(200);
    }

    while(getData (textFile, &aStudent))
        writeBinaryFile (&aStudent, binFile);
        //INVFILE 2 
    if (!(textFile = fopen(textFileID2, "r")))
    {
        printf("\n\nCannot Open %s\n", textFileID2);
        exit(100);
    }
    if (!(binFile = fopen(binFileID2, "wb")))
    {
        printf("\n\nCannot Open %s\n", binFileID2);
        exit(200);
    }
    while(getData (textFile, &aStudent))
        writeBinaryFile (&aStudent, binFile);


    fclose(textFile);
    fclose(binFile);

    printf("\n\nFile creation complete\n\n");

    printf("Begin File Merge: \n");

    if (!(spM1 = fopen (file1ID, READ_MODE)))
        printf("\aError on %s\n", file1ID), exit(100);

    if (!(spM2 = fopen (file2ID, READ_MODE)))
        printf("\aError on %s\n", file2ID), exit(100);

    if (!(spM3 = fopen (fileOutID, WRITE_MODE)))
        printf("\aError on %s\n", fileOutID), exit(100);

    fread(&recM1, sizeof(int), 1, spM1);

    if(feof(spM1))
        recM1 = sentinel;

    if(feof(spM2))
        recM2 = sentinel;

    while(!feof(spM1)|| !feof(spM2))
    {
        if (recM1 <= recM2)
        {
            fwrite(&recM1, sizeof(int), 1, spM3);
            mergeCnt++;

            fread(&recM1, sizeof(int), 1, spM1);

            if(feof(spM1))
                recM1 = sentinel;
        }
        else
        {
            fwrite(&recM2, sizeof(int), 1, spM3);
            mergeCnt++;

            fread(&recM2, sizeof(int), 1, spM2);
            if(feof(spM2))
                recM2 = sentinel;
        }
    }
    fclose(spM1);
    fclose(spM2);
    fclose(spM3);
    printf("\nEnd file Merge. %d item(s) merged.\n\n", mergeCnt);


}   

char *getData(FILE* textFile, INV_REC* txt)
{
    char buffer[100];
    char *feof;


    feof = fgets(buffer, sizeof(buffer), textFile);
    if(feof)
        sscanf(buffer, "%s %s %d", txt->partNo, txt->partName, &txt->qtyonHand);
        return feof;
}
void writeBinaryFile (INV_REC* aStudent, FILE* binFile)
{
    int amtWritten;

    amtWritten = fwrite(aStudent, sizeof(INV_REC), 1, binFile);

    if(amtWritten != 1)
    {
        printf("Can't write student file. Exiting\n");
        exit(201);
    }
    return;
}

can anyone help me please?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Okay, write is kind of okay. But your read isn't.

Either pass in an ASCII buffer into the function or make the buffer static. Passing in the buffer is the better solution!

You may want to use better names then getData() vs writeBinaryFile().
It's actually write a binary record!
get data implies binary but its ascii.
So maybe getAsciiRecord() or something like that!

char *getData(FILE* textFile, INV_REC* txt)
{
    char buffer[100];
    char *feof;

    feof = fgets(buffer, sizeof(buffer), textFile);
    if(feof)
        sscanf(buffer, "%s %s %d", txt->partNo, txt->partName, &txt->qtyonHand);

    return feof;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

You used void main() instead of int main(). Any parent process will have no idea if the program ran properly or not.
Try attaching to code if it's long. It make's the thread's easer to read.

This article has been dead for over six months. Start a new discussion instead.