Does "\n" work for "fprint()"?

Anything that doesn't fit elsewhere.
Post Reply
thisman

Does "\n" work for "fprint()"?

Post by thisman »

Hi Ted,
For standard output command "printf()", "\n" works for sure.
But when I use "fprint()" that I want put my results into a .txt file,
the "\n" can not give me a newline, and all results just in a very long row.
I read the "Programmer's Reference", which said "BUGS: Only a subset of the C standard library functions."

I am wondering does "\n" work for "fprint()"?
If not, how can I put my result into a .txt file in a column not in a very long row?
thisman

Re: Does "\n" work for "fprint()"?

Post by thisman »

I also find "\r" does not work neither,
but surprisingly, "\t" works for "fprint()"!...
ted
Site Admin
Posts: 6394
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Does "\n" work for "fprint()"?

Post by ted »

fprint has no problem with \n that I can see. Here's an example in which it works just fine:

Code: Select all

objref y
y = new Vector(3)
y.indgen()
y.printf()

proc w() { local i
   wopen("file.dat")
   for i=0,y.size()-1 {
      fprint("%g %g\n", y.x[i], sin(0.1*PI*y.x[i]))
   }
   wopen()
}

w()
Can you provide a program that illustrates failure?
thisman

Re: Does "\n" work for "fprint()"?

Post by thisman »

Hi ted, I just copied your program and run it in my computer.
I also added a "printf()" after your "fprint()" for compare.
Here is the program,

Code: Select all

objref y
y = new Vector(3)
y.indgen()
y.printf()

proc w() { local i
   wopen("file.dat")
   for i=0,y.size()-1 {
      fprint("%g %g\n", y.x[i], sin(0.1*PI*y.x[i]))
      printf("%g %g\n", y.x[i], sin(0.1*PI*y.x[i]))
   }
   wopen()
}

w()
For the "printf()", the output is correct in the window like this
0 0
1 0.309017
2 0.587785
While for the "fprint()", the results in the "file.dat" looks like this
0 01 0.3090172 0.587785
There is no new line between data pairs.
My system is Win7 64.
Thanks!
ted
Site Admin
Posts: 6394
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Does "\n" work for "fprint()"?

Post by ted »

thisman wrote:for the "fprint()", the results in the "file.dat" looks like this
0 01 0.3090172 0.587785
There is no new line between data pairs.
No, there _is_ a newline, but it's the UNIX/Linux newline, and you would see it if you examine the file's contents character-by-character. What's missing is a carriage return (\r).

If you insist on having MS-DOS style newlines, put \r\n in your format strings, instead of \n. But you're much better off abandoning MS-DOS newlines. Why take extra effort to write nonportable code? Any decent spreadsheet and scientific graphing software automatically knows how to read Mac, MS-DOS, and UNIX/Linux text files. And so do good programmer's text editors--you'll find many free and commercial programmer's editors for MSWin listed elsewhere in the NEURON Forum, which will allow you to read, write, and exchange text files with OS X and UNIX/Linux users.
thisman

Re: Does "\n" work for "fprint()"?

Post by thisman »

Thanks Ted, I find Matlab can read the .txt file as columns although it shows in rows.
Post Reply