error fopen with existing file

This is a discussion on error fopen with existing file within the C Programming forums, part of the General Programming Boards category; Hello, I am having a problem when I try to open a file from within a calling function. I have ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    error fopen with existing file

    Hello,

    I am having a problem when I try to open a file from within a calling function.

    I have a main function that defines part of the string name of a file (the file already exists in the working directory), passes this name to the function openmyfile(), and openmyfile uses strcat to give the correct extension to the name. When though openmyfile calls fopen, the file cannot be read.

    I put a sample of what main.c does and how openmyfile works.

    It is the first time that I happen to not be able to open an existing file for reading.
    Thank you in advance

    main.c
    Code:
    main(int argc, char *argv[])
    {
       char *problem_name;
       problem_name = (char*) malloc(16 * sizeof(char *));
    
       //Input problem name
         strcpy(problem_name, argv[1]);
    
       //Call openmyfile:
         openmyfile(problem_name)
    
    free(problem_name);
    
    return;
    }
    openmyfile.c
    Code:
    int openmyfile(char *problem_name)
    {
        FILE *file_ID;
        char *complete_filename;
        complete_filename = (char*) malloc(32 * sizeof(char *));
        
        //Build the name of the existing file (e.g. my_file_problem.txt)
            strcat(complete_filename, problem_name);
    
       //TRY TO OPEN FILE NOW:
       if((file_ID = fopen(complete_filename, "r")) == NULL){
    		printf(" I cant open %s for reading\n\n", complete_filename);
    		exit(1);
       }
    
    return;
    }
    The problem must be in the way the string "complete_filename" is assigned because, if I put the name by hand in fopen
    Code:
    fopen ("my_file_problem.txt", "r"))
    the file is open correctly.
    Last edited by cfdprogrammer; 10-02-2009 at 10:53 AM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    You don't know for sure what's causing the error unless you print the error message fopen is kicking out. Use perror or print errno in some fashion:

    e.g.

    Code:
     if ( (fp_out = fopen("myfile.dat", "w+")) == NULL) {
        printf("Error opening file: %s.\n", strerror(errno));
        exit (-1);
     }
    Print out the contents of complete_filename and see what's in it.

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    994
    char *problem_name needs a semicolon, for starters.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Quote Originally Posted by samf View Post
    You don't know for sure what's causing the error unless you print the error message fopen is kicking out. Use perror or print errno in some fashion:

    e.g.

    Code:
     if ( (fp_out = fopen("myfile.dat", "w+")) == NULL) {
        printf("Error opening file: %s.\n", strerror(errno));
        exit (-1);
     }
    Print out the contents of complete_filename and see what's in it.
    Hi Sam,

    thanks fro helping. Yes, what I obtain is the error of inexistent file although the file is physically in the working directory.

    Code:
    Error opening file: No such file or directory.
    That is why I suppose the problem is hidden behind the definition of the file string name (my guess), but I can't figure out where it would be.

    Thank you again

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    #1
    Code:
    problem_name = (char*) malloc(16 * sizeof(char *));
    ...
    
    complete_filename = (char*) malloc(32 * sizeof(char *));
    First off, avoid casting the return value of malloc. Second, sizeof(char*) is likely 4, I believe you were trying to say sizeof(char) instead but even this would not be needed since it's guaranteed (I think) to be 1. I'm guessing those statements should simply be:
    Code:
    problem_name = malloc(16);
    ...
    complete_filename = malloc(32);


    #2
    Code:
    strcat(complete_filename, problem_name);
    strcat depends on the array complete_filename being either empty or containing a properly terminated string. In either case, you'd expect a null to be in that array someplace but since you've just malloc'd and not initialized it you can't be sure what's in it at all. A better choice here would likely be strcpy instead but it doesn't really seem to be needed at all (just use problem_name directly in the fopen call). Were you attempting to concatenate something else to the string passed into the function?

    #3 You're missing a call to free in the openmyfile function for complete_filename.
    "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

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If complete_filename is not used outside openmyfile(), you should just use a local stack variable and not malloc at all.
    Code:
    char complete_filename[1024];  /* big enough */
    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

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    To further what hk_mp5kpdw wrote about casting the return value of malloc, the cboard FAQ has a decent entry on that subject here.

    There is also some good reading about the matter in the comp.lang.c FAQ:

    7.6 -- Why am I getting ``warning: assignment of pointer from integer lacks a cast'' for calls to malloc?
    7.7 -- Why does some code carefully cast the values returned by malloc to the pointer type being allocated?
    7.7b -- What's wrong with casting malloc's return value?

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi again,

    [QUOTE=hk_mp5kpdw;898314]#1
    Code:
    problem_name = (char*) malloc(16 * sizeof(char *));
    ...
    
    complete_filename = (char*) malloc(32 * sizeof(char *));
    First off, avoid casting the return value of malloc. Second, sizeof(char*) is likely 4, I believe you were trying to say sizeof(char) instead but even this would not be needed since it's guaranteed (I think) to be 1. I'm guessing those statements should simply be:
    Code:
    problem_name = malloc(16);
    ...
    complete_filename = malloc(32);

    #2
    Code:
    strcat(complete_filename, problem_name);
    strcat depends on the array complete_filename being either empty or containing a properly terminated string. In either case, you'd expect a null to be in that array someplace but since you've just malloc'd and not initialized it you can't be sure what's in it at all. A better choice here would likely be strcpy instead but it doesn't really seem to be needed at all (just use problem_name directly in the fopen call). Were you attempting to concatenate something else to the string passed into the function?
    I actually need to concatenate because the string name at hand depends on something that is done beforehand inside this function. About that I cant really do otherwise.

    #3 You're missing a call to free in the openmyfile function for complete_filename.
    Sorry about the lack in this forum; in the original complete code I am actually freeing correctly.
    Thanks for the suggestion though.

    I tried to print to screen what the variable "complete_filename" is assigned before passing it to "fopen", and I obtain that the name Io exopect to receive is correct, although I am afraid there must be some EOF or end of string somewhere in it.

    I report here the output of my printing the variable string, and the error of fopen:

    Code:
    NODE_FILENAME: agnesi_nodes.txt
    
     Error opening file agnesi_nodes.txt: No such file or directory.
    thank you again

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cfdprogrammer View Post
    I tried to print to screen what the variable "complete_filename" is assigned before passing it to "fopen", and I obtain that the name Io exopect to receive is correct, although I am afraid there must be some EOF or end of string somewhere in it.

    I report here the output of my printing the variable string, and the error of fopen:

    Code:
    NODE_FILENAME: agnesi_nodes.txt
    
     Error opening file agnesi_nodes.txt: No such file or directory.
    thank you again
    Can you see agnesi_nodes.txt? If so, in what directory is it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21