I am more than sure that my code is really bugged.
I am trying to read a binary file ,and get error and system slowdown that only restart can handle. So what I am doing wrong ?
Here is main.c
#include <stdio.h>
#include "binaryIO.h"
char filename[1024]; //this is our file
FILE* file;
char *buffer; //here is buffer where we save info
char *ibuffer; //here is numeric values transformed
int bcount = 4; //how mucht byte are we going to read per turn
int maxcount; //maximum number of chars'
int fcounter; //counts file
//transforming to dec values
char* ToInt(char* buf)
{
char *result;
char *temp;
int j;
unsigned char c;
maxcount = 0;
result = (char*)malloc(sizeof(char) * 1024 );
temp = (char*)malloc(sizeof(char) * 255);
//result = "\0";
j = 0;
for(j = 0; j < bcount; j++)
{
c = buf[j];
sprintf(temp,"%d\t",c);
//counting length of characters
memcpy(result + maxcount,temp,lstrlen(temp));
maxcount += lstrlen(temp);
}
free(temp);
return result;
}
// MAIN
int main()
{
char ts;
//reading input filename from keyboard
printf("Please type filename to test ex- <test.txt> : \n");
sprintf(filename,"%s","o754_3869_j.exe");
puts(filename);
//opening file
//getting memory equal to amount of bytes
buffer = (char*)malloc(sizeof(char) * bcount);
ibuffer = (char*)malloc(sizeof(char) * bcount);
fcounter = 0;
BFileOpen(&file,filename);
while(fcounter < 17043983)
{
buffer = BFileRBuf(&file,bcount);
ibuffer = ToInt(buffer);
// printf("try=%d buf= %.*s ibuf= %.*s \n",fcounter++,bcount,buffer,maxcount,ibuffer);
// printf("try =%d",fcounter++);
if((fcounter % 100000) == 0)
{
printf("%d\n",fcounter);
}
//somewhere hear here i get an error
if(fcounter >= 753629)
{
printf("%d\n",fcounter);
}
fcounter++;
}
//closing after reading
printf("try=%d buf= %.*s ibuf= %.*s \n",fcounter++,bcount,buffer,maxcount,ibuffer);
BFileClose(&file);
//freeig memory
free(buffer);
return 0;
}
here is binaryIO.h
/* Binary.h
Is used for working with files
(opening writing and error checking)
use FILE file;
to create buffer and
FileOpen(&file....
ti use it
*/
#include "BinaryIO.c"
//Opening file
void BFileOpen(FILE **file,char* path);
//Creating file
void BFileCreate(FILE **file,char* path);
//Closing file
void BFileClose(FILE **file);
//Writes line to the file
void BFileWLine(FILE **file,char *line);
//Reads Line from a file
char *BFileRLine(FILE **file);
//Reads a buffer of characters from file
char *BFileRbuf(FILE **file,int count);
Here is binaryIO.c
/* BinaryIO.C
- working with files */
#include <stdio.h>
#include <windows.h>
// FILEOPEN()
//opens file
void BFileOpen(FILE **file,char* path)
{
*file = fopen(path,"rb");
// open a text file for mode specified by user
if(file==NULL) {
// error here :( exeting
MessageBox(NULL,"file open","ERROR",0);
fclose(*file);
return;
}
}
//
//
//
//
//
//
//
//
// FILECREATE()
//creates new file
void BFileCreate(FILE **file,char* path)
{
*file = fopen(path,"wb");
// open a text file for mode specified by user
if(file==NULL) {
// error here :( exeting
MessageBox(NULL,"file create","ERROR",0);
fclose(*file);
return;
}
}
//
//
//
//
//
//
//
//
// FILECLOSE()
//closes file
void BFileClose(FILE **file)
{
fclose(*file);
}
//
//
//
//
//
//
//
//
// FILEWRITELINE()
//Writes line to the file
void BFileWLine(FILE **file,char *line)
{
int c;
while(c = *line++)
putc(c,*file);
// draws error if found
if (ferror(*file) != 0)
{
char temp[25];
sprintf(temp,"file write error = %d",ferror(*file));
puts(temp);
MessageBox(NULL,temp,"ERROR",0);
}
}
//
//
//
//
//
//
//
// FILEREADLINE()
//Writes line to the file
char *BFileRLine(FILE **file)
{
int i;
char *result;
char c;
i = 0;
//getting memory for strings
result = (char*)malloc(sizeof(char) * 1024);
while(TRUE)
{
c = getc(*file);
if((c == '\n')||(c == EOF))
break;
result[i++] = c;
}
//putting end to string
result = '\0';
return result;
}
//
//
//
//
//
//
//
// FILEREADBUFFER()
//Writes line to the file
char *BFileRBuf(FILE **file,int count)
{
int i;
char *result;
unsigned char c;
i = 0;
//returning nothing
if(count == 0)
return NULL;
//getting memory for strings
result = (char*)malloc(sizeof(char) * count);
while(i < count)
{
if((c = getc(*file)) == NULL)
break;
result[i++] = c;
}
//putting end to string
return result;
}