Thread: Unable to open file using fopen()

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    Unable to open file using fopen()

    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

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You should post the simplest program that demonstrates the problem. Also be sure it is able to compile, including all necessary headers.

    Here's a simplification of your code. Does it work for you? If not, then either the file doesn't exist or you're accessing the wrong directory.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char filename[40];
    	FILE *pFile;
    
    	strcpy(filename,"noop.txt");
    
    	pFile = fopen(filename,"r+");
    	if(pFile == NULL)
    	{
    		printf("ERROR : File cannot be opened\n");
    		return 1;
    	}
    
    	fclose(pFile);
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    I agree your suggestion....... was in hurry......

    I am unable to open the file, But it works fine if i pass the file name directly to the function.

    what may be wrong in the higlighted code?

    Thanks in advance

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Nothing seems to be wrong with the highlighted code.
    You didn't answer my question.
    Does the simplified program that I posted work for you?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sanddune008 View Post
    1. Why can't open File ,[B]when I pass a buffer containing the filename to fopen()
    Ask the system itself:

    Code:
    	pFile = fopen(filename,"r+"); //Does open file with this
    	if(pFile == NULL)
    	{
    		perror(filename);
    		return 1;
    	}
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    @oogabooga.......Simplified ex worked!!!....

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    How are you running the code? Within an IDE or from the command-line?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by sanddune008 View Post
    @oogabooga.......Simplified ex worked!!!....
    then probably something you are doing between the strcpy and fopen is corrupting filename. printf the filename after your mallocs and right before the fopen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to open included file
    By jaisha in forum C Programming
    Replies: 12
    Last Post: 04-22-2010, 12:23 AM
  2. Unable to open include file SDL_audio.h
    By rraj.be in forum C Programming
    Replies: 2
    Last Post: 06-28-2008, 08:04 PM
  3. Unable to open a file for reading
    By raghuveerd in forum C Programming
    Replies: 16
    Last Post: 06-18-2007, 11:47 PM
  4. (Error message) Fatal: Unable to open file 'C.OBJ'
    By drojen in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2006, 12:05 AM
  5. Cannot Open FILE-using fopen()
    By anwar_pat in forum C Programming
    Replies: 10
    Last Post: 02-08-2006, 09:28 PM