Pelles C forum

Pelles C => General discussions => Topic started by: tony74 on March 11, 2023, 03:24:02 AM

Title: File Open Error - A Head Scratcher
Post by: tony74 on March 11, 2023, 03:24:02 AM
OK, I've got a head-scratcher.
I saved this webpage to my drive: https://www.reddit.com/r/FREEMEDIAHECKYEAH/wiki/video/
It's a 677k .htm file on the local drive.

I attempt to open it like this:
Code: [Select]
    if((fp=fopen(infile,"rb"))==NULL)
        return ERR;
And it errors out immediately, won't even try to open.

Now, if I take out everything down to line 3157 (the start of the links list) in the .htm file, it opens just fine and the rest of the program runs without error.

I've never run into this before, a file open error that goes away when 3k lines are deleted from the file.
How does what's in a file cause it to fail to even open?
I'm not even processing anything yet, this is the first thing that happens in the program (beside checking args).
I've opened a lot bigger files that this without problems.
Definitely a new one for me!

I haven't tried it in gcc or VS yet.
I'm running Pelles 11.00.2

Any ideas?

Title: Re: File Open Error - A Head Scratcher
Post by: algernon_77 on March 11, 2023, 08:51:32 AM
Hello,

I wasn't able to reproduce the behaviour, but I'm using Pelle 10.
I tried the following code...

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main ( int argc, char ** argv )
{
    FILE * fp;

    if (( fp = fopen ( "video - FREEMEDIAHECKYEAH.htm", "rb" )) == NULL )
    {
        fprintf ( stderr, "UNABLE TO OPEN\n" );
        return 1;
    }

    fprintf ( stderr, "SUCCESS !!!\n" );
    fclose (fp);

    return 0;
}

...and it worked.

So I dunno :)
The only thing appearing to be "special" about that file is that it was saved as a html file from the browser, so it has a "filename_files" folder associated (they're managed as a pair by the shell). But I renamed both of them and it still works. Maybe try saving the file under a different name?
 
Title: Re: File Open Error - A Head Scratcher
Post by: TimoVJL on March 11, 2023, 09:58:25 AM
Is filename in ANSI format ?
Title: Re: File Open Error - A Head Scratcher
Post by: John Z on March 11, 2023, 11:47:33 AM
Hi Tony74

I suspect you have a problem elsewhere giving you a false lead.
I downloaded the exact file in your post and used the code below - no problem.
Code: [Select]
char infile[256]={0};
sprintf(&infile[0],"%s","C://Users/John/Downloads/Tester/video - FREEMEDIAHECKYEAH.htm");
FILE *fp;
    if((fp=fopen(infile,"rb"))==NULL)
      {  MessageBoxA(NULL,"BAD","Filecheck",MB_OK);
return;}
    else
  { fclose(fp);
MessageBoxA(NULL,"OK","Filecheck",MB_OK);
}
Said OK every time.  Then I added a z to the filename and it said BAD, remove the z then OK.

John Z

Pelles C 11 on Windows 11 btw....

Also check your file size "677" or is it 657?  See attached file size picture (typo or maybe yours is corrupt in some way). Does your browser open it?

Another possibility is your antivirus is blocking access because of a flag on the file.  When you edit the file the flag could be removed.  Look at file properties and check the unblock box and try again.  It was not necessary to do on my system but maybe it is on yours.  See third attachment.
Title: Re: File Open Error - A Head Scratcher
Post by: tony74 on March 11, 2023, 07:30:06 PM
OK, it's something on my end. Have no idea what, at this point.
 
I re-downloaded the web page as:
fmhy.htm, from firefox-developer 111.0b8 (64 bit)
I'm on Win7 Pro SP1

Size:         674 KB (690,470 bytes)
Size on Disk: 676 KB (692,224 bytes)
I did click the 'unblock' button in file properties.

re-compiled in Pelles,  same error result.
re-compiled in VS2015,  same error result.
Debug build in VS2015,  error code 0x00000002.
compiled w/John's code, error 'BAD'.
Code: [Select]
//john.c
////////////////////////////
#include <windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    char infile[256]={0};
    FILE *fp;

    sprintf(&infile[0],"%s","C:\\mycollectedprogramming\\reddit3\\fmhy.htm");

    if((fp=fopen(infile,"rb"))==NULL){
        MessageBoxA(NULL,"BAD","Filecheck",MB_OK);
return 0;
    }
    else{
            fclose(fp);
            MessageBoxA(NULL,"OK","Filecheck",MB_OK);
     }

return 0;
}
Attached a screenshot of debug build.

Title: Re: File Open Error - A Head Scratcher
Post by: John Z on March 11, 2023, 08:55:08 PM
Hi,

Just for grins try using my download of the same url.  It is attached here as a 7z.
Naturally this does not include the additional files but just the top level file that
you are trying to open.  This should eliminate the download itself - I hope.


John Z

I have a win 7 pro 64 bit system but I'll need to see if I ever loaded Pelles C onto it.

Looked at your visual studio code:  I don't see where you retrieve the error code into ERR.
I'm not at all familiar with Visual Studio but it seems that there should be a line similar to
DWORD ERR = GetLastError(); once fp is found to be NULL. 
Without this you are probably getting the wrong error code number.
for example:
Code: [Select]
    fp = fopen (p_fname,"rb");
    if (fp == NULL )
      { DWORD ERR = GetLastError();
 //     Show_Error(err,L"File Open");// note you don't have this proc....
return ERR;
      }
Title: Re: File Open Error - A Head Scratcher
Post by: tony74 on March 11, 2023, 09:47:34 PM
Yeah John, still no dice with your file either.

As for the error handling, it works like this (main, being the executive, handles the returns):
Error check:
Code: [Select]
   if((fp=fopen(infile,"rb"))==NULL)
        return ERR;

ERR is :
Code: [Select]
#define ERR 100 in the header.

Main calls:
Code: [Select]
r=extract(fname);
    if(r==ERR){
       printf("%s file error\n",argv[1]);
       return 0;
    }

I noticed that if I delete everything both above and below the list of links, it'll open. But I can't leave anything but the links in there.

I gotta tell you, I've been doing this since Quick-C first came out and I've never run into this before.
So far, it's got me stymied.

If it opened and It choked while processing, I would guess it ran into some html 'minification', where lines could be longer than a buffer size, but that's handled by zeroing the buffers and limiting fgets to bufsize-1...but it doesn't get that far.

Anyway, thanks to all for giving it a shot.
What I'm going to try next is moving this to my grandkid's win10 machine and see if it runs over there.
I can't figure how my system could cause a file-open to choke, but I guess it does, for some reason.

Thanks again, everybody.

 

Title: Re: File Open Error - A Head Scratcher
Post by: tony74 on March 11, 2023, 10:09:08 PM
Oh, I se what you mean about the error #.
No, I don't have a way to retrieve the system error in the code, maybe i'll work on getting that in place!

Thanks!
Title: Re: File Open Error - A Head Scratcher
Post by: tony74 on March 11, 2023, 10:30:38 PM
OK John, after putting the GetLastError() call in there, the error code is still 2. The same as what VS sees.

Thanks again!
Title: Re: File Open Error - A Head Scratcher
Post by: John Z on March 11, 2023, 10:31:10 PM
Here is something you can use/modify to show the error meaning.
Keep the title input to < 200 I did not add error/limit checking
Code: [Select]
//-----------------------------------------------------------------------
// Function : Show_Error
// Task : SHow Error Text
// Returns :
// Input : error code, title text
// Globals      :
//-----------------------------------------------------------------------
void Show_Error(int err, wchar_t *p_title)
{
  LPVOID lpMsgBuf; char ltext[200];
  lpMsgBuf = &ltext;
   
  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                err,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                (LPTSTR) &lpMsgBuf,
                0,
        NULL );

  MessageBox(gHWND,lpMsgBuf,p_title,MB_OK|MB_ICONERROR);

}/* show_error */

Hope it is helpful.

John Z

Update Error Code 2 system can't find the file.
Try printing the filename out to a message box to verify it is correct.
Try moving the file to the top level of the C drive to simplify path.

Note that the above code uses wide char type and supports unicode so you'll need to modify if not using that
Title: Re: File Open Error - A Head Scratcher
Post by: John Z on March 12, 2023, 01:28:55 AM
If the filename looks right in the MessageBox then -

I’m thinking it could be your antivirus software. You might try disconnect from the internet, then disable your antivirus software and try the program again.

John Z
Title: Re: File Open Error - A Head Scratcher
Post by: algernon_77 on March 12, 2023, 01:54:00 AM
I'm also putting money on the antivirus... You should be able to configure it to make an exception for the folder with the html.
Otherwise, it's the pixie fairies :-)
Title: Re: File Open Error - A Head Scratcher
Post by: tony74 on March 12, 2023, 06:51:01 AM
Yeah John, thanks for all the help and advice. I'll implement the error text readout and see what I get.

If it matches the filename, I'll try the antivirus shut down and see if that makes a difference.

That being the case (the AV thing), it doesn't bode well for shipping this to list maintainers, does it?

Doesn't exactly exude confidence in reliability.
Title: Re: File Open Error - A Head Scratcher
Post by: John Z on March 12, 2023, 12:15:08 PM
Hi Tony74,

Well don't be too hard on yourself.  Once your software can access the file YOUR list should be good.
The AV software is picking up something.  If it is the AV blocking the file making it unavailable then
if your provider supports it you might send the file in for analysis of a false positive. 

I use GData and have had to send it several of my OWN programs that it flagged.  GData then adjusted their scanner.

One easy thing you might try first is to rename the the file with a .txt extension.

John Z

BTW I started windows 'C' programming with Quick C as well and still have version 1.0 floppies ;)
Title: Re: File Open Error - A Head Scratcher
Post by: tony74 on March 12, 2023, 05:10:51 PM
Earlier in this fiasco, I did try renaming the .htm file to .txt and nothing changed.
I think what I might try is actually *rewriting* the file as a txt (should the AV thing turn out to be the issue).
That way maybe the AV ( Avira ) would see it as a local file with system ownership.

But being it's Avira, even that is no guarantee.
I've had updates that have put even text files in quarantine, like: .h, .c, .bat, .js, etc.

One time I had to re-download Pelles because Avira pretty much wiped out the installation.
I was able to retrieve my sources, but it was a tedious affair.
Notifying support just got the usual boilerplate response, which means they did nothing.

So, yes, in thinking about it now, it's quite possible the AV is the culprit.

I'll try some things later today.

Title: Re: File Open Error - A Head Scratcher
Post by: tony74 on March 12, 2023, 08:54:22 PM
Well, the file re-write fixed it, The AV must have been the problem all along.
I'll have to include the re-write function in the code and call that before I do anything.

Thanks again for all the help and support!
Title: Re: File Open Error - A Head Scratcher
Post by: tony74 on March 12, 2023, 11:16:00 PM
Re-writing the htm file fixed it. I guess it was the AV after all.
I'll have to put the re-write function in the code and call it first before processing anything.

Thanks again for all the help and support, this was a real head-bender for me.

I'm sure I'll be back for more code-adventures.

Ta!
Title: Re: File Open Error - A Head Scratcher
Post by: John Z on March 12, 2023, 11:23:03 PM
 :)
Good to hear you’ve a path forward. ;)

Stop back anytime we are here all year long!

John Z