Error opening file when debugging - c++

When i try to run the following program:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::ifstream inFile("test.txt");
if(!inFile.is_open()){
std::cout << "Doesn't work" << std::endl;
}
inFile.close();
return 0;
}
The program isn't able to open the file, the file exists in the same folder as the executable(I also tried to put in the explicit path of the file: C:\Users\..)
The value of the variable inFile after trying to open the file is:
+ inFile {_Filebuffer={_Set_eback=0xcccccccc <Error reading characters of string.> _Set_egptr=0xcccccccc <Error reading characters of string.> ...} } std::basic_ifstream<char,std::char_traits<char> >

Try this:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::ifstream inFile("c:\\test.txt");
if(!inFile.is_open())
{
std::cout << "Doesn't work" << std::endl;
}
inFile.close();
return 0;
}
This should work if you put your file to C:\test.txt. The rest is up to you to figure out. Relative paths work, you need to make sure the file is where your program looks for it (or the other way 'round). Try to print out the current path if you are unsure what's wrong.

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

How to change the extension of a file?

I am currently working on project where I need to add some message at the end of a file and then I want to change its extension.
I know how to add the message at the end of the file; my code:
_ofstream myfile;
_myfile.open("check.txt", std::ios_base::app);
_myfile << "Thanks for your help.\n";
How can I change the file's extension?
Actualy, it is very simple:
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
int main(int argc, char* argv[])
{
ofstream fout("test.txt", ios_base::app);
fout << "My cool string";
fout.close();
rename("test.txt", "test.txt1");
return 0;
}

how to read command output line by line in gcc in windows just as with the standard input?

This is what I tried:
#include <iostream>
#include <string>
int main(int argc, char const *argv[]) {
using namespace std;
for (string cin_line; getline(cin, cin_line);) {
cout << cin_line << endl;
}
FILE* pipe = popen("app.exe", "r");
for (string result_line; getline(pipe, result_line);) {
cout << result_line << endl;
}
pclose(pipe);
return 0;
}
It doesn't compile, the result is:
no matching function for call to 'getline(FILE*&, std::__cxx11::string&)'
Second example I've found here: https://stackoverflow.com/a/10702464/393087
But it seems mingw doesn't have pstream included: fatal error: pstream.h: No such file or directory - edit: ok I know, I missed that this is not a GCC library, it is named like it was but this is separate download: http://pstreams.sourceforge.net/
I know how to do it using buffer and get whole output on single line (like here: https://stackoverflow.com/a/478960/393087 ) then explode the line by \n and get my array, but the point here is that I must provide the output as soon as the input comes in.
Also I tried example from here: https://stackoverflow.com/a/313382/393087 - I've added main function to that:
#include <cstdio>
#include <iostream>
#include <string>
int main(int argc, char const *argv[]) {
using namespace std;
FILE * fp ;
if((fp= popen("/bin/df","r")) == NULL) {
// error processing and exit
}
ifstream ins(fileno(fp)); // ifstream ctor using a file descriptor
string s;
while (! ins.eof()){
getline(ins,s);
// do something
}
return 0;
}
This also doesn't compile:
error: variable 'std::ifstream ins' has initializer but incomplete type
ifstream ins(fileno(fp)); // ifstream ctor using a file descriptor
You can't do this:
FILE* pipe = popen("app.exe", "r");
for (string result_line; getline(pipe, result_line);) {
cout << result_line << endl;
}
pclose(pipe);
You need to do this:
#include <boost/noncopyable.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
FILE* pipe = popen("app.exe", "r");
boost::iostreams::file_descriptor_source
source(fileno(pipe), boost::iostreams::never_close_handle);
boost::iostreams::stream<boost::iostreams::file_descriptor_source>
stream(source, 0x1000, 0x1000);
string result_line;
while (getline(stream, result_line)) {
cout << result_line << endl;
}
:)

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.

ifstream not working

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;
}