Thread: Code failing to create file

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4,183

    Code failing to create file

    I am trying to build some third party code that is an compiler for the old MC6809 CPU.

    Code:
            /*  Create an asm file that will receive the assembly language code:
            */
            if (numErrors == 0)
            {
                if (verbose)
                {
                    cout << "Assembly language filename: " << asmFilename << "\n";
                    cout << flush;
                }
                ofstream asmFile(asmFilename.c_str(), ios::out);
                if (!asmFile)
                {
                    int e = errno;
                    cout << PACKAGE << fatalErrorPrefix << "failed to create assembler file " << asmFilename
                         << ": " << strerror(e) << endl;
                    return EXIT_FAILURE;
                }
                if (!asmText.writeFile(asmFile))
                {
                    cout << PACKAGE << fatalErrorPrefix << "failed to write output assembly file " << asmFilename << endl;
                    return EXIT_FAILURE;
                }
                asmFile.close();
                if (!asmFile)
                {
                    cout << PACKAGE << fatalErrorPrefix << "failed to close output assembly file " << asmFilename << endl;
                    return EXIT_FAILURE;
                }
            }
    
            if (verbose)
                cout << numErrors << " error(s)"
                        << ", " << numWarnings << " warning(s)." << endl;
    The output from this section of code and the sub-program it calls is
    Code:
    Assembly language filename: atol.s
    0 error(s), 0 warning(s).
    Assembler command: lwasm -fobj --pragma=forwardrefmax -D_COCO_BASIC_ --output='atol.o' 'atol.s'
    Cannot open file ''atol.s'': No such file or directory
    Exit code from assembler command: 1
    Any suggestion of code to add to trace the problem; it is a large program built using configure/make so using debugger would like be beyond me.

    Edit: I have search the file atol.s does not exist.
    Edit2: I am thinking this test "if (!asmFile)" is not correct.
    Edit3: Link to full file cmoc/main.cpp at master * stahta01/cmoc * GitHub
    The snippet posted is around line 1250.
    Edit4: I am porting the code to work under Windows 10; it is designed for Linux; but, has worked in the past under windows with, unknown to me, minor changes.

    Tim S.
    Last edited by stahta01; 04-26-2023 at 07:29 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Can anyone tell me how to get the location the file is being saved into?

    Edit: I am trying the below
    Code:
    cout << "Current path is " << std::filesystem::current_path() << endl;
    Tim S.
    Last edited by stahta01; 04-26-2023 at 08:45 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Consider changing it to this, so you can see if there is any surprise white space in the name.
    Code:
    cout << "Assembly language filename: >>" << asmFilename << "<<\n";
    In the absence of any of the error messages, you're almost certain to have a file somewhere on the file system.

    Do you run the program directly in the IDE?
    IDE writers do seem to like monkeying around with whatever the "current directory" is for the program being debugged/executed.

    For debugging purposes (aka - not portable), use this to get the current dir
    GetCurrentDirectory function (winbase.h) - Win32 apps | Microsoft Learn
    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.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Was running it as part of building the compiler using configure/make build method.
    I spend the last few hours moving the code into an Code::Blocks project; used the same MinGW GCC 32 bit compiler; it still has the bug.
    So, next will be getting the debugger to work with the CB IDE; likely be a few days before I figure out what the problem is.

    Based on the test code I added the file was created; but, the test code might have been wrong. But, where the file was created is unknown.
    I am wondering if something is deleting it like an virus scanner. But, I changed the setting of my virus scanner and it still was not there.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    sysinternals process monitor springs to mind as a quick alternative.
    Process Monitor - Sysinternals | Microsoft Learn

    Set it to just watch your process and it will tell you the real path of the files it accesses.
    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.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Using CB Debugger, the file is created and later deleted.

    The line below is calling the program that cannot find the file.
    Code:
    int status = invokeAssembler(asmFilename, compilationOutputFilename, lstFilename, targetPreprocId, verbose);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Found the cause; but, not yet the solution.

    Code:
    Cannot open file ''atol.s'': No such file or directory
    Those are two sets of single quotes not a double quotes.

    Single quotes around a filename must work okay under Linux; but, it fails under Windows.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I have decided just to remove the single quotes for now.

    Any reason the Author needed single quotes around filenames under Linux?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    They're only necessary when the filename contains characters that are interpreted by the shell (like space).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. N00b @ C: Code is failing at line 12 which is a basic scanf
    By Mike Garber in forum C Programming
    Replies: 3
    Last Post: 09-01-2013, 10:25 AM
  2. Replies: 10
    Last Post: 05-14-2013, 07:49 PM
  3. fread failing to read full file
    By DeusAduro in forum C Programming
    Replies: 3
    Last Post: 05-27-2009, 12:21 PM
  4. Failing to open() a file
    By sa125 in forum C Programming
    Replies: 4
    Last Post: 07-31-2008, 11:37 PM
  5. program failing while trying to open/create file
    By 1978Corvette in forum C Programming
    Replies: 17
    Last Post: 04-26-2006, 10:55 AM

Tags for this Thread