ifstream can't locate file? - c++

I'm trying to read a file called "sample.txt" but whenever i try to do an ifstream, it can never locate the file. So my question is where do you put the file so that it can be located?
ifstream iFile("sample.txt");

Here is the solution.
1) Open your project in Xcode;
2) Locate the Products folder in the Groups & Files tree on the left side of your Project window;
3) When you expand that folder, you should see the icon for the Unix Executable File (usually a black rectangle);
4) Ctrl-click on that icon and select Show in Finder_ (If you accidentally choose Open With Finder, the Finder will start your program in a Terminal window);
5) Show in Finder should bring up a Finder window showing the contents of the folder which includes your executable. You should see your output files in that window.
Source: https://discussions.apple.com/thread/2145737?start=0&tstart=0

If you don't specify an absolute path the file name is interpreted to be relative to the working directory of the program.

Related

Selecting files with folder using QFileDialog

I have a use case in an application using C++ and Qt (on windows 10). The application uses 7zip.exe as a subprocess to uncompress the files in a selected folder. I need to use QFileDialog to select a folder, and get all the files with extension .zip and .7z, to be selected automatically and then uncompress them using QProcess and display them in the output.
I came up with this code snippet. For selecting the files with selected folders.
void MainWindow::on_browseButton_clicked()
{
QFileDialog d(this);
d.setFileMode(QFileDialog::Directory);
d.setNameFilter("*.zip");
if (d.exec())
qDebug () << d.selectedFiles();
}
but this code does not run, and it displays just the folder name not with no files selected. Could anyone suggest where I am doing wrong.
it displays just the folder name not with no files selected.
That is what it is supposed to return. You asked it to display a dialog to select a folder, so that is all you can select. selectedFiles() will return the path to the selected folder, per the documentation:
https://doc.qt.io/qt-5/qfiledialog.html#FileMode-enum
Constant
Value
Description
QFileDialog::Directory
2
The name of a directory. Both files and directories are displayed. However, the native Windows file dialog does not support displaying files in the directory chooser.
https://doc.qt.io/qt-5/qfiledialog.html#selectedFiles
Returns a list of strings containing the absolute paths of the selected files in the dialog. If no files are selected, or the mode is not ExistingFiles or ExistingFile, selectedFiles() contains the current path in the viewport.
After the dialog has closed and exec() has returned, you will then need to iterate that folder yourself to discover .zip and .7z files in it.
An easier way to handle the dialog is to use QFileDialog::getExistingDirectory() instead. You can construct a QDir from the selected folder, and then use the QDir::entryList() method to search for zip files in the folder.
See How to read all files from a selected directory and use them one by one?.
For example:
void MainWindow::on_browseButton_clicked()
{
QDir directory = QFileDialog::getExistingDirectory(this);
QStringList zipFiles = directory.entryList(QStringList() << "*.zip" << "*.7z", QDir::Files);
foreach(QString filename, zipFiles) {
// do whatever you need to do
}
}

Cant open html resource file using ifstream::open - Visual Studio 2013

I am working on a project which requires me to open an HTML file and use its contents. I added it to Resource files but when I try to open it lie this:
std::ifstream templateFile;
templateFile.open("filename.html", std::ifstream::in);
The operation fails. I checked it by using templateFile.fail().
The above operation works when I provide the full path. The file lies in the project folder along with other files. I tried setting build action to content but still it doesnt work. Please Help.
Output directory, where your executable is compiled and put into differs from the source directory, where you create all your .cpp/.hpp files (I assume there is filename.html file). Local path filename.html is supposed to be local for your executable file, not the source file.
Read more about changing the output directory here: https://msdn.microsoft.com/en-us/library/ms165410.aspx
Under Configuration Properties / Debugging, see what your Working Directory is using the macros dialog box. Move your file into this folder.
Click the button shown in the figure. There, click either Edit or Browse. Browse will take you to the working directory. Edit will expose the link to open the macros box

Open Text File in the root directory

I have made a simple scoring system which upon correct answer, stores the numbers of the player in the file.
I have used the file name like this :
ofstream outfile ("C:\Aadam\Desktop\Project\Scores.txt",ios::app);
But the problem with this approach is that what if I move the program over to a USB and try to run it in another computer. Now it will look in the directory I specified above but there is no Scores.txt file in there.
What I want to do is to give it a path which is in the project folder. So when I move the program, it shouldn't make a difference because I will move the whole project folder.
Of course I can do this :
ofstream outfile ("Scores.txt",ios::app)
which will always look in the project directory and it will work fine as long as I run the program from the IDE but what if I run the program from the .exe file which is two directories down like
"C:\Aadam\Desktop\Project\bin\Debug\Project.exe"
Now in this case, it can't open the file.
So if you know a good way to open files and kindly, Show me the Way.....
You can parse argv[0] (it will contain path used to invoke your executable - absolute or relative) and replace executable name in it with "Scores.txt"
The easiest way is to pass the file path to program as an argument.
When you run a program from IDE, the project directory is considered as current working directory. If the program is run from the command line, the current working directory is from where the command is being run.
If you run the exe file,ofstream outfile ("Scores.txt",ios::app) will create a file named "Scores.txt" in the same directory as your program.

Creating shader files

I'm trying to learn some DirectX11 and I found good tutorial I'm following; however, for some reason the program crashes (The window appears but then a "not responding" message box appears), even though the build was successful. There were some thing about creating shader-files the author did specify on how to do, and i wanted to make sure I'm not making any errors there and that why my program crashes. So my questions are :
How do you create a shader files? In the tutorial we are using the extension .fx, but when you add a file in vs you can only choose from .h and .cpp . Do you only have to select .cpp and then add .fx in the end of the file name?
In what directory is the file suppose to be? (My files are currently in Source Files)
Yes that will work. Another way to do it is just to create a .txt then change the extention to .fx
(To show the file extention just open any folder, click on the organize button in the menu then choose "folder and search options". Uncheck the box "Hide extentions for know file types" under the tab view). After you change the extention you can open the document in notepad and write your code there
Go in to your project folder (in windows file system) and create a map called Data. Add you .fx file there. Then go to your solution explorer and rigth click on the project and choose properties. Go to Build Events -> Pre- Build Events. Add the line "xcopy /y /d "$(ProjectDir)Data" "$(OutDir)" in the box "Command Line". Now when building your project the files will be copied to the rigth place and you will be able to use your .fx file.
The .fx is associate it with an effect file. Meaning you have a combination of many shaders in the same file. Can you post some code of your render loop... also, are you calling the Present method?i.e.,
m_pSwapChain->Present(0, 0);

Where does Xcode create .txt files to?

http://www.cplusplus.com/doc/tutorial/files/
I just finished executing this but I didn't get any file on my desktop.
So where does the .txt file get placed on my computer?
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Damn... I've seen this post never answered many times..
Here's the solution
In your project navigator when you're working in your current project there are many files. Try products (if I remember) and look for the executable file. Now go to the properties inspector or whatever is called. (Right side of your Xcode.)
There you will find somewhere a part called PATH the path that is written over there. It's the path where the executable runs. That means there is where you'll find all the files you create with your program.
Try it... that's how I manage and look my .txt files since sometimes I wanna give them some kind of formatting.
I hope I've solved your problem... cheers! ;)
Click on the executable in the Products tab on the left hand side, and you should see the path to it displayed on the right in the utilities section. This is where they are saved. On my computer this path is:
/Users/myName/Library/Developer/Xcode/DerivedData/CurrentXcodeProject-adsnvetbleazktcgitfymfcbhkkt/Build/Products/Debug/filename.txt
In the "current directory", meaning the working directory of the environment from which you ran the executable.
I can't tell you what that is, but in basic cases it may be the directory where the executable is located. In many other cases it may not be.
Performing a search for the file on your hard drive will reveal its location to you.
Right click on your product and select "Show in Finder". Thats opens the path to where the output files go. Also you can add input files there to be read in by your program.
Found best answer here:
File creation in C++ on Xcode
Basically you can specify a known directory so that all files created go there.
If you are on Mavericks Right click in your user folder with your documents and pictures and stuff (~/Users/yourusername)
Now right click and view options. Check "show library". Now follow to the path that the first user above said.
What I've always done when an application saves a file and I don't know where... is just go back to that application, and hit File>Save As...
So re-open Xcode and go to the "File" menu, and click "Save As..."
It will show you the same directory it just saved to.
I have checked the "Current Directory" but I believe I have to specify the location. Not even
~User/Desktop
will work.
What I do is create an empty file with a the correct name on my desktop then drag that file into my Xcode project to get its path. I then delete the file.
Same with adding files to my project. I can add them from the File menu, but I will need to drag them in to the spot in my code where I want to reference them.
It is kind of convoluted but it is the only system I have.
Some of the answers are confusing. Here's the simplest solution.
1. Expand your Products folder on the left-hand side
2. Right click on the terminal icon(sorry I do not know what it's called)
3. Click show in Finder.
You should be able to see your output file in there.
" Where does Xcode create .txt files to? "
You can decide where you want: follow these->
" Product->Scheme->EditScheme "
chose options tab in the new popup "options"
"working directory" tick "use coatroom working directory" and locate the folder where you want.
The .txt file that you "create", or that you want too "read" has to keep in this folder.
In 2022, click on the project name in the "breadcrumbs" area above the editor. Select the correct thing in "Products", then in the right area, see "Full Path".