Make a directory and put your source code in it.
Make a directory called foo in your source directory.
Make a file in foo called bar containing some text or whatever.
Put this in your source directory, compile it and run it:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fun1( void )
{
FILE *fp = fopen( "./foo/bar", "r" );
if( fp == NULL )
{
printf("fopen failed on \'./foo/bar\'\n");
}
else
fclose( fp );
}
void fun2( void )
{
char buf[BUFSIZ] = {0};
FILE *fp;
strcat( buf, "./foo" );
strcat( buf, "/" );
strcat( buf, "bar" );
fp = fopen( buf, "r" );
if( fp == NULL )
{
printf("fopen failed on \'%s\'\n", buf );
}
else
fclose( fp );
}
void fun3( void )
{
char buf[BUFSIZ] = {0};
FILE *fp;
sprintf( buf, "./%s/%s", "foo", "bar" );
fp = fopen( buf, "r" );
if( fp == NULL )
{
printf("fopen failed on \'%s\'\n", buf );
}
else
fclose( fp );
}
int main( void )
{
fun1( );
fun2( );
fun3( );
return 0;
}
*glances at code* Yeah that looks about right. I'm not at my compiler. Try that. If any of those fail, they all will, and it'll be because you didn't create the file and/or directory correctly.
Quzah.