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:

#include

#include

int main()

{

#if 1

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

#else

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

#endif

...

}

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
Respected Sir, I want the above written text in a C program

Create a user-defined data structure (struct) called Node that represents a node within a linked list where the "data" stored in each node is a pointer to a Car object.    a) Wr

Pawnbroker software to be written in VB Project Description: I want software written for my pawnshop. I am currently using a program called pawnboss but would like to have my


Write a Haskell program that calculates a balanced partition of N items where each item has a value between 0 and K such that the difference between the sum of the values of first

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing that

how to make the coding

Program is to create a file student.dat: Program is to create a file student.dat which contains all name, roll_no,marks(5 sub) and percentage   class student    {


A: Depends. Might be "zero" Objects which are thrown must have a publicly accessible copy-constructor. The compiler is permitted to generate code which copies the thrown object