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;
}
Related
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 read a whole text-file using a simple ifstream.
The code
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::string line;
std::vector<std::string> DataArray;
//std::vector<std::string> QueryArray;
std::string filename = "c:\\helloworld.txt";
std::ifstream myfile(filename.c_str());
//std::ifstream qfile("queries.txt");
if (myfile.fail()) {
perror("c:\\helloworld.txt");
getchar();
return -1;
}
if (!myfile) //Always test the file open.
{
std::cout << "Error opening output file" << std::endl;
system("pause");
return -1;
}
while (std::getline(myfile, line))
{
DataArray.push_back(line);
}
/*if (!qfile) //Always test the file open.
{
std::cout << "Error opening output file" << std::endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}*/
//std::cout << QueryArray[20] << std::endl;
std::cout << DataArray[7] << std::endl;
return 0;
}
The result
I am getting the following result:
Error opening output file
The text-file my_text_file.txt is rightly in the same directory of my program.
Final question
It looks like it can't read my_text_file.txt, why? Did I do something wrong?
Also changing
std::ifstream myfile("c:\\my_text_file.txt");
to
std::ifstream myfile("my_text_file.txt");
doesn't solve the problem.
I think your error can be caused of many different reasons.
There is a handy Error-Message-Interpreteur thing called perror(c++ reference perror).
A standard way to use it(after declaring the file):
if(myFile.fail()){
perror("my_text_file.txt");
return -1;
}
That should give you a more detailed report on the issue, and it can tell you if the problem really is the directory of the file. Hope this helps.
I'm looking to create a file, then open it and rewrite to it.
I've found I can create a file by simply doing this:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile ("test.txt");
outfile << "my text here!" << endl;
outfile.close();
return 0;
}
while this works to create the test file, I cannot open the file and then edit it. this (below) does not work even after the file is created.
outfile.open("test.txt", ios::out);
if (outfile.is_open())
{
outfile << "write this to the file";
}
else
cout << "File could not be opened";
outfile.close;
If by "does not work" you mean that the text is overwritten instead of appended, you need to specify std::ios::app as one of the flags to the call to open to have it append more data instead of overwriting everything.
outfile.open("test.txt", ios::out | ios::app);
The following example works fine for me:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile ("test.txt");
outfile << "my text here!" << endl;
outfile.close();
outfile.open("test.txt", ios::out | ios::app );
if (outfile.is_open())
outfile << "write this to the file" << endl;
else
cout << "File could not be opened";
outfile.close();
return 0;
}
Produces the following text file:
my text here!
write this to the file
You can also do that with FOPEN. Some compilers will notice you that the function its OBSOLETE or DEPRECATED but for me its working good.
/* fopen example */
#include <stdio.h>
int main ()
{
FILE * pFile;
pFile = fopen ("myfile.txt","w");
if (pFile!=NULL)
{
fputs ("fopen example",pFile);
fclose (pFile);
}
return 0;
}
More info here: http://www.cplusplus.com/reference/cstdio/fopen/
I just need to write a string into a file created using ofstream, but I am getting an error.
This is the code:
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
QString aux = "Hello";
ofstream myfile ("test.txt");
if (myfile.is_open())
{
myfile << aux;
myfile.close();
}
else
{
cout << "CANT OPEN FILE";
}
return 0;
}
The error is: no match for 'operator<<' in 'myfile << aux'
P.S: I am using QT4
Thanks for your help!
You should convert to a string by doing :
myfile << aux.toStdString();
This is because the << operator does not know any conversion from qt string.
I'm trying to open a file using ifstream, but no matter what solutions I find that I've tried, nothing seems to work; my program always outputs "unable to open". Below is my code in its entirety. Any help at all is appreciated!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char ** argv)
{
string junk;
ifstream fin;
fin.open("somefile.txt");
if(fin.is_open())
{
fin >> junk;
cout << junk;
}
else
{
cout << "unable to open" << endl;
}
fin.close();
return 0;
}
Also, the contents of somefile.txt, which is in the same directory as the created executable is the following:
SOME
FILE
As some commenters have suggested, it could easily be that the file truly doesn't exist, because you're looking for it in the wrong place. Try using an absolute path to the file rather than just assuming it's looking where you expect.
And output a more helpful error message using strerror(errno).
// ...
fin.open("C:\\path\\to\\somefile.txt");
// ...
else
{
cout << "unable to open: " << strerror(errno) << endl;
}