where do I find/create txt file TextFile1.txt?

In this sample code where do I create file with filename TextFile1.txt? I am using VS2017.

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
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::ifstream;
using std::string;

int main()
{
	std::cout << "Hello World!\n";
	int winwidth, winheight, wincolor;       // Declare your variables
	winwidth = winheight = wincolor = 99;   // Set them all to 0
	string path = "TextFile1.txt";          // Storing your filename in a string
	ifstream fin;

	fin.open(path);                        // Open the file
	if (fin.is_open())                      // If it opened successfully
	{
		fin >> winwidth >> winheight >> wincolor;  // Read the values and
						   // store them in these variables
		fin.close();                   // Close the file
	}

	else

	{
		std::cout << " file not opened successfully..." <<std::endl;
	}
	cout << winwidth << '\n';
	cout << winheight << '\n';
	cout << wincolor << '\n';

	return 0;
}



Hello World!
file not opened successfully...
99
99
99
Last edited on
The easiest way to find out where the input file must be is to first create an output file, then place your input file in the directory that contains that output file.


Also, as a hacky debugging method, you can do system("cd"); to print the working directory.
You can set the working directory under properties->configuration properties->general->working directory. Make sure to set it for All configurations
I think I am really missing the mark. Apologies.

string path = "C:\Users\clang\source\repos\fileops5\TextFile1.txt";

error: file not opened successfully

I thought hard coding the location would render success.
closed account (z05DSL3A)
try:
string path = "C:\\Users\\clang\\source\\repos\\fileops5\\TextFile1.txt";
In case it isn't clear, the \ character needs to be escaped so that it isn't used as an escape for the character that follows it.

If you had warnings enabled, you would probably see a warning like:
unrecognized character escape sequence


https://msdn.microsoft.com/en-us/library/thxezb7y.aspx
Open the project's Property Pages dialog box.
Select C/C++.
On the General property page, modify the Warning Level


Please note that you should prefer relative paths and not absolute paths, to make your program more portable.
Last edited on
Thats the thing, this is a problem of orientation for me:

string path = "C:\Users\clang\source3\repos\fileops5\TextFile1.txt";

was

string path = "TextFile1.txt";

Where was TestFile1.txt relative to my .cpp body?
Ok, this worked! But I need to know how to change the path to relative:
string path = "C:\\Users\\clang\\source\\repos\\fileops5\\TextFile1.txt";
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

#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::ifstream;
using std::string;

int main()
{
	std::cout << "Hello World!\n";
	int winwidth, winheight, wincolor;       // Declare your variables
	winwidth = winheight = wincolor = 99;   // Set them all to 0
	string path = "C:\\Users\\clang\\source\\repos\\fileops5\\TextFile1.txt";          // Storing your filename in a string
	ifstream fin;

	fin.open(path);                        // Open the file
	if (fin.is_open())                      // If it opened successfully
	{
		fin >> winwidth >> winheight >> wincolor;  // Read the values and
						   // store them in these variables
		fin.close();                   // Close the file
	}

	else

	{
		std::cout << " file not opened successfully..." <<std::endl;
	}
	cout << winwidth << '\n';
	cout << winheight << '\n';
	cout << wincolor << '\n';

	return 0;
}


And so far its been reading file contents; how do I change the file contents?
Last edited on
closed account (z05DSL3A)
If you are looking for the place to drop the file so you can just use string path = "TextFile1.txt";...

probably something like...
In Visual studio, once the solution is built for the first time.
Go to the Solution Explorer and right click on the 'Solution' node at the top and in the context menu, go down to 'Open Folder in File Explorer' and click.
In the explorer view look for a folder called 'Debug' or 'Release' depending on your build settings. Drop the test file in that folder.
Awesome, that is exactly what I needed.

Then, to answer my own question, to write to the file

1
2
3
4
5
6
7
8
 ofstream fout ("TextFile1.txt");
  if (fout.is_open())
  {
    fout << "This is a line.\n";
    fout << "This is another line.\n";
    fout.close();
  }
It looks like I need to have the TextFile1.txt in both the source folder and the Release. Does that sound right?
closed account (z05DSL3A)
It looks like the default working directory is set to the project directory, so unless you change it the it should be fine in the source directory.

I missed Thomas1965 earlier post about the working directory but it looks like they have moved it...in the project properties it is under configuration properties->Debugging->working directory

Last edited on
most likely something like...

In Visual studio, when the arrangement is worked out of the blue.

Go to the Solution Explorer and right tap on the 'Arrangement' hub at the best and in the setting menu, go down to 'Open Folder in File Explorer' and snap.

In the traveler see search for an organizer called 'Investigate' or 'Discharge' contingent upon your assemble settings. Drop the test record in that organizer.
___________________________________________________________
https://plex.software/ , https://luckypatcher.pro/ ,https://kodi.software/
Last edited on
Topic archived. No new replies allowed.