I have a c++ project, add a text file to the project, now I can't read from it I remember in XCode 3 there was property in GetInfo of the Project node, to set the build relative to the project directory and that made it work.
But in XCode 4 I can't find such setting or find a substitute I can only reference files if they are with absolute paths.
#include <iostream>
using namespace std;
int main (int argc, const char * argv[])
{
//This works
freopen("/Volumes/MyHD/XProjects/Test/File.txt", "r", stdin);
//This doesn't
freopen("File.txt", "r", stdin);
int i;
cin>>i;cout<<i;
return 0;
}
Are you referring to the working directory the executable uses when run? If so, you can set the working directory by editing the scheme.
Choose Edit Scheme from the Schemes popup, then choose the Run action from the side bar. Working Directory controls are near the bottom. Check the "Use custom working directory" box then click the little window/folder/card-looking icon (I have no idea what symbol they're going for there) in the right side of the text field and choose your path.
To determine the working directory programmatically works the same as it always has, regardless of the IDE.
Xcode 5: menu bar > Product > Scheme > Edit Scheme...
Note: This Answer is in reply to the following comment because I don't
have enough reputation to reply to comment above:
Where is the "Scehemes Popup"? – aaronsnoswell Nov 28 '13 at 1:51
Related
I'm using CLion 2018.2.6 on MacOS. I'm trying to use ncurses but getting the error "Error opening terminal: unknown." I'm not sure how to fix this. Any help appreciated. Code below.
#include <iostream>
#include <ncurses.h>
using namespace std;
int main(){
initscr();
clear();
printw("Seems legit!");
refresh();
getch();
endwin();
}
The initscr manual page mentions this:
Unset TERM Variable
If the TERM variable is missing or empty, initscr uses the value "unknown", which normally corresponds to a terminal entry with the generic
(gn) capability. Generic entries are detected by setupterm (see
curs_terminfo(3x)) and cannot be used for full-screen operation. Other
implementations may handle a missing/empty TERM variable differently.
Also, depending on how your system is configured, ncurses may not even find the terminal database, e.g., if it is installed in a different location than the compiled-in default location. Like TERM, that can be fixed using the TERMINFO or TERMINFO_DIRS environments. As an additional complication, MacOS by default uses case-insensitive filesystems, and ncurses uses a different directory organization for that. The term(5) manual page mentions that:
A small number of terminal descriptions use uppercase characters in
their names. If the underlying filesystem ignores the difference
between uppercase and lowercase, ncurses represents the "first character" of the terminal name used as the intermediate level of a directory
tree in (two-character) hexadecimal form.
Check the path of terminfo folder in the application running system and the same path in your application like this,
Ex:- setenv("TERMINFO","/usr/share/terminfo", 1);
It working.
all
I'm pretty new to programming and I'm currently teaching myself C++ with sublime text editor. I'm having a problem where the code does not proceed after I input something through cin. For example,
#include <iostream>
using namespace std;
int main() {
string password = "password";
cout << "Enter your password: " << flush;
string input;
cin >> input;
if(input == password) {
cout << "Password accepted." << endl;
} else {
cout << "Access denied." << endl;
}
return 0;
}
After I put my input, it doesn't cout anything such as "password accepted" or "access denied". Does anyone know how to solve this problem and make it work? Thanks.
As mentioned your code does work as expected.
The issue is when you build a c++ program in sublime text it will compile that file and then run. What you see is sublime text piping the output from your program back to a window within sublime text.
Sublime does not have the ability to send input back to your program. Hence why your program "does not proceed after I input something through cin". There are some plugins available on the linux version of sublime that give access to a full terminal emulator, I have not tried using one of these but they do exist.
What I would recommend is that you learn how to use the gnu tool chain using the command line.
This is an old post but since I faced the same problem and this was the first page to pop up when I searched for it, I'll post the solution I think best for other newbros (Does this term work outside of Eve online? Who knows...).
Do remember I am new as well and what I am saying is probably the wrong approach and bad for you, but it will get you started....
Option 1: Use Sublime text to edit and run the file on terminal as everyone suggests. After the first few times it gets extremely repetitive and annoying. As long as you know how to do it, you should be fine. NOT RECOMMENDED.
Option 2: Use VS Code or other IDEs. The problem with this one is that sublime text is just faster to start which is primarily why I like subl. In all honesty VS code is also fast and responsive and I think switching to it is not at all a bad idea.
Option 3: If you still wanna continue using Sublime text. Set it up for something they call competitive programming. Skip the 'bits/stdc++.h' part and do everything else. Follow this link: https://www.geeksforgeeks.org/setting-up-sublime-text-for-cpp-competitive-programming-environment/
In the rare event this webpage stops existing in the future:
Step 1: Have mingw/mingw64 compiler installed and added to path. (You will find plenty of resources that will help you with that).
Step 2:
(i) Open Sublime Text editor and then go to Tools > Build System > New Build System.
(ii) Paste the following code in the file and save it.
{
"cmd": ["g++.exe", "-std=c++17", "${file}",
"-o", "${file_base_name}.exe",
"&&", "${file_base_name}.exe<inputf.in>outputf.out"],
"shell":true,
"working_dir":"$file_path",
"selector":"source.cpp"
}
(iii) Name the file as “CP.sublime-build“ (or anything really, just name something you know so that you can find it from the Tools > Build system list. Also make sure to keep the '.sublime-build' part.)
Step 3: Create these three new files and save them in the same folder. (Keep it somewhere where you store your codes or something like that).
file.cpp: The file for writing the code.
inputf.in: The file where we will be giving the input.
outputf.out: The file where the output will be displayed.
//Also don't change the names of these files since this is part of the code we had earlier.
Step 4: Setting up Window layout:
-Select View > Layout > Columns : 3. This will create three columns in the workspace. Move the three files into three columns.
-Select View > Groups > Max Columns : 2.
-Select the build system we just created from Tools > Build System > CP (or whatever you named it).
This will create a live environment for you write and run/test code at the same time in subl. o7
One way to do this in Submlime is to create a new Build System.
Goto Tools > Build System > New Build System..
Paste something like below into the open file and save it.
{
"cmd": ["bash", "-c", "/usr/bin/g++ '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
}
*This is for Mac OSX, you will have the change it for your environment.
Change Build System to saved name
When you build next time, there will be a new Terminal App window waiting for the cin input.
[Project]
FileName=sjkabljt
Name=sukablyat
Type=1
Ver=2
ObjFiles=
Includes=
Libs=
PrivateResource=
ResourceIncludes=
MakeIncludes=
Compiler=
CppCompiler=
Linker=
IsCpp=1
Icon=
ExeOutput=
ObjectOutput=
LogOutput=
LogOutputEnabled=0
OverrideOutput=0
OverrideOutputName=
HostApplication=
UseCustomMakefile=0
CustomMakefile=
CommandLine=
Folders=
IncludeVersionInfo=0
SupportXPThemes=0
CompilerSet=0
CompilerSettings=0000000000000000101000000
UnitCount=1
[VersionInfo]
Major=1
Minor=0
Release=0
Build=0
LanguageID=1033
CharsetID=1252
CompanyName=
FileVersion=
FileDescription=Developed using the Dev-C++ IDE
InternalName=
LegalCopyright=
LegalTrademarks=
OriginalFilename=
ProductName=
ProductVersion=
AutoIncBuildNr=0
SyncProduct=1
[Unit1]
FileName=files\siplasplas\sukablyat.cpp
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
this is what i get after i open the .cpp file....the file was supposed to get 10 numbers into a[x]; then display them from x+1 to x+10 and then from x+10 to x+1
after that make the sum of all the numbers, and then locate the min and max number....after the program was written i tested it and it all worked...then clicked the top left corner icon and clicked SAVE FILE AS, after that i entered a name and pressed enter while i saved it to desktop to upload it to my schools homework site...so is there anyway to get the code out of this file, or will i have to re write all the lines D: ?
The file you're providing us with is apparently a .dev file, used by Dev-C++. It is used for projects, like Visual C++'s .vcxproj or (sort of) a Makefile used with g++. http://www.file-extensions.org/dev-file-extension-dev-c-project-file
Somehow you saved that .dev file over your source .cpp file. When you overwrite a file, the old version is gone. You'll be well advised to keep backup copies (I copy the entire folder with all its files) in case something strange like this happens again. Sorry you lost your code.
As has been stated in the comments, there is no way to recover the code now that the file has been overwritten/was not saved correctly.
I am using ubuntu. The command to get window id of a window by its name in terminal is
xdotool search --name "name of the window"
But i want to get the same using c++ program.
References:
Sending Keystrokes to a X Window
Github
I cannot compile myself and check, but I think this is the minimal code to search for a window by name.
#include <xdo.h>
Window *list;
xdo_search_t search;
unsigned int nwindows;
memset(&search, 0, sizeof(xdo_search_t));
search.max_depth = -1;
search.require = xdo_search::SEARCH_ANY;
search.searchmask |= SEARCH_NAME;
search.winname = "enter name here";
// the context
xdo_t* p_xdo = xdo_new(NULL);
int id = xdo_window_search(p_xdo, &search, &list, &nwindows);
I've had the same proble for xdo_window_search. Check the function's name in xdo.h, because of changing names.
I've had the same problem with (xdo_window_search changed to xdo_search_windows as ewen said)
undefined reference to xdo_new'
undefined reference to xdo_search_windows'
it was fixed using in terminal
sudo apt-get install libxdo-dev
in addition to, in cpp file,
#include <xdotool-master/xdo.h>
in my particular case, was used for a qt application. For it, I've had to add the line below in .pro file too
LIBS += -lxdo
but the last variable 'id' always returns zero (0).
To get the window ID definitively, I used code in link below, using xdotool command too. In this case, the use should be carefully, because a program can generate more than one ID until fully open and the code runs faster than the ID is available to read. So a timer or a another strategy is necessary.
How do I execute a command and get the output of the command within C++ using POSIX?
When using iostream in C++ on Linux, it displays the program output in the terminal, but in Windows, it just saves the output to a stdout.txt file. How can I, in Windows, make the output appear in the console?
Since you mentioned stdout.txt I google'd it to see what exactly would create a stdout.txt; normally, even with a Windows app, console output goes to the allocated console, or nowhere if one is not allocated.
So, assuming you are using SDL (which is the only thing that brought up stdout.txt), you should follow the advice here. Either freopen stdout and stderr with "CON", or do the other linker/compile workarounds there.
In case the link gets broken again, here is exactly what was referenced from libSDL:
How do I avoid creating stdout.txt and stderr.txt?
"I believe inside the Visual C++ project that comes with SDL there is a SDL_nostdio target > you can build which does what you want(TM)."
"If you define "NO_STDIO_REDIRECT" and recompile SDL, I think it will fix the problem." > > (Answer courtesy of Bill Kendrick)
For debugging in Visual Studio you can print to the debug console:
OutputDebugStringW(L"My output string.");
If you have a none-console Windows application, you can create a console with the AllocConsole function. Once created, you can write to it using the normal std::cout methods.
If you're using Visual Studio you need to modify the project property:
Configuration Properties -> Linker -> System -> SubSystem.
This should be set to: Console (/SUBSYSTEM:CONSOLE)
Also you should change your WinMain to be this signature:
int main(int argc, char **argv)
{
//...
return 0;
}
The AllocConsole Windows API function will create a console window for your application.
If you're using Visual Studio, it should work just fine!
Here's a code example:
#include <iostream>
using namespace std;
int main (int) {
cout << "This will print to the console!" << endl;
}
Make sure you chose a Win32 console application when creating a new project. Still you can redirect the output of your project to a file by using the console switch (>>). This will actually redirect the console pipe away from the stdout to your file. (for example, myprog.exe >> myfile.txt).
I wish I'm not mistaken!
Whether to use subsystem:console or subsystem:windows kind of depends on whether how you want to start your application:
If you use subsystem:console, then you get all of the stdout written to the terminal. The trouble is that if you start the application from the Start Menu/Desktop, you (by default) get a console appearing as well as the application window (which can look pretty ugly).
If you use subsystem:windows, you won't get stdout/stderr even if you run the application from a DOS window, Cygwin, or other terminal.
If you want the middle way which is to output to the terminal IF the application was started in a terminal, then follow the link that Luke provided in his solution (http://dslweb.nwnexus.com/~ast/dload/guicon.htm)
For reference, I ran into this problem with an application that I want to run in either normal Windows mode or batch mode (that is, as part of a script) depending on command-line switches. The whole differentiation between console and Windows applications is a bit bizarre to Unix folks!
First off, what compiler or dev environment are you using? If Visual Studio, you need to make a console application project to get console output.
Second,
std::cout << "Hello World" << std::endl;
should work in any C++ console application.
Your application must be compiled as a Windows console application.
There is a good solution
if (AllocConsole() == 0)
{
// Handle error here. Use ::GetLastError() to get the error.
}
// Redirect CRT standard input, output and error handles to the console window.
FILE * pNewStdout = nullptr;
FILE * pNewStderr = nullptr;
FILE * pNewStdin = nullptr;
::freopen_s(&pNewStdout, "CONOUT$", "w", stdout);
::freopen_s(&pNewStderr, "CONOUT$", "w", stderr);
::freopen_s(&pNewStdin, "CONIN$", "r", stdin);
// Clear the error state for all of the C++ standard streams. Attempting to accessing the streams before they refer
// to a valid target causes the stream to enter an error state. Clearing the error state will fix this problem,
// which seems to occur in newer version of Visual Studio even when the console has not been read from or written
// to yet.
std::cout.clear();
std::cerr.clear();
std::cin.clear();
std::wcout.clear();
std::wcerr.clear();
std::wcin.clear();
I assume you're using some version of Visual Studio? In windows, std::cout << "something"; should write something to a console window IF your program is setup in the project settings as a console program.
If using MinGW, add an option, -Wl,subsystem,console or -mconsole.
You don't necessarily need to make any changes to your code (nor to change the SUBSYSTEM type). If you wish, you also could simply pipe stdout and stderr to a console application (a Windows version of cat works well).