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

  1. #1
    Registered User
    Join Date
    Aug 2012

    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:

    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
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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.

  4. #4
    Registered User
    Join Date
    Aug 2012
    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
Website Security Test