0
std::string file = "Cell.txt";
myfile.open (file);

makes a file in current program folder. i dont want the files mixed with the program that is writing them.

std::string file = "Cell\\Cell.txt";

does nothing

std::cout << file << '\n';

prints Cell\Cell.txt

i even tried

std::string file = "\\Cell\\Cell.txt";

did not expect this to work, but tried it anyway

std::string file = "\\\\Cell\\\\Cell.txt";

i have done it before, and can not fine anything on web to help

1 Answers1

0

You say you're not using windows, and yet you create paths like this:

std::string file = "Cell\\Cell.txt";

This isn't a file called Cell.txt in a directory called Cell, but a file called Cell\Cell.txt because backslash path separators are a windows-ism, and under other operating systems they're part of the directory or file name. Use a forward slash instead: Cell/Cell.txt.

Better yet, use the new C++ filesystem libraries to build your paths in a platform-independent sort of manner, and avoid this issue entirely.

#include <experimental/filesystem>
#include <iostream>

namespace fs = std::experimental::filesystem;

int main()
{
  auto path = fs::path("Cell") / fs::path("Cell.txt");

  std::cout << path.string() << std::endl;
}

This will output

Cell\Cell.txt

under windows and

Cell/Cell.txt

under linux, for example. You can also create directories using create_directory.

(note: this works out of the box on windows under vs2017 and probably 2015, but under g++ you'll need to include an extra library at compile time, like this: g++ -std=c++14 -O2 -Wall -pedantic main.cpp -lstdc++fs)

Rook
  • 5,734
  • 3
  • 34
  • 43
  • could not get #include to work, but changing \ to / and manually making folder, worked – michael eric Jun 11 '17 at 21:24
  • @michaeleric which compiler are you using? name and version, if you can. – Rook Jun 12 '17 at 07:55
  • um, NetBeans not sure NetBeans 8.2 Patch 2 dont know where to look for which compiler it is using – michael eric Jun 13 '17 at 01:42
  • @michaeleric It is probably using g++ underneath so there's no reason why it _couldn't_ work. I'm not familiar with the use and configuration of netbeans so I can't advise you how to actually make it work. You could try asking a separate question about this. – Rook Jun 13 '17 at 05:51