Join 2 strings inside 'fopen'?

This is a discussion on Join 2 strings inside 'fopen'? within the C Programming forums, part of the General Programming Boards category; I cannot find if it is possible to join 2 strings for to use inside in function ' fopen '. ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    Europe Cz Prague
    Posts
    14

    Join 2 strings inside 'fopen'?

    I cannot find if it is possible to join 2 strings for to use inside in function 'fopen'.
    I have one string variable eg. 'char name[9] = filename' with name of file without extension.
    I need add extension eg. 'txt' and open 'filename.txt' by function 'fopen'. It is possible this way:
    strcat(name, "txt");
    fopen(name, "f");

    - but it change original variable value from 'filename' to value 'filename.txt'.
    But I need to use original variable 'name' with value 'filename' (without extension '.txt') for next functions.

    Could you know please, how it is possible to simply join 2 strings for to use them together inside some function?

    I tried fopen((name".txt"), "f"); without success.
    Thank you for your help.
    Last edited by tomas.ra; 09-24-2005 at 04:41 AM.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    cant you just use another variable.........

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    No you can't do that.
    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
    Jun 2005
    Posts
    6,433
    strcat() returns it's first argument. Make sure the buffer is long enough, and ....

    Code:
    char name[13] = "filename";
    if (fopen(strcat(name, ".txt"), "w+") != NULL) ....
    However, using a shorter buffer will yield undefined behaviour.
    Code:
    char name[9] = "filename";
    if (fopen(strcat(name, ".txt"), "w+") != NULL) ....
    This works, but I fail to see why this is a specific requirement. The code is a bit less cryptic if you do this...
    Code:
    char name[13] = "filename";
    strcat(name, ".txt");
    if (fopen(name, "w+") != NULL) ....
    or, if you know the strings at compile time you can merge string literals.
    Code:
    #define EXTENSION ".txt"
    char name[13] = "filename" EXTENSION;
    if (fopen(name, "w+") != NULL) ....
    Incidentally, passing "f" as second argument to fopen is invalid.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Location
    Europe Cz Prague
    Posts
    14
    Thank you for your time and your help.
    It must be probably solved with 2 char variables.
    I used:
    scanf("%6s", str10); /*put string to variable str10*/
    strcpy(str6, str10); /*copy original string to variable str6*/
    strcat(str10, ".txt"); /*add to string in str10 -> 'str10.txt'*/

    now I have 2 variables:
    str6 with original string
    and
    str10 with original string joined with '.txt'

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    You only need the two variables if you insist on keeping a copy of the original string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings in C++
    By elad in forum C++ Programming
    Replies: 11
    Last Post: 05-20-2006, 02:27 AM
  2. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:34 AM
  3. help with fopen() and strings. i'm a noob.
    By xhoangx in forum C Programming
    Replies: 3
    Last Post: 12-04-2005, 07:40 PM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM

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