Luigi Auriemma

aluigi.org (ARCHIVE-ONLY FORUM!)
It is currently 19 Jul 2012 14:42

All times are UTC [ DST ]





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: [C]Making very little sense
PostPosted: 22 Jun 2009 22:13 

Joined: 17 Mar 2009 21:04
Posts: 7
This is driving me insane, everything on the program works until it reaches the readbin(); function, then it will either say it cannot open the file or just crash. I have no idea what I could be doing wrong, and was really hoping I could get some help:

Code:
/********************Header Files*********************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/*****************************************************/

/**************************Windows Only Remove Headerfiles If Compiling In Linux****************************/
#include <windows.h>
#include <conio.h>
/*Also if compiling in linux replace all getche(); functions within the program and replace with getchar();*/


/*******************Define Compile Settings*************************/
#ifdef WIN32
#define WINDOWS 1
#else
#define WINDOWS 0
#endif
#define MAJVER  0
#define MINVER  0
#define BUILD   1
#define DEBUG   1
/*******************************************************************/


/*******************************Program Functions***********************************************/
int start(void);                      /*Menu*/
int inputhex(void);                   /*Input standard ASCII into the console and convert it*/
int readbin(void);                    /*Open and read a binary file to covnert*/
int myerror(void);                    /*Error handler*/
int debugpause(void);                 /*If Debug is set to 1*/
void shell(char *cmd);                /*To clear the screen*/
/***********************************************************************************************/

/********** Global Variables**********/
char *ap;
FILE *in, *out;
/*************************************/

int main(int argc, char *argv[])
{
ap=argv[0];
if(DEBUG) printf("-__{Debug Mode}__-\n");
printf("Purple-hex'd Two Way Binary Converter %d.%d.%d\n", MAJVER, MINVER, BUILD);
printf("by Sean Killian and Keith Miller\n\n\n\n");
start();
return 0;
}

/*Function Prototypes*/
int start(void)
{
    char ch;
    printf("Press 1 to input text to covnert.\nPress 2 to open a binary file to convert to a text file.\n>");
    ch=getche();
    if(ch=='1') inputhex();
    else if(ch=='2') readbin();
    if(DEBUG) debugpause();
               
    else {
         printf("\aError! Could not proccess input! ");
         myerror();
         }
}

int inputhex(void)
{
    char ch, string[9000];
    int i=0;
   
    shell("clearscreen");
    if(DEBUG) printf("-__{Debug Mode}__-\n\n\n");
    printf("\nEnter string to convert\n>");
    gets(string);
    if(DEBUG) {
              printf("String is: %s\n", string);
              printf("Length of string is: %d\n", strlen(string));
              debugpause();
              }
    for(i=0; i<=strlen(string); i++) printf("%x", string[i]);
    printf("\nDone! Press 1 to return to menu or 2 to exit.\n>");
    ch=getche();
    if(DEBUG) debugpause();
    if(ch=='1') {
                shell("clearscreen");
                start();
                }
    else if(ch=='2') exit(0);
    else myerror();
}

int readbin(void)
{
    size_t i;
    char inputf[2000], input[2002], output[2000], outputf[2002], tmp;
    void *buffer;
    struct stat st;
   
   
    shell("clearscreen");
    if(DEBUG)printf("-__{Debug Mode}__-\n\n\n");
    printf("\nEnter location of file to open\n>");
    gets(input);
    printf("\nEnter location of file to output results\n>");
    gets(output);
    sprintf(inputf, "\"%s\"", input);
    sprintf(outputf, "\"%s\"", output);
    if(DEBUG) {
              printf("input file: %s\n", input);
              printf("input file length: %d\n", strlen(input));
              printf("input file quotes added: %s\n", inputf);
              printf("input file quotes added length: %d\n", strlen(inputf));
              printf("output file: %s\n", output);
              printf("output file length: %d\n", strlen(output));
              printf("output file quotes added: %s\n", outputf);
              printf("output file quotes added length: %d\n", strlen(outputf));
              debugpause();
              }
    if((in = fopen(inputf, "rb")==NULL)) {
                                         printf("\nCannot open file!\n");
                                         myerror();
                                         }
    if((out = fopen(outputf, "wb+")==NULL)) {
                                                            printf("\nCannot open file!\n");
                                                            myerror();
                                            }
    stat(inputf, &st);
    fread(&buffer, 1, st.st_size, in);
    for(i=0; i<st.st_size; i++) {
                                fscanf(buffer, "%c", &tmp);
                                fprintf(out, "%x", tmp);
                                }
    fclose(in);
    fclose(out);
}

   
int myerror(void)
{
    char ch;
    char executable[4096];
   
    shell("clearscreen");
    if(DEBUG) printf("-__{Debug Mode}__-\n\n\n");
    printf("\aAn Error Occured! Please wait....\n");
    sleep(3000);
    printf("Press 1 to restart the program or 2 to exit\n>");
    ch=getche();
    if(ch=='1') {
                sprintf(executable, "\"%s\"", ap);
                if (DEBUG) {
                           printf("Executable path: %s\n", executable);
                           debugpause();
                           }
                shell("clearscreen");
                printf("\n");
                system(executable);
                }
    else system("pause");
}

void shell(char *cmd) {
     if(cmd=="clearscreen") {
                            if(WINDOWS) system("cls");
                            if(!WINDOWS) system("clear");
                            }
}

int debugpause(void)
{
    char ch;
    ch=getchar();
    printf("\nPaused For Debugging Purposes\nPress Enter To Continue....\n\n");
    return 0;
}


I am compiling with Dev-C++ and running in Windows XP home Service Pack 3


Top
 Profile  
 
 
 Post subject: Re: [C]Making very little sense
PostPosted: 23 Jun 2009 17:33 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
ehmm but why that "void *buffer;" is not allocated? :)


Top
 Profile  
 
 Post subject: Re: [C]Making very little sense
PostPosted: 24 Jun 2009 02:06 

Joined: 17 Mar 2009 21:04
Posts: 7
aluigi wrote:
ehmm but why that "void *buffer;" is not allocated? :)


Not quite sure to tell you the truth never used fread or made a buffer


Top
 Profile  
 
 Post subject: Re: [C]Making very little sense
PostPosted: 24 Jun 2009 09:09 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
ok.
practically fread does a simple job, reads a certain amount of bytes from a file (argument 2 * argument 3) and stores them in the specified buffer... something like a memcpy but with the file as source.
so in your case you have buffer which is simply a pointer to nothing (uninitialized) and so when you call fread it put these bytes to a random location (if you used void *buffer = NULL; it tried to put the bytes into 0x00000000).

so you must first allocat the buffer giving it enough space for containing all the bytes so:
unsigned char *buffer;
buffer = malloc(st.st_size);
fread(&buffer, 1, st.st_size, in);
...
free(buffer);

(as you can see I have specified buffer as "unsigned char" and not as "void" because void should be used only for functions and functions arguments).
you could use also alloca() if you want which practically allocates the memory on the stack but obviously it's limited by the stack size so is generally not suggested.

anyway exist a lot of things that you can optimize or just remove in that code.
one of the first rules is NEVER using system(), never never never.
then when you have a #define set it's better to use:
Code:
#ifdef WIN32
   dothis,dothat
#endif
instead of if(WIN32) dothis,dothat
and then the:
Code:
    for(i=0; i<st.st_size; i++) {
                                fscanf(buffer, "%c", &tmp);
                                fprintf(out, "%x", tmp);
                                }
which is bad 2 times, first because you don't need to use fscanf and then because you print the chars as concatenated %x and so they don't have a fixed size (byte 0x01 occupies 1 char while 0x10 occupies 2) so it's better to use:
Code:
    for(i=0; i<st.st_size; i++) {
                                fprintf(out, "%02x", buffer[i]);
                                }
there are also other things but you will have fun to find and resolve them :)

oh remember EVER to improve the verbosity of your compiler (more warnings) and being sure that it compiles without warnings


Top
 Profile  
 
 Post subject: Re: [C]Making very little sense
PostPosted: 24 Jun 2009 15:50 

Joined: 17 Mar 2009 21:04
Posts: 7
Luigi, you are truly the nicest person I have ever spoken to, If I ask anyone else I get called idiot or something, and you just explain it to me and help me thank you so much :)


Top
 Profile  
 
 Post subject: Re: [C]Making very little sense
PostPosted: 24 Jun 2009 16:39 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
be happy, usually those people who say "idiots" to other users who simply ask for an help (although it's a basic question or a basic argument) are:

- lazy and so they want an excuse to avoid to reply, lazyness is normal and affect anyone so I don't blame them for not helping another person but I blame them for making an useless reply (moreover if it can be offensive)

- ignorant, so people who don't know the answer but they want to give their opinion at any cost being 100% sure of what they say and which (for some misterious reasons) is ever wrong, this is enough common in the computer field and does a lot of direct and indirect damages

- just idiots or frustrated people, so anything they will say and do in their useless lives (which is not life at all) is nothing and must be ignored

well, 3 reasons to be happy :)
and I'm happy that my suggestions have been useful, I like to give tips in C programming (like never using gets, sleep which differs between windows and the other platforms, various optimizations and a lot more)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: