I'm using Netbeans 8.2 and I am programming with C++.
I'm trying to read a file. I have put it in the correct project folder of sources and I have used #include <fstream> .
ifstream myfile ("figures.txt");
code...
myfile.close();
I have the file in the correct place. But this message pops up:
Unrecognized file
Then, my other part of code also gives me error, because never detects the file I'm talking about open.
if (myfile.is_open()){
code...
}else{
cout << "file not open" << endl;
}
Auto solved, the .txt must be in the project folder, not in nbproject folder. At least it has worked doing it this way. Any more reccomendations will be accepted!
Related
I'm having a very weird behaviour in Windows 10 enviroment using both VS2008 and VS2015 when I try to read a file using ifstream.
I'm using the following simple code:
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::ifstream ifs("C:\\Users\\dd\\Desktop\\test.txt");
if(ifs.is_open())
{
std::string line;
while(std::getline(ifs,line))
{
std::cout << line << "\n";
}
}
else
{
std::cout << std::strerror(errno) << "\n";
}
return 0;
}
Obviously the file exists.
When I try to read the file .txt I get:
Permission denied
I run Visual Studio as Administrator and I have the permission to read the file because I am able to open it using Notepad++.
If I change the file extension, for instance .test, I am able to read the file content correctly and everything works as expected.
I uninstalled and then reinstalled VS2008, VS2015 and all C++ Redistributable, but nothing changed. Any help is greatly appreciated!
Perhaps your file isn't named test.txt. Did you check that your file is really named test.txt and not test.txt.txt (with a hidden extension)?
Try to list files in command prompt to see what the real file name is:
cmd>
cd C:\Users\dd\Desktop\
dir | findstr test
Onother possibilities:
test.txt could possibly use similarly looking letters from another
language (in your code or in actual file name).
not properly quoted slashes in your code (e.g. C:\Users\\ instead of C:\\Users\\dd\\). Try to change to forward slashes.
I know there are a ton of questions pertaining to this subject but I cannot get this to work. The program ran fine on my laptop, but when I try to compile and run it in the the schools Linux lab the program cannot open the file. I have tried defining the absolute file position but nothing has worked. The file name is correct and everything but when I try to run the program it displays "failed". I'm using gedit and compiled the program with bash.
ifstream fin("rainfall.dat"); // If the file cannot open display failed
if(fin.fail()){
cout << "failed" << endl;
return 1;
}
try
#include <errno.h>
if(fin.fail())
perror("open failed ");
this will give you a human readable message for the last error
I am having a strange issue. I have a function (writedata) that take a string and writes it to a file. Is works fine and files are created when I build and run the program within xCode.
When I export the compiled project from xCode and just try to run the executable terminal app no file is ever written. I can see the process completes in terminal with no errors given. I even get the 'cout<<"Done. File Written!"' message but no file.
void writedata(string writedata, string filename){
ofstream data(filename, ios::app);
if (data.is_open())
{
data<<writedata;
cout<<"Done. File Written!";
data.close();
}
else cout << "Unable to open file";
data.close();
}
Does anyone know why this is happening?
I have a C++ project that I haven't used in a year. It used to run perfectly, opening files with:
Matrix pmatrix;
pmatrix.readFromFile("pmtx.txt");
and in the function:
void Matrix::readFromFile(string filename){
ifstream tmfile;
tmfile.open(filename.c_str());
if (!tmfile) {
cout << "unable to open tm file";
//return 1;
}
etc
}
It keeps telling me "unable to open tm file". I've been breaking my head about this. Obviously the file is in the directory of the src and I copied it to Debug and Release, I gave it full permissions. I use eclipse.
Any ideas why this just stopped working?
There was something strange screwed up with the eclipse project. I created a new project, copied in existing files and it worked.
Playing around wit boost library using ubuntu, with the following code the file main.cpp gets open and being printed but when I try to open some other file on some other location, it does not work.
Here is the code
std::string line;
boost::filesystem::ifstream file ("main.cpp") ;
if (file.is_open())
{
while ( getline (file,line) )
{
std::cout << line << '\n';
}
file.close();
}
else std::cout << "Unable to open file"<< std::endl;
Here is the path that I am giving and its not working.
boost::filesystem::ifstream file ("/home/0circle/workspace/practice/main.cpp") ;
can someone tell me what could be the problem and how to cure it ?
Thanks :)
Make sure the file exists and you have permissions for reading the file:
execute in shell: ls -l /home/0circle/workspace/practice/main.cpp
verify if you have read rights (first column of the output of the
previous shell command). See this!
try to find out reason of failure: See this!