What problem have I created using a char array in fopen?

This is a discussion on What problem have I created using a char array in fopen? within the C Programming forums, part of the General Programming Boards category; I'm not sure how to interpret function syntax. I am using Borland C++ IDE version 4.52 to create C code. ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    21

    Question What problem have I created using a char array in fopen?

    I'm not sure how to interpret function syntax. I am using Borland
    C++ IDE version 4.52 to create C code.

    The Syntax for fopen is:

    FILE *fopen(const char *filename, const char *mode);

    But I am using a char array for my file name. It isn't a constant but is created form other char arrays. The code seems to work fine but what problem have I created using a char array in fopen instead of a constant? Excerpts from my code are:

    Code:
    void DownloadAFile(char *strDirectory,char *strFileName,char *strRxBlock, int intOffset)
    {
         char strFullFileName[121];
    
         Other code
    
         //Create FullFileName
         strFullFileName[0] = '\0';
         strcat(strFullFileName, strDirectory);
         strcat(strFullFileName, strFileName);
    
         Other code
    
         TxtDLFilePtr = fopen(strFullFileName, "rt");
    
         Other code
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    This is fine. In this context, the array of char is converted to a pointer to its first element. Passing a pointer to char as an argument for a parameter that is pointer to const char is okay because it just means that the function promises not to modify the object/array through the pointer, but the array could be modified to begin with, so there is no problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > FILE *fopen(const char *filename, const char *mode);
    The const in this context doesn't mean you have to provide a pointer to a const string.
    It means that fopen itself is promising NOT to modify the parameter passed in.

    So given
    TxtDLFilePtr = fopen(strFullFileName, "rt");
    you can be sure that strFullFileName still contains what you initially set it to contain.

    Say for example, later on doing something like
    fprintf(stderr, "Syntax error in file %s\n", strFullFileName );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    21
    Thank you both. This clears things up for me a bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array created in another method..
    By rodrigorules in forum C++ Programming
    Replies: 7
    Last Post: 04-03-2010, 10:38 PM
  2. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  3. using fopen with const char array?
    By traxy in forum C Programming
    Replies: 9
    Last Post: 09-16-2008, 02:53 PM
  4. passing an array created with malloc as a param
    By mariano_donati in forum C Programming
    Replies: 12
    Last Post: 02-20-2008, 11:26 AM
  5. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM

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