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.
Related
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");
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.
I am trying to open a text file, and the code below is my attempt:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
std::ifstream file;
file.open("InputFile.txt");
std::string fileOutput;
if (file.is_open())
{
while (!file.eof())
{
file >> fileOutput;
std::cout << fileOutput << std::endl;
}
}
else
{
std::cout << "File failed to open" << std::endl;
}
file.close();
return 0;
}
The text file is located on my desktop, and it only contain two integers.
Whenever I run the code above, it will show me the "file failed to open" message. I am completely new to c++, so I really don’t have any idea why my code is not working. So any comments would be appreciated.
The text file is located on my desktop
So where is your C++ source file, is it located in my desktop as well?
Note this code file.open("InputFile.txt"); tries to open the InputFile.txt in the current folder, that means it only works if both C++ source file and your text file are in the same folder. That seems to be your problem.
Like #ShadowRanger's this comment, the existing answers are both inaccurate. The argument for file.open() needs to either 1. reflect the relative location of the text file in relation to the current working directory (where you are calling the executable from), or 2. give the absolute location of the text file on the disc.
I suggest the following solution:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char** argv) {
if (argc != 2) {
std::cout << "incorrect number of inputs" << "\n";
std::cout << "correct usage: this_executable.exe file_location" << "\n";
return -1;
}
std::ifstream file;
file.open(argv[1]);
std::string fileOutput;
if (file.is_open())
{
while (file >> fileOutput)
{
std::cout << fileOutput << std::endl;
}
}
else
{
std::cout << "File "<< argv[1] <<" failed to open" << std::endl;
}
file.close();
return 0;
}
This solution takes the file's address info out of the code. With this solution, when you call your executable, the file's address(directory path + file name) is given to the executable at run-time rather than compile-time. Now, you'd run the executable like:
C:\path_to_your_exe>my_executable.exe C:\path_of_your_txt_file\InputFile.txt
The benefits of this approach are:
You can change the file's name / path without having to recompile the code;
On the commandline, it is easier to check that the target file's address is correct by tab completion
Also note:
As #ShadowRanger also pointed out the Why is “while ( !feof (file) )” always wrong? issue which I was not aware of.
If you are wondering what argv[1] means, see this guide for more information on commandline arguments for C++. You also want to make sure to catch situations when the user did not specify an input (meaning argv[1] is invalid, thus the argc != 2)
Unless the file you are opening and your executable are in the same directory, the same message will be printed since it will search for the file in the current working directory. You can specify the absolute path to the file on your desktop using %USERPROFILE%\\Desktop\\InputFile.txt or any other environmental variable that maps the absolute path of a disk, from which your file can be found.
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;
}
I'm trying to encrypt an rsa public key that I have generated using libary poco crypto. The idea came from these answers (C++ Encrypt a text file, allow use of decrypt via ifstream). The pubkey is in the form of
"MIIBIDANBgkqhkiG.........==
I'm using ubuntu 16.04 and poco library version 1.7.8p2 which has been built with OpenSSL 1.0.2g.
The code I use is the following:
#include <iostream>
#include <fstream>
#include "Poco/Crypto/CipherFactory.h"
#include "Poco/Crypto/Cipher.h"
#include "Poco/Crypto/RSADigestEngine.h"
using namespace std;
using namespace Poco::Crypto;
int main(int argc, char** argv)
{
try {
Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey("pubkey.txt"));
}
catch (const exception& exc)
{
cout << exc.what() << endl;
}
}
When I run the above code I get the exception "file access error"
The txt file is given all the permissions to read write and execute.
Afterwards I tried with the istream constructor that RSAKey class provides:
int main(int argc, char** argv)
{
try {
ifstream myfile("pubkey.txt");
Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey(&myfile));
catch (const exception& exc)
{
cout << exc.what() << endl;
}
}
But I got the same error.
It worked when I replaced the above code with the following line:
Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey(RSAKey::KL_1024, RSAKey::EXP_SMALL));
But this is not what I want.
I also print the text file into a string by using
ifstream myfile("pubkey.txt");
string file;
myfile >> file;
file;
and the file is correctly written into the string.
What am I doing wrong in this case?
I have faced the same problem and it turned out to be format error.
A friend of mine saw that the file format was wrong and fixed it and it worked.
Make sure that the file format is right, check every line ending.
Here is a screenshot from VIM for the file. Also, the file extension is .pub or .pem. I'm not sure though if the file extension affect it.