Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:11:36+00:00 2026-06-12T12:11:36+00:00

I have a simple line in my C++ code to create a new file:

  • 0

I have a simple line in my C++ code to create a new file:

string fileName = "test";

// Create a file named "test"
rc = pf->CreateFile(fileName.c_str());

Inside CreateFile function (which takes const char *fileName as an argument, I have following snippet of code;

// Create the file in current working directory
char *path = NULL;
path = getcwd(path, 0);
path = strcat(path, "/");
path = strcat(path, fileName);
FILE *fHandle = fopen(path, "wb");

The string path contains full absolute path of the file to be created. The file name is test. However when I run the code, the file is indeed created, however its name contains unprintable characters (code was run between following two commands):

enter image description here

Please suggest what could be wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:11:37+00:00Added an answer on June 12, 2026 at 12:11 pm

    From man getcwd:

    As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc(3) if buf is NULL. In this case, the allocated buffer has the length size unless size is zero, when buf is allocated as big as necessary. The caller should free(3) the returned buffer.

    This means there is no extra space left in path to be appending to and results in overwriting the bounds of the array that path points to, causing undefined behaviour and is the probable cause of the unprintable characters.

    To construct a buffer capable of holding the path you need to determine the full size, malloc() and build it:

    char *path;
    path = getcwd(path, 0);
    if (path)
    {
        /* The '+2' is for null terminator and the '/'. */
        const size_t size = strlen(path) + strlen(fileName) + 2;
        char* fullPath = malloc(size);
        if (fullPath)
        {
            sprintf(fullPath, "%s/%s", path, fileName);
            /* fopen() ... */
            free(fullPath);
        }
        free(path);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple code that looks up a text file, reads the line
I have a fairly simple code, a button click event, with the first line
I have a very simple line graph I'm trying to create in gRaphael. My
I have a simple JEditorPane inside a JScrollPane that displays line numbers on the
I have a simple JSF line chart that uses PrimeFaces (via jqPlot) library: <p:lineChart
I have made a simple Line Chart: Which is showing 4 grid-lines at points
I have a simple in-line edit in my grid, and I want to commit
Say you have a simple loop while read line do printf ${line#*//}\n done <
I have a simple application which basically consists of a line of buttons and
I have some pyunit unit tests for a simple command line programme I'm writing.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.