Why can''t one open a file in a different directory , C/C++ Programming

Why can't one open a file in a different directory like "..\test.dat"?

A: Since " " is a tab character.

You must employ forward slashes in your filenames, even on operating systems which use backslashes (Windows, DOS, OS/2, etc.). For instance:



int main()


#if 1

std::ifstream file("../test.dat"); // RIGHT!


std::ifstream file(".. est.dat"); // WRONG!




Keep in mind, the backslash ("\") is utilized in string literals to generate special characters: "\b" is a backspace, "\n" is a newline, and "\a" is an "alert", " " is a tab, "\v" is a vertical-tab, etc. Thus the file name "\version\next\alpha\beta est.dat" is interpreted such as a bunch of extremely funny characters. To be secure, employ "/version/next/alpha/beta/test.dat" in spite of, even on systems which use a "\" as the directory separator. It is because the library routines on these operating systems handle "\" and"/" interchangeably.


Certainly you could employ "\\version\\next\\alpha\\beta\ est.dat", however that might hurt you (there's a non-zero chance you'll forget one of the "\"s, a rather subtle bug since most people don't notice it) & it can't help you (there's no advantage for using "\\" over "/"). In addition "/" is more portable since it works on all flavors of Unix, Inferno, Plan 9, all Windows, OS/2, etc., however "\\" works only on subset of that list. Thus "\\" costs you something and gains you nothing: use "/" instead.


Posted Date: 3/21/2013 7:43:47 AM | Location : United States

Related Discussions:- Why can''t one open a file in a different directory , Assignment Help, Ask Question on Why can''t one open a file in a different directory , Get Answer, Expert's Help, Why can''t one open a file in a different directory Discussions

Write discussion on Why can''t one open a file in a different directory
Your posts are moderated
Related Questions
I am working on a program that allows a player to play a game against the computer. In this game the player can only chose to draw 1, 2, or 3 toothpicks. How do I make restrictions

program that counts the number of occurrences of the string in the n-th padovan string p(n)

You may work in pairs for this assignment. Submit only one project per team; both partners will receive the same grade. Also, you can earn up to 40 points (out of 25) on this assig

Set of Integers) Create class IntegerSet. Each IntegerSet object can hold integers in the range 0-100. The set is represented by an array of bools. Array element a[i] is true if in

Write a program called OddEven that will prompt the user for an integer and print/display a message indicating whether it is even or odd. Continue prompting for numbers from the us

redirection in c++

Implement, in rPeANUt, the a Fibonacci function by using the simple recursive approach. The Fibonacci function can be implemented in c as follow: int fib(int x) {   if (x

The for Loop For loop is the controlled form of loop. The general format of this : for( initialize ; test ; update)                  {                     statements;

#include #include #include #include #include class Employee { private: char *Name; //Set them as pointers... int IdNumber; char *Department; char *Position; public: voi