I have file (let's call it "file.txt") which is in a folder /folder/where/the/file/is.
And this folder has been exported to $FOLDER, such as if I do :
echo $FOLDER, I got : folder/where/the/file/is
Now, I want to test if the file exists or not.
So, I tried
ifstream ifile(Name_finput);
if(!ifile.good()){
cout << "File doesn't exist !" << endl;
return;
}
This works if Name_finput = "/folder/where/the/file/is/file.txt", but not if Name_finput=$FOLDER/file.txt
Is there a way for it to work by keeping the form $FOLDER/file.txt ?
It seems that the compiler doesn't interpret $FOLDER as /folder/where/the/file/is.
$FOLDER is not valid C++ code. In order to access the environment variables, you need to use std::getenv(). Here's how your code should look:
#include <iostream>
#include <cstdlib>
#include <fstream>
int main() {
std::ifstream ifile;
if (const char* e = std::getenv("FOLDER")) {
ifile.open(std::string(e) + std::string("/file.txt"));
if (!ifile.is_open()) {
std::cout << "File doesn't exist !" << std::endl;
} else {
// Do-stuff with the file
}
}
return 0;
}
Related
I'm trying to figure out how to write to a file outside the working directory. This is the code I currently have.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string sp{};
std::fstream ss("C:\\Users\\onion\\AppData\\Roaming\\MetaQuotes\\Terminal\\some numbers\\MQL5\\Files\\testnew.txt", std::ios::in | std::ios::out);
if (!ss.is_open()) std::cout << "Failed" << '\n';
else
{
while (ss.is_open())
{
std::getline(ss, sp);
std::cout << sp << '\n';
ss << "new data";
if (ss.eof())break;
}
}
}
I can read the file perfectly fine, but I cant write to it? Could it be that Metatrader itself is limiting my ability to write to a file or does a file have to be in the working directory to be able to write to it? or am I just doing it wrong?
I am trying to run a cpp program on raspberry pi 3 b+ (from 'pi' user) but when I try to open a file with 'fstream' library it doesn't work.
I am using the following code (from main):
std::ios::sync_with_stdio(false);
std::string path = "/NbData";
std::ofstream nbData(path);
if (!nbData) {
std::cout << "Error during process...";
return 0;
}
nbData.seekp(std::ios::beg);
The program always fails there and stops because no file is created (I don't get a fatal error but the test fails and it outputs 'Error during process' which means no file was created).
I am compiling with the following command (there are no issues when I compile):
g++ -std=c++0x nbFinder.cpp -o nbFinder
I have already tried my program on Xcode and everything worked perfectly...
The problem is your path. You must put the file, you are using just the path and if the path do not exist will throw an error. In your case you just using std::string path = "/NbData";, that is you path not your file.
To be able to open your file you need make sure your path exist. Try use the code bellow, he will check if the path exist case not will create and then try to open your file.
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
int main() {
std::ios::sync_with_stdio(false);
std::string path = "./test_dir/";
std::string file = "test.txt";
// Will check if thie file exist, if not will creat
struct stat info;
if (stat(path.c_str(), &info) != 0) {
std::cout << "cannot access " << path << std::endl;
system(("mkdir " + path).c_str());
} else if(info.st_mode & S_IFDIR) {
std::cout << "is a directory" << path << std::endl;
} else {
std::cout << "is no directory" << path << std::endl;
system(("mkdir " + path).c_str());
}
std::ofstream nbData(path + file);
if (!nbData) {
std::cout << "Error during process...";
return 0;
}
nbData.seekp(std::ios::beg);
return 0;
}
ifstream fin;
fin.open("C:\\Users\\Zach\\Desktop\\input.txt");
if (!fin)
{
cout << "e";
}
e is printing whether I use the full pathway or just input.txt from a resource file
If the file exists, make sure that you have got the path specified correctly. Since you're running on Windows, you can verify the full path to your executable with the following code.
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#define BUFSIZE 4096
std::string getExePath()
{
char result[BUFSIZE];
return std::string(result, GetModuleFileName(NULL, result, BUFSIZE));
}
int main()
{
std::ifstream infile("input.txt");
if (infile.is_open())
{
std::cout << "Success!" << std::endl;
infile.close();
}
else
{
std::cout << "Failed to open input.txt!" << std::endl;
std::cout << "Executable path is ->" << getExePath() << "<-" << std::endl;
}
return 0;
}
This will allow you to verify that your path to the input file is correct, assuming that it's collocated with your executable.
You need to direct output into the ifstream object by using fin << "string"; and not directing to standard out via cout.
I'm trying to open a simple txt.file in c++ (visual studio), but are only triggering "else".
codes.txt is together with the main file in source files and are included. This is more or less how it looks
#include <iostream>
#include <fstream>
int main()
{
std::ifstream file("codes.txt");
if (file.is_open())
{
std::cout << "success" << std::endl;
}
else
{
std::cout << "Unable to open file" << std::endl;
}
}
The txt file did not exist in the folder of the program. Runned perfectly after it was included.
I am new to C++.
I was trying to read a file using fstream.
here is the code,
I put the file inside the a.out directory but still cannot read it, where is my mistake?
#include<iostream>
#include<fstream>
int main()
{
std::ifstream myfile("my.txt");
int a, b;
while(myfile>>a>>b)
std::cout<<a<<b;
return 0;
}
Try:
#include <iostream>
#include <fstream>
#include <unistd.h>
int main()
{
char* name = get_current_dir_name();
std::cout << "Current Working Dir: " << name << "\n";
free(name);
std::ifstream myfile("my.txt");
if (!myfile))
{
std::cout << "Failed to open file\n";
exit(1);
}
int a, b;
while(myfile>>a>>b)
{
std::cout<<a<<b;
}
return 0;
}
Make sure that the file is located in the current directory of the .exe. This is usually the same directory as where the .exe is located on your harddrive.
If you don't know what the current directory is, I recommended you use the full path.