Hi all,
My program seems to be giving me an error in the form of a segmentation fault...
Please see my code below with a comment show the point where program execution fails.
Please advise!
Code:
//////////////////////////////////////////////////////////////////////////
// Pre-processor Directives
//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NBARGS 2
#define FIFO_FILE "MYFIFO"
#define MESSAGESIZE 80
//////////////////////////////////////////////////////////////////////////
// Sub Function Prototypes
//////////////////////////////////////////////////////////////////////////
void argumentValidater( int );
char * concatenator( int, char** );
//////////////////////////////////////////////////////////////////////////
// Main Function Implementation
//////////////////////////////////////////////////////////////////////////
int main( int argc, char *argv[] )
{
argumentValidater( argc );
char *userString;
FILE *fp;
if( ( fp = fopen( FIFO_FILE, "a" ) ) == NULL )
{
perror( "fopen" );
exit( EXIT_FAILURE );
}
userString = concatenator( argc, argv );
printf( "\nUSERSTRING: %s", userString );fflush( stdout );
fputs( userString, fp );
fclose( fp );
}
//////////////////////////////////////////////////////////////////////////
// Sub Function Implementation
//////////////////////////////////////////////////////////////////////////
void argumentValidater( int argCount )
{
if( argCount < NBARGS )
printf( "\nYou must enter a number followed by your string of text! EXITING...\n" );fflush( stdout );
}
//////////////////////////////////////////////////////////////////////////
char * concatenator( int argCount, char *argVector[] )
{
int i = 1;
char *str;
while( i < ( argCount + 1 ) )
{
char *tmpStr;
tmpStr = argVector[i];
strcat( str, tmpStr ); /* Problem is here... */
strcat( str, " " );
i++;
}
strcat( str, "\n" );
return str;
}
//////////////////////////////////////////////////////////////////////////