the following proposed changes to the OPs code:
- cleanly compiles
- performs the desired functionality
- properly checks for errors
- properly free's the allocated memory when done with it
Note: function: 'perror()' passes the error message and the text reason the system thinks the last failure occurred to 'stderr'
And now, the proposed changes, with comments
#include <stdio.h> //<-- missing
#include <stdlib.h> //<-- missing
//void batchMode(char **c) { //<-- needs pointer to a string, not a single character
//<-- suggest using meaningful name, like: 'filename'
void batchMode( char *fileName ) {
//char *batchBuffer = NULL; //<-- limit scope and keep with 'setter'
//size_t batchSize = 0; //<-- ftell() returns a 'long', not a unsigned long
//long batchSize = 0; //<-- limit scope and keep with 'setter'
FILE *fp = fopen( fileName, "r"); //<-- check returned value for success (!=NULL)
if( ! fp )
{
perror( "fopen failed" );
exit( EXIT_FAILURE );
}
//fseek(fp, 0, SEEK_END); //<-- returns an 'int', check for success (!=-1)
long batchSize;
if( (batchSize = fseek( fp, 0, SEEK_END )) == -1 )
{
perror( "fseek for end of file failed" );
exit( EXIT_FAILURE );
}
batchSize = ftell(fp); //<-- check the returned value for an error indication (-1)
//<-- returns a 'long', not a 'unsigned long'
if( batchSize == -1 )
{
perror( "ftell failed" );
exit( EXIT_FAILURE );
}
rewind(fp); //<-- does not have error checking, suggest: using 'fseek(fp, 0, SEEK_SET )'
// batchBuffer = malloc((batchSize + 1) * sizeof(*batchBuffer)); //<-- 'sizeof(*batchBuffer)' is the size of a char pointer 4 or 8 depending on the underlying hardware architecture and certain compile options
//<-- check for success, returned value (!=NULL)
char * batchBuffer = malloc( (size_t)batchSize+1);
if( ! batchBuffer )
{
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
//fread(batchBuffer, batchsize, 1, fp); // incorrect capitalization of batchSize
if( fread(batchBuffer, (size_t)batchSize, 1, fp) != 1 ) //<-- if returned value != third parameter, then error occurred
{
perror( "fread failed" );
exit( EXIT_FAILURE );
}
batchBuffer[batchSize] = 0;
printf("%s\n", batchBuffer);
free( batchBuffer ); //<-- to avoid memory leak
}
int main(int argc, char **argv){
//if (argc == 2) //<-- handle error first
if( argc != 2 )
{
fprintf( stderr, "USAGE: %s <fileName>\n", argv[0] );
exit( EXIT_FAILURE );
}
//batchMode(&argv[1][0]); //<-- send pointer to first command line parameter, not a single character
batchMode( argv[1] );
return 0;
}
fopen()spec and make sure it gets the type it wants. That should get you started. en.cppreference.com/w/c/io/fopenbatchModewants to be a character pointer (one star), not a pointer to a pointer to a character (two stars):void batchMode(char *c).#includestatements for the needed header files.