How to open a file from a spesific directory


First I want to open a text file and save some data in it.
1)ofstream out;
out.open("C:\\output1.txt"); <= this does not work, it works fine if i try to open a file under visual basic documents(out.open(output1.txt)) but cant open from another directory. Says it cannot be found.

Than I want to read that file later in my program but
I cannot save that file under C:\ so have to open it from C:\Users\W7\Documents\Visual Studio 2010\Projects\proj\proj\output.text but this So I use
system("C:\\Users\\W7\\Documents\\Visual Studio 2010|\Projects\\proj\\proj\\output.text); But this does not work eighter because command window does not understand spaces between Visual studio so it tries to call "C:\\Users\\W7\\Documents\\Visual only.

Any ideas how can i fix this?
You probably don't have permission to write to files in C:\. Why use system? Can't you use pass the path to ofstream?
you dont have the right do write there. you could try to run your exe as an admin.
I have to use system because what i do is,
randomly create some data on a text file,
open dos console, call a 3rd party exe file along with my text file
than redirect the output in dos console into another text file.
I am reading from dos console not from a text file.

How can I gain access to write in C:? I am running my code through compiler not as an exe file.
ofstream out;
out.open("C:\\output9.txt");

I compiled this to see if it creates a file called output9 and started it as an administrator. It did not work.
No it didnt nvm lol
Last edited on
I think you can use spaces in paths in system if you put the path inside quotes.
Well i think it makes sense but i cant.
I am literally going to smash my keyboard into my screen.
I am trying to open a goddamn file for last 4 days and cannot believe such a simple operation giving me such a headache.
maybe the boost library can help you out
maybe the boost library can help you out

How?

Well i think it makes sense but i cant.

Of course you can. You need to escape quotation marks inside a string: \"
I have to use system because what i do is, ...

Usually, apps write temporary files to the temp folder, not the system root.

Windows will tell you the path to temp if you call GetTempPath (it's returned as a short path, i.e. long files names are converted to 8.3 names)

GetTempPath function (Windows)
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa364992%28v=vs.85%29.aspx

And it's good manners to create a subfolder in the temp folder and write your files there. And easy to find them for debugging purposes. (stick %temp% into Windows Explorer address bar, Start/Run %temp%, or cd %temp% from command line).

Andy

PS Mis-using system to list the .cpp files in a folder.

The app needs to be run from a folder containing .cpp files. If successful, then notepad should open to display a list of .cpp files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define _CRT_SECURE_NO_WARNINGS
// uncomment #define to force use of long path
//#define USE_LONG_NAMES
#include <windows.h>
#include <stdio.h>
#include <string.h>

int main()
{
#ifndef USE_LONG_NAMES
    char tempPath[MAX_PATH] = "";
    GetTempPath(_countof(tempPath), tempPath);
    printf("tempPath = %s\n", tempPath);
#else
    char shortTempPath[MAX_PATH] = "";
    GetTempPath(_countof(shortTempPath), shortTempPath);
    printf("shortTempPath = %s\n", shortTempPath);

    char tempPath[MAX_PATH] = "";
    GetLongPathName(shortTempPath, tempPath, _countof(tempPath));
    printf("tempPath = %s\n", tempPath);
#endif

    char folderPath[MAX_PATH] = "";
    strcpy(folderPath, tempPath);
    strcat(folderPath, "~cplusplus");
    printf("folderPath = %s\n", folderPath);

    char cmdLine0[1024] = "";
    sprintf(cmdLine0, "mkdir \"%s\" 2> nul", folderPath);
    printf("cmdLine0 = %s\n", cmdLine0);
    system(cmdLine0);

    char filePath1[MAX_PATH] = "";
    strcpy(filePath1, folderPath);
    strcat(filePath1, "\\dir.txt");
    printf("filePath1 = %s\n", filePath1);

    char cmdLine1[1024] = "";
    sprintf(cmdLine1, "dir /b > \"%s\"", filePath1);
    printf("cmdLine1 = %s\n", cmdLine1);
    system(cmdLine1);

    char filePath2[MAX_PATH] = "";
    strcpy(filePath2, folderPath);
    strcat(filePath2, "\\find.txt");
    printf("filePath2 = %s\n", filePath2);

    char cmdLine2[1024] = "";
    sprintf(cmdLine2, "findstr .cpp \"%s\" > \"%s\"", filePath1, filePath2);
    printf("cmdLine2 = %s\n", cmdLine2);
    system(cmdLine2);

    char cmdLine3[1024] = "";
    sprintf(cmdLine3, "notepad.exe \"%s\"", filePath2);
    printf("cmdLine3 = %s\n", cmdLine3);
    system(cmdLine3);

    return 0;
}


With short names:

tempPath = C:\DOCUME~1\ANDYWE~1\LOCALS~1\Temp\
folderPath = C:\DOCUME~1\ANDYWE~1\LOCALS~1\Temp\~cplusplus
cmdLine0 = mkdir "C:\DOCUME~1\ANDYWE~1\LOCALS~1\Temp\~cplusplus" 2> nul
filePath1 = C:\DOCUME~1\ANDYWE~1\LOCALS~1\Temp\~cplusplus\dir.txt
cmdLine1 = dir /b > "C:\DOCUME~1\ANDYWE~1\LOCALS~1\Temp\~cplusplus\dir.txt"
filePath2 = C:\DOCUME~1\ANDYWE~1\LOCALS~1\Temp\~cplusplus\find.txt
cmdLine2 = findstr .cpp "C:\DOCUME~1\ANDYWE~1\LOCALS~1\Temp\~cplusplus\dir.txt"
> "C:\DOCUME~1\ANDYWE~1\LOCALS~1\Temp\~cplusplus\find.txt"
cmdLine3 = notepad.exe "C:\DOCUME~1\ANDYWE~1\LOCALS~1\Temp\~cplusplus\find.txt"

With long names:

shortTempPath = C:\DOCUME~1\ANDYWE~1\LOCALS~1\Temp\
tempPath = C:\Documents and Settings\Andy Westken\Local Settings\Temp\
folderPath = C:\Documents and Settings\Andy Westken\Local Settings\Temp\~cplusplus
cmdLine0 = mkdir "C:\Documents and Settings\Andy Westken\Local Settings\Temp\~cplusp
lus" 2> nul
filePath1 = C:\Documents and Settings\Andy Westken\Local Settings\Temp\~cplusplus\di
r.txt
cmdLine1 = dir /b > "C:\Documents and Settings\Andy Westken\Local Settings\Temp\~cpl
usplus\dir.txt"
filePath2 = C:\Documents and Settings\Andy Westken\Local Settings\Temp\~cplusplus\fi
nd.txt
cmdLine2 = findstr .cpp "C:\Documents and Settings\Andy Westken\Local Settings\Temp\
~cplusplus\dir.txt" > "C:\Documents and Settings\Andy Westken\Local Settings\Temp\~c
plusplus\find.txt"
cmdLine3 = notepad.exe "C:\Documents and Settings\Andy Westken\Local Settings\Temp\~
cplusplus\find.txt"
Last edited on
If you have problems with the following because it contains spaces:
system("C:\\Users\\W7\\Documents\\Visual Studio 2010|\Projects\\proj\\proj\\output.text);

Then use this instead:
system("\"C:\\Users\\W7\\Documents\\Visual Studio 2010\\Projects\\proj\\proj\\output.text\"");
Last edited on
I think ShellExecute() could be much better choice if you want to execute the program associated with .txt extension, as system() does in that example.
Topic archived. No new replies allowed.