I have following piece of code:
Code:
#define MAX_BYTES_LINE 100
// Main Program starts here
//int main(int argc,char *argv[])
int main()
{
char **ptdata,filename[40],count[10];
int cnt=2,i;
FILE *pFile;
/*if((argv[1] == NULL)&&(argv[2] == NULL))
{
printf("ERROR: Insufficient info \n");
return 1;
}
//Copy command line strings
strcpy(filename,argv[1]);
strcpy(count,argv[2]);
cnt = atoi(count);
printf("Filename : %s count :%s , count :%d",filename,count,cnt);*/
strcpy(filename,"noop.txt"); //Assigning the filename
//Allocate memory
ptdata = malloc(cnt*MAX_BYTES_LINE*sizeof(char *));
if(ptdata == NULL)
{
printf("ERROR:Insufficient Memory\n");
return 1;
}
for(i=0;i<(cnt*MAX_BYTES_LINE);i++)
{
ptdata[i] = malloc(20*sizeof(char));
if(ptdata[i] == NULL)
{
printf("ERROR:Insufficient Memory\n");
return 1;
}
}
//File open
pFile = fopen(filename,"r+"); //Does open file with this
//pFile = fopen("noop.txt","r+"); //Works fine here
if(pFile == NULL)
{
printf("ERROR : File cannot be opened\n");
return 1;
}
//Read file content
fread (ptdata[1],1,20,pFile);
printf("File content:%s",ptdata[1]);
//Destructor
fclose(pFile);
for(i=0;i<(cnt*MAX_BYTES_LINE);i++)
free(ptdata[i]);
free(ptdata);
return 0;
}
1. Why can't open File ,when I pass a buffer containing the filename to fopen()
Thanks