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

  • SEARCH
  • Home
  • 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 6118043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:23:25+00:00 2026-05-23T15:23:25+00:00

I am having some trouble with sprintf and fstream functions in order to create

  • 0

I am having some trouble with sprintf and fstream functions in order to create new text files for a POS program/check whether the file already exists. I don’t know if i am doing something wrong because the same set of functions works fine in other places in my code…

This particular section of code is taking input from the user to create a details file, the name is made up of the first and last name details that were entered into the system. For some reason the new file is not being created. When I step through the program I can see that the custDetC variable is being filled with the correct data. I have also included the file existence check as it may or may not have something to do with the issue at hand…

Tony Mickel

    sprintf(custDetC,"%s%s.txt", firstName.c_str(), lastName.c_str());
    cout << custDetC << endl;

    FileEX = FileExists(custDetC);

    if (FileEX == true)
    {
        fopen_s(&custDetF,custDetC, "rt");

        fprintf(custDetF, "%s %s\n", firstName, lastName);
        fprintf(custDetF, "$d\n", phoneNo);
        fprintf(custDetF, "%s $s\n", unitHouseNum, street);
        fprintf(custDetF, "%s %s %d", suburb, state, postCode);

        fclose(custDetF);
    }
    else
    {
        char *buf = new char[100];
        GetCurrentPath(buf);
        cout << "file " << custDetC << " does not exist in " << buf << endl;
    }
}

bool FileExists(char* strFilename) 
{
    bool flag = false;
    std::fstream fin;
    // _MAX_PATH is the maximum length allowed for a path
    char CurrentPath[_MAX_PATH];
    // use the function to get the path
    GetCurrentPath(CurrentPath);
    fin.open(strFilename, ios::in);

    if( fin.is_open() )
    {
        //cout << "file exists in " << CurrentPath << endl;
        flag = true;
    }
    else
    {
        //cout << "file does not exist in " << CurrentPath << endl;
        flag = false;
    }
    fin.close();

    return flag;
}
  • 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-05-23T15:23:25+00:00Added an answer on May 23, 2026 at 3:23 pm

    You seem to be opening the file for reading, but you need to open it for writing.

    Instead of "rt" use "wt" in fopen_s()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey having some trouble trying to maintain transparency on a png when i create
Having some trouble with the CSS alignment of text generated with @font-face. For some
Having some trouble with these methods on C# 2010 express with a new winform
Having some trouble understanding variable scope within a class structure. I want to create
having some trouble with this can't understand why it won't work properly. http://host.philmadelphia2.com/~clare/order/ when
I having some trouble with an APCS assignment. The program is supposed to read
Im having some trouble reading xml files using jquery that containts swedish chars (åäö)
Having some trouble with what should be a very simple scenario. For example purposes,
Basically having some trouble with using Hover to hide or show an item. The
Been having some trouble implementing a single-column index in LaTeX. I've been employing the

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.