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
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)
Related
I'm trying to compile a simple "hello world" C++ program but when I use the g++ command I get the error "undefined reference to `WinMain#16" this happens when I compile in vscode or in terminal, whats happening?
code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello World"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
Your error is not a compiler, but a linker error.
That means it still compiles fine but fails to link.
A header file gets included (if it can be found in the include path the compiler uses to find headers), so this works fine.
But for linking two (object-)files together it has to know which files should be linked. And how should it decide which files to use?
That's the cause to create projects in it (and other IDE's) or to create makefiles.
If you turn on full command line-logging in "Settings -> Compiler and debugger... -> Global compiler settings -> [the_compiler_you_use] -> Other settings (rightmost tab) -> Compiler loggin" you can see which commands are sent to the compiler/linker and where the difference is between a single file and a project with multiple files.
By the way: your book/tutorial should explain the steps needed to compile and link files, what happens if headers are included and how to use prebuild libraries/dlls.
I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.
I have several files initialised using paths like this:
String filePath = "/Users/user1/Documents/UWE/Year_3/SDA/GDA GUI Test/Program_Files/modelgraphic1.png";
They display images and when run on another computer the images do not appear. I recall doing something like this in the past:
String filePath = "/.../.../.../.../.../.../GDA GUI Test/Program_Files/modelgraphic1.png";
This isn't working. How can I rectify this? Many thanks.
Boost Filesystem is one of the most reliant libraries, when it comes to paths.
Boost Filesystem Docs => https://www.boost.org/doc/libs/1_66_0/libs/filesystem/doc/index.htm
Reasons for using:
A modern C++ interface, highly compatible with the C++ standard library.
Portability between operating systems.
Error handling and reporting via C++ exceptions (the default) or error codes.
Original answer => https://stackoverflow.com/a/6297807/2303176
Sample Code :
#include <iostream>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main ()
{
fs::path dir ("/tmp");
fs::path file ("foo.txt");
fs::path full_path = dir / file;
std::cout << full_path << std::endl;
}
And then running it -
$ g++ ./test.cpp -o test -lboost_filesystem -lboost_system
$ ./test
/tmp/foo.txt
I'm trying to open a text file in c++ with ifstream but it won't locate the file even though the file is in the same directory as the .cpp file:
#include <fstream>
std::ifstream textInput("words.txt");
if (!textInput) {
return false;
I've triple checked and the file definately exists and is named correctly. I'm not sure if I'm doing something wrong with ifstream or with the path.
EDIT: I put the file in the current working directory of visual studio, it shows the files relative path as "words.txt", but it still can't find the file.
Find out where you application is running (what is know as the "current working directory") using:
TCHAR NPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, NPath);
std::cout << NPath << std::endl;
Or if you are using C++17, you can do it using the standard library:
std::cout << std::filesystem::current_path().string() << std::endl;
Make sure that the file is located in the same path as the above snippets print.
I am trying to compile a simple program but the MingW C++ compiler cannot find the path. I have two files one is C:\main.cpp the other one is C:\Include\test.h
#include <iostream>
#include "test.h"
using namespace std;
int main()
{
cout << "test" << endl;
getchar();
return 0;
}
I have modified the CPATH, CPLUS_INCLUDE_PATH enviroment vars to include the C:\Include path but it still will not compile with g++ c:\main.cpp -o c:\main.exe
Output from command line.
c:\main.cpp:2:18: fatal error: test.h: No such file or directory
compilation terminated.
Also I used this registry file. Still doesn't work.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment]
"LIBRARY_PATH"="C:\\Include"
"C_INCLUDE_PATH"="C:\\Include"
"CPLUS_INCLUDE_PATH"="C:\\Include"
There's not really enough information here, and storing source files in the root is suspect, but you might try:
g++ -I Include c:\main.cpp -o c:\main.exe
Assuming your cwd is C:\
This plus system restart was needed.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment]
"LIBRARY_PATH"="C:\\Include"
"C_INCLUDE_PATH"="C:\\Include"
"CPLUS_INCLUDE_PATH"="C:\\Include"