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

Assignment Help:

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.

 


Related Discussions:- Why can''t one open a file in a different directory

Program of swapping in c++, Program of swapping two varibales: void sw...

Program of swapping two varibales: void swap(int *, int *);   // This is swap's prototype int main() {                 int x = 5, y = 7;                 swap(&x, &

Determine the size of operator, The size of () operator This is a pseud...

The size of () operator This is a pseudo-operator given by the language, which returns the number of bytes taken up by a variable or data type. The value returned by this opera

Operation on string - c ++ program, Operation on String - C ++ Program: ...

Operation on String - C ++ Program: Write a program to define operations on string in c++. class String {    char *char_ptr;   // pointer to string contents    int le

Lennie, Lennie McPherson, proprietor of Lennie''''s Bail Bonds, needs to ca...

Lennie McPherson, proprietor of Lennie''''s Bail Bonds, needs to calculate the amount due for setting the bail. Lennie requires something of value as collateral, and his fee is 10%

Valid segments 1, Consider text comprised of sentences and sentences compri...

Consider text comprised of sentences and sentences comprised of words. Words in a sentence will be space delimited. Given a text and K strings, task is to find out the number valid

Programing project, Hello, Do you have any idea for programming project to...

Hello, Do you have any idea for programming project to simulate a network attack (threat) mechanism or a defense mechanism. Can I get some suggestions for this. Please include th

What is abstraction?, What is abstraction? - Simplified view of an obje...

What is abstraction? - Simplified view of an object in user's language is termed as abstraction. - It's the simplest, well-defined interface to an object in OO and C++ which

Briefly describe how a linear search algorithm works, Question 1: (a) D...

Question 1: (a) Describe the following objects and condition states: (i) ifstream and ofstream objects (ii) eof(), fail(), bad() and good() functions. (b) Write a C++ pr

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd