copy a existing cpp file in C++ - c++

This is a very basic question:
I just want to copy a existing cpp file into a new empty project.
I create a new empty project Test, then add a existing item Main from other folder, but the address of Main is still the original address and we can not see the Main under the Test folder. Even I copy the Main into the Test folder, the Source file can still not see the Main in the visual studio.

Open up windows explorer. Manually copy the file to the project folder. Then add that existing copy to the project.

Related

My Visual Studio can't find files and so can't do basic operations. What's wrong with it?

Using C++, if I launch an 'empty project', create a new C++ file, and try to run it, I just get this error message:
Unable to run program 'C:\Users\User\source\repos\Project2\Debug\Project2.exe' The system cannot find the file specified
I go to the file it's referencing and the file IS there -- what??
Using a 'Console App' project is different: it will actually compile and run the code.
Similarly, I can't join new header files to the main file, not even in a 'Console App' project: if I write the code for doing #include "Header.h", I get a red line underneath #include, and if I hover over that it says:
cannot open source file "Header.h"
I'm new to coding, and don't know why I'm having such a seemingly absurd problem here. Help!
You should create a new project and then create a c++ file. While adding a file, make sure to select c++ console application and do not check empty project if you are new.
From the result you described, I suspect you used File -> New file to add that cpp file, right? This does NOT add that file to a project.
Instead, you should right-click the Project file in the Solution explorer, select Add new item (or Project -> Add new item menu), and choose the C++ file type.
This works!

Adding a CSV file to a project in Visual Studio

I am working on a project where I have to read in serveral pre-existing CSV (dog.csv, horse.csv, etc.). I want to know how would I add these file into my project so that I may test to see if my print functions work (the code is written in c++). Would I have to copy and paste the files into the debugging folder or would I place it under the test folder of the project?
You can include the files in your project in whatever (sub)folder you wish by using Right click -> Add -> Existing Item. Then, right-click on each file and choose Properties. Set up "Copy to output directory" to "Copy if newer".
Then after build, your files will be copied into the bin/debug folder.
To read the file, you can just use:
System.IO.File.ReadAllText("dog.csv");
Another possible way is to add a file within project, right click and select properties, and then in Copy to Output Directory, select Copy always. This way, csv file will be automatically copied in your debug and release packages too.
string executableLocation = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location);
string csvLocation= Path.Combine(executableLocation, "file.csv");
Above code will read file location from bin directory where your csv file will be stored.
This link should help guide you how to add CSV files to a project.
If you wanted to do a down and dirty way you could just save the CSV's somewhere on your local machine, and then hard code the file path to that location.
Example:
c:\test\Dog.csv and then set that as a variable for whenever you need to read in the csv file.

How to add an image to a C++ project in Xcode

I would like to add an image to my C++ project in Xcode so that I can read that image and do something with it. How do I include the image into my project?
tried copy pasting both into my project and into the folder with my c++ source.
In Xcode 10, I can't put the image in the same folder as the executable, and I don't think it's a good idea anyway as this folder gets deleted when a clean is performed.
The solution I found when dealing with "Command Line Tool" projects in Xcode, is to modify or create a new Copy File Phase (and not a Copy Bundle Resources one) in Targets > Build Phases. But the default values should be modified:
Destination: Resources
Subpath: Nothing
Uncheck Copy only when installing
The image should then be added to this phase (using the + button). Beforehand, when adding the image to the project, selecting a target is not necessary, adding it to the Copy Files Phase will do it automatically.
The code will then look like this:
std::string filename("input.bmp");
bitmap_image image(filename);
Put the image in the same folder with the executable i.e. with the file in the Products folder.

VS2010 failing to locate source file

My project has following structure:
Someproject
include
workspace
bin
projectspace
source
I am using MFC for dialog based application. I add a new dialog and add a new class.Header and source files are generated in workspace/projectspace folder. I do exclude from project, move the files to respective location (includes/source) and do Add Existing Items in VS. Now when I add a button click event from dialog, it creates a new source file in includes directory. How to avoid this?
You should not do exclude. Instead, copy the files to the desired location, then remove from the project. Then Add existing from the new location.

File not being created in correct directory C++

I'm trying to use fstream to create a file, however the file i'm trying to create wont appear in the .exe directory or anywhere else in the file directory. After searching in my computer for the file, I found that was created in a different directory entirely even though i'm using a relative directory.
This is the code to create the file:
ofstream file;
file.open("something.jpg", ios::out|ios::binary|ios::beg);
Directory of created file: C:\Users\user-pc
Directory of project: D:\Users\user-pc\Documents\Visual Studio 2012\Projects\recvFile
by the way, using an absolute directory works perfectly fine. Could this be a problem with the projects working directory?
In your Visual Studio right-click on your project, click Properties, then go to Configuration Properties, then Debugging. There is a row "Working Directory". You can set the working directory there. If you need to do this programmatically, you can use SetCurrentDirectory .
If you need to create the file in the same directory as the .exe location, you can use this approach: https://stackoverflow.com/a/124901/1915854
Call GetModuleFileName() using 0 as a module handle...
If the .exe is installed in a shared directory like Program Files, then creation of the file in the same directory could require additional permissions and may be a bad idea. If the .exe is just cloned to the directory where it should create files, then there is no such problem.
Try adding "../" to the link:
file.open("../something.jpg", ios::out|ios::binary|ios::beg);
File will be created in Debug/Release folder of your project. try what Timo Rzipa suggested.