ifstream.open() not opening file for reading and writing - c++

i am trying to open file for read/write purpose. Below code is not working ?
can anyone explain me where i m doing wrong?
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
ifstream file;
file.open("program.txt");
if (!file)
{
cout << "failure";
}
return 0;
}
the output of above program is "failure".
but why?
isn't it supposed to open file sucessfully?

If you are using linux / macos. try this code, it will show you the reason of failure.
#include <iostream>
#include <fstream>
#include <stdio.h>
int main()
{
using namespace std;
ifstream file;
file.open("program.txt");
if (!file)
{
perror("open failure");
}
return 0;
}
I guess the reason is "No such file or directory".
Maybe you can try to switch your "current directory" to find the file.

I got the same issue and did some research and not find the cause.
later I tried it using absolute path for the file and find it works.
file.open("program.txt");
------> should be
file.open("/absolute path/program.txt");

Related

not able to open a file in write mode in cpp, strerro says permission denied

I am trying to do a simple file operation by opening it and writing something.
Here is the code:
// image_read.cpp : Defines the entry point for the console application.
#include<cerrno>
#include<cstring>
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
ofstream myfile;
myfile.open("C:/file_example.txt");
cout << myfile.is_open();
if (!(myfile.is_open()))
{
cout << "cannot open the file, error number" << strerror(errno);
}
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
getting the error from cout<<strerror as permission denied
How can I resolve this?
Thanks in advance
Please try to set 'UAC Execution Level' = 'requireAdministrator'.
https://learn.microsoft.com/en-us/cpp/build/reference/manifestuac-embeds-uac-information-in-manifest

Why is this not producing a file in the current directory?

I have some trouble with producing files in C++. I consulted this answer here but when I try using it, it doesn't produce a file. What I wrote:
//~/Documents/Test_CPP/ex2/main_2.cpp
#include <fstream>
int main()
{
std::ofstream file("Hello.txt");
// Hello.txt has been created here
}
I compile it with the command g++ main_2.cpp and run it with ./a.out. I don't really know what could go wrong here, except theorizing that the file might be produced not in the current directory but somewhere else. So I tried changing Hello.txt to ~/Documents/Test_CPP/ex2/Hello.txt, which doesn't change anything. What exactly am I doing wrong here?
I have encountered this problem on macOS with Xcode if you use some IDEs you should point to build-dir.
My suggestion: use std::filesystem::current_path(). It will give full path to you elf\exe dir.
#include <string>
#include <iostream>
#include <filesystem>
#include <fstream>
int main() {
std::string file_name{"Hello.txt"};
auto path{std::filesystem::current_path()};
path = path / file_name;
if (std::filesystem::exists(path)) {
std::filesystem::remove(path);
}
std::ofstream out_stream(path, std::ios::out);
if (!out_stream.is_open()) {
std::cerr << "Error open file" << std::endl;
return -1;
}
out_stream << "test" << std::endl;
out_stream.close();
return 0;
}
This can sometimes happen if you do not properly terminate the connection to the file
EG.
file.close();
This must be done before the program terminates.

Can't open txt files in c++ program with Visual Studio 2019

I just started using Visual Studio 2019 after using XCode for a while.
I was always able to open txt files in XCode but now
I can't open them in Visual Studio 2019.
Basically what I do is I press "Start Without Debugging" in the "Debug" tab I and get the error message "File Did Not Open!" from the else statement that I wrote. I am not sure if it has something to do with where the txt file is located or with the file path.
Below is the simple program that I've so far been using
to figure out how to open txt files in Visual Studio 2019:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream fobj;
fobj.open("input.txt");
if (fobj)
{
cout << "File Opened!\n";
}
else
{
cout << "File Did Not Open!\n";
}
return 0;
}
You are using a relative file path to open the file. The calling process' "current working directory" is likely not what you are expecting (check with GetCurrentDirectory() to verify). Always use absolute file paths when opening files.
For instance, if the file is in the same folder as your EXE, use GetModuleFileName() to get the EXE's full path, then replace the filename portion with your desired filename:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <shlwapi.h>
int main()
{
char filename[MAX_PATH] = {};
::GetModuleFileNameA(NULL, filename, MAX_PATH);
::PathRemoveFileSpecA(filename);
::PathCombineA(filename, filename, "input.txt");
std::ifstream fobj;
fobj.open(filename);
if (fobj)
{
std::cout << "File Opened!\n";
}
else
{
std::cout << "File Did Not Open!\n";
}
return 0;
}

Error in file reading with ifstream

I'm having issue in the ifstream function, I have tried using the argv[1] as parameter but wont load the map, the map is located in the same folder of main code.
I'm stucked here and can not debug.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
int main (int argc, char *argv[]){
int h;
int w;
int var;
string inputLine;
ifstream f;
f.open("map.pgm",ios::in);
if (!f){
cout << "error" << endl;
exit(1);
}
I'm using Visual Studio 2017
Change this line:
if (!f){
by this:
if (!f.is_open()){
BTW you can check current directory path with GetModuleFileName

Why does this code fail to read Blender's .obj file?

Hi I am trying to read a Wavefront file which was created using Blender. I put a copy of this file into the solution Explorer. When I tried to compile for the first time I got the following message:
fatal error LNK1107: invalid or corrupt file: cannot read at 0x...
It seemed like the compiler confused Blender's .obj files with some other format which also uses the .obj ending. The solution was to exclude the file from the build process in its properties.
Now the application does compile but there is no data displayed like I would expect it. Not sure if this is a code issue.
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
void ReadPrintFile(string _fileName)
{
std::string line;
std::ifstream fileStream (_fileName);
if (fileStream.is_open())
{
while (getline(fileStream,line))
{
cout << line << '\n';
}
fileStream.close();
}
else
{
cout << "Unable to read file";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
ReadPrintFile("Drone.obj");
std::cin.get();
return 0;
}
The code does not jump into the else statement. The filestream simply seems to be empty and I am directly forwarded to the cin.get(); statement. I know that there are tons of tutorials on how to parse .OBJ in C++ but I want to understand.
The trick was not to copy the file into the solution explorer but into the project folder.