# How do I print a PetscBag to a specific file?

I have a PetscBag in my program that I want to write to a filename I have stored as a string. If I want to write to stdout, there is a pre-made PetscViewer macro for that. How do I create a viewer for a PetscBag that writes to a specified file?

Viewers are discussed in the PETSc user guide. Create one using PetscViewerASCIIOpen or PetscViewerBinaryOpen, then call PetscBagView on your viewer.