PetscFOpen with mode "a+"

Barry Smith bsmith at mcs.anl.gov
Wed Dec 31 16:32:17 CST 2008


   Here is the relevant code

PetscErrorCode PETSC_DLLEXPORT PetscFOpen(MPI_Comm comm,const char  
name[],const char mode[],FILE **fp)
{
   PetscErrorCode ierr;
   PetscMPIInt    rank;
   FILE           *fd;
   char           fname[PETSC_MAX_PATH_LEN],tname[PETSC_MAX_PATH_LEN];

   PetscFunctionBegin;
   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
   if (!rank) {
     PetscTruth isstdout,isstderr;
     ierr = PetscStrcmp(name,"stdout",&isstdout);CHKERRQ(ierr);
     ierr = PetscStrcmp(name,"stderr",&isstderr);CHKERRQ(ierr);
     if (isstdout || !name) {
       fd = PETSC_STDOUT;
     } else if (isstderr) {
       fd = PETSC_STDERR;
     } else {
       ierr =  
PetscStrreplace 
(PETSC_COMM_SELF,name,tname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
       ierr = PetscFixFilename(tname,fname);CHKERRQ(ierr);
       ierr = PetscInfo1(0,"Opening file %s\n",fname);CHKERRQ(ierr);
       fd   = fopen(fname,mode);
       if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Unable to open file %s 
\n",fname);

   It appears the mode that you pass in is passed into fopen()  
unchanged; thus if you pass in an 'a' or 'a+' that should
be passed into fopen() you can confirm this in the debugger. Start up  
your program (with one process) with the
option -start_in_debugger once it is in the debugger do "break fopen"  
then "cont" (without the quotes). When it gets to the
relevant fopen() check the value of mode. Use the debug version of  
PETSc (that is do not config/configure.py with the
option --with-debugging=0)

    Please report back to petsc-maint at mcs.anl.gov

     Barry

On Dec 31, 2008, at 2:30 PM, (Rebecca) Xuefei YUAN wrote:

>
> I tried PetscViewerFileSetMode and use the "FILE_MODE_APPEND", but  
> it seems that the file has been overwritten,too.
>
> I was not able to find any examples in tutorials, is there one using  
> it?
>
> Thanks a lot!
>
>
> Quoting Matthew Knepley <knepley at gmail.com>:
>
>> You use a ViewerCreate() and then
>>
>> http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-current/docs/manualpages/Viewer/PetscViewerFileSetMode.html
>>
>> to set append, and then FileSetName() to open the file.
>>
>>  Matt
>>
>> On Wed, Dec 31, 2008 at 10:24 AM, (Rebecca) Xuefei YUAN <xy2102 at columbia.edu
>>> wrote:
>>
>>> Hi,All,
>>>
>>> I tried to use PetscFOpen to write the numerical result to a file,  
>>> and I
>>> would like to use the mode "a+" to add my result to the existing  
>>> content in
>>> my file. However, whenever the new result comes out, it will  
>>> overwrite the
>>> existing content in the file. Is there any problem here?
>>>
>>> Thanks a lot!
>>>
>>> Rebecca
>>>
>>>
>>
>>
>> --
>> What most experimenters take for granted before they begin their  
>> experiments
>> is infinitely more interesting than any results to which their  
>> experiments
>> lead.
>> -- Norbert Wiener
>>
>
>
>
> -- 
> (Rebecca) Xuefei YUAN
> Department of Applied Physics and Applied Mathematics
> Columbia University
> Tel:917-399-8032
> www.columbia.edu/~xy2102
>




More information about the petsc-users mailing list