fopen "w" return errno ENOENT

This is a discussion on fopen "w" return errno ENOENT within the C Programming forums, part of the General Programming Boards category; My old (worked in older versions of OS and compiler) program has these global definitions and code: Code: // file ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    14

    fopen "w" return errno ENOENT

    My old (worked in older versions of OS and compiler) program has these global definitions and code:
    Code:
    // file name hard coded for test purpose only
    char printerfile[64] = "C:\\xxx\\testenv\\hrrss\\files\\lpt1.txt"; 
    FILE *Prn;
    .
    .
    void main ...
    .
    .
    // the (const char *) was added for test purpose
    // I get the same wrong result even when I have 
    //     if((Prn = fopen(printerfile,"w")) == NULL)
    	if((Prn = fopen((const char *)printerfile,"w")) == NULL)
    	{
    		printf("can't open printer; ERROR: %d %s\n", errno, strerror(errno));
    		printf("%s\n", printerfile);
    		exit(1);
    	}
    .
    .
    .
    The program give me:
    can't open printer; ERROR: 2 No such file or directory
    C:\xxx\testenv\hrrss\files\lpt1.txt

    errno 2 is ENOENT - No such file or directory
    The directory C:\xxx\testenv\hrrss\files exists
    I use Visual Studio 2010 on win7 64 bits and I tell the compiler to compile it as C not C++

    What am I doing wrong?
    Thanks
    ZA

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fopen() won't change the argument, if you want to make printerfile const why not declare it const char printerfile[64] ?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Does the path actually exist? Do you actually have permission to write there?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    14
    Quzah: Yes and Yes

    Bayint: I tried it and it did not work either

    Thank you
    ZA

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    Are you sure on the permission thing?

    I mean, are you sure you have write access outside of your own "C:\Users\username" directory structure?

    Have you tested with local (to your username directory)?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    14
    Hi Salem
    It is my own home machine, I have all users (sans the guest) as administrators and I may write, delete or otherwise modify anything and everything on that machine.
    I compile the thing into an .exe file under the Visual Studio 2010. Then I copy it to my testenv sub directory and run it from there.
    However, whatever is the real issue, is there a way to get a better message?
    ZA

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    14
    Apparently, one may not name a file lpt1.txt. Once I changed to lpt1a.txt it worked fine.
    ZA

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    Thanks for the feedback.
    So the magic DOS device filenames are poisoned even with an extension.
    Device file - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    An explanation, but basically extensions would stop the redirection from happening so they're ignored.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 10:06 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  5. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21