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.
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 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;
}
i'm having trouble reading from a text file in C++, so basically I want to read the text file on "cmd" using this code, however, an error pops up.
#include <iostream>
#include <fstream>
#include <string>
using namespace::std;
int main()
{
string line_;
ifstream file("Seminario.txt");
if(file.is_open()){
while(getline(file, line_)){
cout << line_ << endl;
}
file.close();
}
else{
cout << "File is not open" << endl;
cin.get();
return 0;
}
}
When I compile it, I get no errors, however, when I run a.exe, a window pops up saying:
The procedure entry point
_ZNSt7__cxx1112basic_stringlcSt11char_traitslcESalcEEC1Ev could not be located in the dynamic link library C:\Users\pc\Desktop\Progra\a.exe.
How can I fix this?
Premise: I'm using CLion.
As i said in title, when i try to open a file (txt) nothing will be displayed.
i can't explain it, i don't think i made an error, it's pretty easy this code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
FILE *leggi;
leggi = fopen("lorem.txt", "r");
char datiLetti[1000];
while(fgets(datiLetti, 1000, leggi)!=NULL){
cout << datiLetti << endl;
}
fclose(leggi);
system("PAUSE");
return EXIT_SUCCESS;
}
file "lorem.txt" is in the same directory of the project.
Thank you in advance.
EDIT1: file is lorem not lorem_ipsum, my mistake when i typed here.
You want this:
...
FILE *leggi;
leggi = fopen("lorem.txt", "r");
if (leggi == NULL)
{
cout << "Can't open file" << endl;
return 1;
}
...
---FIXED---
Installed cygwig1.dll and cygstdc++-6.dll and put cygwig in glob variables, then my file worked in the same directory of main and exe.
However, thank you guys for your time!
fopen is a C solution for open a file if you want to open a file in c++ use fstream like flowing code.
fopen is deprecated in c++11.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
fstream myfile;
myfile.open("example.txt");
cerr << "Error: " << strerror(errno);
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
I'm trying to write a program to parse the first and sixteenth columns of a CSV file (converted into .txt). I have the CSV ("posts.txt") document in the folder with the executable. But, whenever I try to run the executable, my program delivers that it cannot open the file (or that "!infile.is_open()"). Mind giving me some assistance? I'm running in Xcode 3.2.3 on Mac OSX 10.8.3. The code is shows below.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string.h>
using namespace std;
void answeredPostGrabber()
{
ifstream inFile("posts.txt");
string postNumber;
string answerNumber;
string throwAway;
if(inFile.is_open())
{
while(inFile.good())
{
getline(inFile,postNumber,',');
cout << postNumber << ",";
for(int y=1;y++;y<16)
{
getline(inFile,throwAway,',');
}
getline(inFile,answerNumber,',');
cout << answerNumber << endl;
ofstream edges;
edges.open("edges.txt",ios::app);
edges << postNumber << "," << answerNumber<< endl;
edges.close();
ofstream nodes;
nodes.open("nodes.txt",ios::app);
nodes << postNumber << "\n" << answerNumber << endl;
nodes.close();
getline(inFile,throwAway);
}
}else cout << "ERROR: Unable to open file." << endl;
}
int main ()
{
answeredPostGrabber();
return 0;
}
Thank you in advance!
I have the CSV ("posts.txt") document in the folder with the executable.
The file should be present in the current working directory of your process, which may or may not be the same directory where the executable lives. If in doubt, try specifying the full path in ifstream inFile(...); to see whether that changes things.
Additionally, the file needs to have the correct permissions to ensure that it's readable by the process.