<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Patrick:</div><div class="gmail_quote">We have replaced this segment of code (reading a data file) with initialized C arrays in petsc master branch. Are you using petsc-release version?</div><div class="gmail_quote"><br></div><div class="gmail_quote">Hong</div><div class="gmail_quote"><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">I tested this out and the strange behavior is very likely because the C string “sp" is not null-terminated, so there is undefined behavior when using snprintf . On different compilers this adds some extra characters (sometimes newlines as below, but arbitrary characters in general). Replacing sp with a null-terminated string fixed the problem for me:<div><br></div><div><div>diff --git a/src/ksp/ksp/examples/tutorials/ex56.c b/src/ksp/ksp/examples/tutorials/ex56.c</div><div>index 16278bb..edccd03 100644</div><div>--- a/src/ksp/ksp/examples/tutorials/ex56.c</div><div>+++ b/src/ksp/ksp/examples/tutorials/ex56.c</div><div>@@ -136,9 +136,10 @@ int main(int argc,char **args)</div><div>     {</div><div>       FILE *file;</div><div>       char fname[256];</div><div>-      char sp[1];</div><div>+      char sp[2];</div><div>       const char* petsc_dir  = getenv("PETSC_DIR");</div><div>       sp[0] = PETSC_DIR_SEPARATOR;</div><div>+      sp[1]='\0';</div><div>       if (!petsc_dir) {</div><div>         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"'PETSC_DIR' not set.");</div><div><div class="h5"><div>       }</div><div><br><div><div>Il giorno 11/mar/2015, alle ore 02:12 AM, Hong <<a href="mailto:hzhang@mcs.anl.gov" target="_blank">hzhang@mcs.anl.gov</a>> ha scritto:</div><br><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Sascha:</div><div class="gmail_quote">I'll replace *.txt file with a C-array to simply ex56.c. </div><div class="gmail_quote">Please ignore my previous request.</div><div class="gmail_quote"><br></div><div class="gmail_quote">Hong</div><div class="gmail_quote"><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hong,<br>
I do not have an immediate explanation for this. In particular, I wonder why<br>
there is no line break after 'sp=/' in this line:<br>
> file = 0x0, sp=/,fname=/Users/Hong/soft/petsc/<br>
but line breaks in the file path.<br>
<br>
On my machine I tested with make runex56, too. I will look at this again tomorrow.<br>
Can anyone else confirm this issue?<br>
<span><font color="#888888"><br>
Sascha<br>
</font></span><div><div><br>
<br>
<br>
On 10 Mar 2015, at 22:22, Hong <<a href="mailto:hzhang@mcs.anl.gov" target="_blank">hzhang@mcs.anl.gov</a>> wrote:<br>
<br>
> Sascha:<br>
> I prepared a patch regarding the reading of the data file in ksp tutorial ex56.<br>
> It sets the path to the data file required for running the example relative to PETSC_DIR<br>
> instead of relative to the tutorials folder to allow running the example from any folder.<br>
><br>
> Also, I used PETSC_DIR_SEPARATOR instead of the currently hard coded ‘/‘<br>
> so that the code would also run on a windows machine … if anyone cared.<br>
> This fails on my MacPro by running 'make runex56':<br>
><br>
> file = 0x0, sp=/,fname=/Users/Hong/soft/petsc/<br>
> src/<br>
> ksp/<br>
> ksp/<br>
> examples/<br>
> tutorials/<br>
> data/<br>
> elem_3d_elast_v_25.txt<br>
>         main failed to open input file '/Users/Hong/soft/petsc/<br>
><br>
> I added<br>
> file = fopen(fname, "r");<br>
>       printf("file = %p, sp=%s,fname=%s\n",file,sp,fname);<br>
>       if (file == 0) {<br>
><br>
> as you see, 'sp=/' is followed by a 'new line'.<br>
> I do not see this behavior on other machines, e.g., linux or an old iMac.<br>
> Any suggestion?<br>
><br>
> Hong<br>
><br>
><br>
> Please discard the patch if you do not want to change the current behavior.<br>
><br>
> Best,<br>
> Sascha<br>
><br>
><br>
<br>
</div></div></blockquote></div><br></div></div>
</blockquote></div><br></div></div></div></div></div></blockquote></div><br></div></div>