skip to Main Content

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:

  1. What is hidden in the datafile that GnuPlot does not like?
  2. Can I solve this through changing settings/flags in GnuPlot?
  3. Is there something wrong with my installation / build of VSC & gcc that’s producing "weird" files?

2

Answers


  1. 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.

    enter image description here

    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.

    Login or Signup to reply.
  2. 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:

    .SPAM | iconv -c -f utf-8 -t ascii//TRANSLIT > data1
    

    or, I think from your comment:

    .SPAM | iconv -c -f UTF-16LE -t ascii//TRANSLIT > data1
    

    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:

    .SPAM >data0
    TYPE data0 >data1
    

    Simple, if it works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search