writing files to a specified directory

Posted on 2004-10-31
Last Modified: 2010-04-15
I want to write files to a specified subdirectory i.e files/ or something like that?
Normally they would be in the same directory as the program
Question by:kenbond
    LVL 45

    Expert Comment

    Hi kenbond,

    If files are in the same as cwd(current working directory), you can simply use filename to create files and they will be created in cwd, else use complete pathname to create a file ... e.g.

    open ("/home/kenbond/programs/test1.txt", O_CREAT|O_EXCL);


    Author Comment

    well im using sprintf to determine my filename

    so could I use strncat where string1="files/" and string2=filename?
    LVL 45

    Expert Comment

    yes you can use strncat

    Author Comment



    string1 = "bankPro/"
    string2 = name

    strncat(string1,string2,strlen(string2)); is givin me a seg fault
    LVL 45

    Accepted Solution

    string1 is a constant memory location ... you cannot append to a constant ... Also, when you use strncat, it is your responsibility to make sure that enough memory is available

    char string1[100] = "bankPro/";
    string2 = name;


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now