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?
Does "\n" work for "fprint()"?
Re: Does "\n" work for "fprint()"?
I also find "\r" does not work neither,
but surprisingly, "\t" works for "fprint()"!...
but surprisingly, "\t" works for "fprint()"!...
-
- 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()"?
fprint has no problem with \n that I can see. Here's an example in which it works just fine:
Can you provide a program that illustrates failure?
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()
Re: Does "\n" work for "fprint()"?
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,
For the "printf()", the output is correct in the window like this
My system is Win7 64.
Thanks!
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()
While for the "fprint()", the results in the "file.dat" looks like this0 0
1 0.309017
2 0.587785
There is no new line between data pairs.0 01 0.3090172 0.587785
My system is Win7 64.
Thanks!
-
- 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()"?
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).thisman wrote:for the "fprint()", the results in the "file.dat" looks like thisThere is no new line between data pairs.0 01 0.3090172 0.587785
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.
Re: Does "\n" work for "fprint()"?
Thanks Ted, I find Matlab can read the .txt file as columns although it shows in rows.