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 538209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:00:27+00:00 2026-05-13T10:00:27+00:00

I am writing a program in c++ that accepts a filename as an argument

  • 0

I am writing a program in c++ that accepts a filename as an argument on the command line:

>> ./myprogram ../path/to/file.txt

I know I can simply open an fstream using argv[1], but the program needs more information about the exact location (ie. full pathname) of the file.

I thought about appending argv[1] to getcwd(), however obviously in the example above you’d end up with /path/../path/to/file.txt. Not sure whether fstream would resolve that path automatically, but even if it did, I still don’t have the full path without a lot of string processing.

Of course, that method wouldn’t work at all if the path provided was already absolute. And since this program may be run on Linux/Windows/etc, simply detecting a starting ‘/’ character won’t work to determine whether the argument was a full path or not.

I would think this is a fairly common issue to deal with path names across multiple OSs. So how does one retreive the full path name of a command line argument and how is this handled between operating systems?

  • 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-13T10:00:27+00:00Added an answer on May 13, 2026 at 10:00 am

    Pathname handling is highly OS-specific: some OS have a hierarchy with just one root (e.g. / on Unix ), some have several roots a la MS-DOS’ drive letters; some may have symbolic links, hard links or other kinds of links, which can make traversal tricky. Some may not even have the concept of a “canonical” path to a file (e.g. if a file has hard links, it has multiple names, none of which is more “canonical”).

    If you’ve ever tried to do path-name manipulation across multiple OS in Java, you know what I mean :-).

    In short, pathname handling is system-specific, so you’ll have to do it separately for each OS (family), or use a suitable library.

    Edit:

    You could look at Apache Portable Runtime, or at Boost (C++ though), both have pathname handling functions.

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

Sidebar

Related Questions

I'm writing a program in Python that accepts command line arguments. I am parsing
I am writing a program that sets up a file path that I use
I'm writing a C program that accepts input one line at a time from
I am writing a program that accepts a file with database entries in it.
I'm writing a program that's parsing an XML file to java objects using smooks.
I'm writing a program that reads huge file (3x280 GB) and does a fitting
I'm writing an autograder web application that accepts a program from the user as
I am writing a small java program that can measure the speed of my
I'm beginner to c++ and writing a program that accepts user choices and acts
I'm just learning COBOL; I'm writing a program that simply echos back user input.

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.