Context:
Data produced by complied C code can’t be read by GnuPlot (on a Windows 10 pc).
Details:
Here’s my (vastly simplified for testing) data file "data1"
0.0000e+00 0.0000e+00
5.0000e-01 2.5000e-01
This is produced by
#include <stdio.h>
void main()
{
double x=0.0 , y=0.0;
printf("%.4e %.4en",x,y);
x=0.5; y=x*x;
printf("%.4e %.4en",x,y);
}
compiled on Visual Studio Code 1.84.2 (using the MSYS2 version 12.2.0 of gcc) into an executable I call "SPAM". In the Windows power shell, in that directory, I call .SPAM > data1
.
Using GnuPlot 5.4 (patchlevel 8) gets me this trouble:
gnuplot> plot 'data1'
^
Bad data on line 1 of file data1
Attempts:
Typing those data in directly using plot '-'
works fine.
Viewing the data file with Notepad++ shows no hidden or special characters.
Copy-pasting the contents of the data file into a new file solves the problem!
But I can not be in the business of duplicating all my (actually huge) datafiles.
Question:
- What is hidden in the datafile that GnuPlot does not like?
- Can I solve this through changing settings/flags in GnuPlot?
- Is there something wrong with my installation / build of VSC & gcc that’s producing "weird" files?
2
Answers
Not sure if this might be the solution, but the comment of @helper might be a hint. Newer versions of gnuplot (you have 5.4.8) can definitely read ANSI and UTF-8 encoded text files, but not all variants of them.
I can reproduce your error message when I convert the encoding of the file in Notepadd++, e.g. from UTF-8 to UTF-8-BOM. Right mouse click on the encoding in the status line.
So, what does your Notepad++ show on the right side of the status line for your file as encoding?
In case your C-Code produces UTF-8-BOM or UTF-16 or some others by default, you need to change it in your C-program.
The post I listed:
https://unix.stackexchange.com/questions/171832/converting-a-utf-8-file-to-ascii-best-effort
suggests piping your data through iconv:
or, I think from your comment:
If iconv exists on your system, this should work. The -c discards characters that cannot be represented in ASCII.
I just noticed that if you pipe the data through TYPE in Win 10 it seems to convert to ASCII:
Simple, if it works.