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
write c++ programm calculate electricity bill with person name,use ,id

C program to check display days and month main() {           int months, days;           clrscr();           printf("ENTER DAYS: ");           scanf("%d", &days);

I have a program and I want someone to fix it for me by using basic c language program.

fine the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder''s name (string), account nu

WAP TO PRINT THE LARGEST NUMBER FROM ANY 10 NUMBERS #include stdio.h> #include conio.h>   void main()   {                    int a[10],i,max;

#question:-program to counts tha no of occurances of the string in the nth padovan string

Why PHP is also known as scripting language? PHP is fundamentally a general purpose language, which is used to write scripts. Scripts are normal computer files which comprises

how do i write a c++ program that will input a number and output its factorial using the while loop

Program is to define a class as employee: Write a program to define a class as employee and collect information about them by using classes and object class employee   {

A: Using the extern "C" linkage specification around the C function declarations. Programmers must know about mangled function names and type-safe linkages. After that they must