The filename I was writing was so long that I couldn't see its entire length. Upon a closer look I saw CrLf or unallowed characters that were causing the problem. I was assuming (always dangerous) that the problem was with the filenumber. My code now removes those characters and the .txt files are being created.