Thread: Using f open with a char variable.

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    6

    Using f open with a char variable.

    Hi.

    I'm very new to the C language, but I have written programs in perl and shell programming. I want to learn C so I started rewriting my shell program in it. I'm using the same structure as my shell program and up to this point I've gotten it to compile and run correctly, Ending up with the same result as it's shell equivalent.

    What I bumped my head on is this

    //interface variables
    char lanint[7] = "";
    char wanint[7] = "";

    //show user systems interfaces
    system("ifconfig -a");

    //ask user for LAN interface
    printf("What interface do you want as your LAN?\n");
    scanf("%s", &lanint);

    //ask user for WAN interface
    printf("What interface do you want as your WAN?\n");
    scanf("%s", &wanint);


    printf("Writing interface configuration\n");
    //open hostname.if files
    lanfi = fopen("/etc/hostname.lanint" "w+");
    fclose(lanfi);

    wanfi = fopen("/etc/hostname.wanint", "w+");
    fclose(wanfi);


    I need the variables lanint and wanint to be used as a suffix to the fopen filename.

    eg fopen("/etc/hostname.em0", "w+");

    but the suffix is just whatever the user types instead of hardcoding it. I'll end up putting valid configuration in those files after I get the name correct.

    If this is completely wrong I totally understand. I'm not the best programmer nor in the profession of such. But regardless I enjoy programming very much.
    Last edited by tman904; 03-10-2019 at 04:37 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can use strcpy / strcat / sprintf to build strings out of other strings.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    6
    Are those functions in stdio.h? I was hoping to keep this as cross platform as possible.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    sprintf (or better yet, snprintf) would be from <stdio.h>, but strcpy and strcat would be from <string.h>
    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

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    6
    Ok Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-28-2014, 05:06 PM
  2. Assign constant char to another char variable
    By effup in forum C Programming
    Replies: 4
    Last Post: 10-04-2012, 07:43 PM
  3. Replies: 7
    Last Post: 09-19-2011, 01:37 PM
  4. Open a file, read it, display it and process char.
    By Devenc in forum C Programming
    Replies: 6
    Last Post: 04-19-2010, 11:29 PM
  5. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM

Tags for this Thread