Reading txt file in Visual studio 2019 C++ - c++

I am trying to open a file for this program. I have tried pathing it directly using examples like C:\User... but for some reason it still says it can't find the file. I have looked on the internet as well as youtube to open files using C++ and its pretty straight forward. However I still can't get this .txt file to be read. Maybe it has to do with Visual Studio?
FILE* in_fp, * fopen();
int main() {
if ((in_fp = fopen("TextFile.txt", "r")) == NULL)
printf("ERROR - cannot open front.in \n");
else
{
getChar();
do {
lex();
} while (nextToken != EOF);
}
}

Related

Decompressing a zip containing files with Cyrillic names with lbarchive

For my C++ project I need a library to decompress various formats of archives. I found libarchive was the best solution for this, however when I started testing it I ran into a problem. I tried running an untar example from the library over a zip containing files named in Russian and it just refuses to work.
archive_read_support_compression_all(a);
archive_read_support_format_all(a);
archive_read_open_filename(a, filename, 10240)));
for (;;) {
r = archive_read_next_header(a, &entry);
if (r == ARCHIVE_EOF) {
break;
}
if (r != ARCHIVE_OK) {
if (r == ARCHIVE_WARN) {
warn("archive_read_next_header()", archive_error_string(a)); // This warning goes off
} else {
fail("archive_read_next_header()", archive_error_string(a), 1);
}
}
msg(archive_entry_pathname(entry)); // entry is null
}
}
When I run this over other archive formats containing the same files everything is fime but for zip it fails with
archive_read_next_header() failed: Pathname cannot be converted from UTF-8 to current locale.
What can I do to make this work? I'm generally fine with the names being gibberish at this point
The system I'm running is Ubuntu 20.04 if that's important

fopen / ofstream::open fail when creating a BMP file

Years ago I created a C++ function using FILE to create bitmap files. Recently (not sure when or why) this code is now failing when opening the file. The problem is with the open call ...
file_ptr = fopen("ScreenShots/Screenshot1.bmp", "wb");
Currently this results in an error 13, permission denied error. Change the filename extension to something else and the fopen works fine. For example,
file_ptr = fopen("ScreenShots/Screenshot1.bm2", "wb");
The file saves correctly and when changing the extension back to BMP I can display the file correctly in Paintshop.
Did a quick check using ofstream and same problem.
Any ideas why I get a permission denied error when trying to open BMP files to write data? For information I am using Visual Studio Community 2017 on Windows 10.
To give the complete section of code ...
BITMAPFILEHEADER bitmap_header;
BITMAPINFOHEADER bitmap_info;
FILE *file_ptr;
unsigned int count;
unsigned char tempRGB;
char filename[256];
bool finished;
// CREATE A UNIQUE FILENAME
count = 1;
finished = false;
do
{
// CREATE NAME
sprintf(filename, "ScreenShots/Screenshot%d.bmp", count);
// CHECK IF FILE EXISTS
errno = 0;
file_ptr = fopen(filename, "rb");
if (file_ptr)
{
// FILE EXISTS
fclose(file_ptr);
count = count + 1;
}
else
{
// UNIQUE FILENAME
file_ptr = fopen(filename, "wb");
if (file_ptr == NULL)
{
// UNABLE TO OPEN FOR WRITING - GIVE UP
// (USING OWN LOGGING CLASS)
jalog.log("\nERROR on Screenshot >");
jalog.log(filename);
jalog.log("< >");
jalog.log((short)errno);
return;
}
finished = true;
}
}
while (finished == false);
I've managed to find the issue ... Avast antivirus. I noticed that trying to do an open action for a BMP file took a few seconds while opening any other file type (successfully or unsuccessfully) was instantaneous. As something similar happens when running new programs I tried disabling all the Avast shields and I could successfully create a BMP file using the existing code.
For my own personal use I can whitelist my own programs, but annoying if I get to distributing the program to other people.
Thanks for the help ... and sorry for raising a C++ issue that in the end had nothing to do with C++!

Cannot open file /sys/class/net/eth0/carrier

I have following function isEthernetCableConnected() which I am calling in a thread in continuous loop. After a long time I started to get log "Could not open /sys/class/net/eth0/carrier". How can it be possible? If it is possible then please give me some idea how to open the file every time.
ETH_FILE_CARRIER /sys/class/net/eth0/carrier
int isEthernetCableConnected(){
FILE *fp = fopen(ETH_FILE_CARRIER, "r");
int result;
if(fp == NULL) {
CLog::getInstance()->error("utility",__LINE__,__FILE__,"networked::isEthernetConnected, Could not open %s", ETH_FILE_CARRIER);
return 0;
}
fscanf(fp,"%d",&result);
fclose(fp);
return result;
}
Please refer the below link, it might help you
http://wikistack.com/how-to-check-if-ethernet-cable-is-connected-linux
In this link, they are trying to open the file in different way. May be you can try that.

Load the output of another command line program into mine

Ok suppose I have a program ( in windows a .exe file ) and when I run it, it outputs some information... now I'm writing another program ( in c++ ) and I need it to automatically run that .exe file and read the output so that it can process that information for further actions...
what should I do ?
Use popen or on windows (per comment) _popen. Basically it functions as the thing behind the | in some program | thing.
Normally I'm against posting complete code but I literally wrote this today and have it on hand, so, here you go. From what I understand C++ doesn't have a great interface that replaces popen but if you're bringing in the boost libraries or something at that layer there are solutions.
Note I use char[10] because in my application I know the output will be that short.
PopenWrapper(const std::string& command) {
fd = popen(command.c_str(), "r");
if(fd == NULL) {
throw PopenException("Failed to open command: " + command);
}
}
std::string get() {
char line[10];
fgets(line, sizeof(line), fd);
return std::string(line);
}
~PopenWrapper() {
if(fd != NULL) {
pclose(fd);
}
}

How to handle JPEG #41 error

I have a project in C++ Builder6. There's an OpenDialog where I upload images to the project. I'd like my project to be safe and because it only accepts .jpg or .bmp images I decided to make a restriction. As far as I'm concerned I can recognize a .jpg file by setting my stream reader to the 4th position. If I find "JFIF" here, It'll be .jpeg file. And so on.
Here's my code
if(OpenDialog1->Execute())
{
TFileStream *stream = new TFileStream(OpenDialog1->FileName, fmOpenRead);
if(stream != NULL)
{
if(stream->Size < 10)
{
delete stream;
return;
}
char str[10];
stream->Read(str, 10);
if(AnsiString(str + 6).SetLength(4)=="JFIF")
{
ShowMessage("It's jpeg");
}
else if ( AnsiString(str).SetLength(2)=="BM") {
ShowMessage("It's bmp");
}
else
{
ShowMessage("It can not be downloaded");
return;
}
}
delete stream;
}
But unfortunately that code raises an exception about JPEG error #41 when I put here a text file with renamed extension.
So my idea doesn't work. The whole question is:
Can I make my program return my error messages without using try-catch method?
By the way, I understand why the exception is being raised, because my jpeg file is empty. But I'd like to handle it using my own system, not the standart exception.