title: C reading in from a vertical line character delimited file
body: <p>new to C and trying to read in a text file, "stocks", delimited with vertical line '|' characters. Aim is to open the file which is has a string and float value on each line, in the format:</p>
  
  <pre><code>TOM|149.62
  JIM|23.25
  </code></pre>
  
  <p>I have read other posts about commas, colon and tab delimited files, but the "scanset" suggestion applied here as a <code>[^|]</code> between the <code>%s</code> and <code>%f</code> doesn't seem to work. I seem to have managed to store the first character of the string at least now, but the float value saved is nonsense. This will later be writing to the arrays declared after the file but the basic case of displaying the string and float value for each line means I can continue on my own. Appreciate any help you can give me.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;stdio.h&gt;
  using namespace std;
  
  // TOM|149.62
  // JIM|23.25
  
  
  int main()
  {
      FILE *stocks;
  
      char *stock_Tickers[100];
      float stock_Prices[100];
  
      if ((stocks = fopen("Stocks.txt", "r")) == NULL)
      {
          fprintf(stderr, "Error opening file.\n");
          exit(1);
      }
  
      char tempchar;
      float tempfloat;
  
      for (int i = 0; i &lt; sizeof (stock_Tickers); i++)
      {
          if (feof(stocks))
          {
              break;
          }
          fscanf(stocks, "%s[^|]%f\n",&amp;tempchar,&amp;tempfloat);
          cout &lt;&lt; tempchar &lt;&lt; " " &lt;&lt; tempfloat;
          cin.get();
      }
      return 0;
  }
  </code></pre>
  
  <p>Update: @Michel Billaud Apologies, but I have one last error here. Your method worked perfectly, but when I try it on this slight variant it starts printing rubbish on the last floating point and breaks on subsequent loops. Looked at the local variables and the last array just isn't updating. I think it has to do with the new line, as when I change the float for an integer it still doesn't work. Can you see what I might be doing wrong as it seems the same format to me? Format this time is <code>Ryan|B|IBM|100|176.10</code>. All files are read in after this...Thanks.</p>
  
  <pre><code>FILE *trades;
      // Ryan|B|IBM|100|176.10    
      // Ryan|S|IBM|50|177.10
  
      char trade_User[100][20];
      char trade_Type[100];
      char trade_Tickers[100][4];
      int trade_Quantity[100];
      float trade_Prices[100];
      int trade_Count = 0;
  
      if ((trades = fopen("Trades.txt","r")) == NULL)
      {
          fprintf(stderr, "Error opening file.\n");
          exit(1);
      }
  
      for (int i = 0; i &lt; sizeof(trade_Tickers); i++)
      {
          if (feof(trades))
          {
              break;
          }
      fscanf(trades, "%19[^|]|%1[^|]|%4[^|]|%d[^|]|%f\n", 
          &amp;trade_User[i], &amp;trade_Type[i], &amp;trade_Tickers[i], &amp;trade_Quantity[i], &amp;trade_Prices[i]);
      printf("%s %c %s %d %f\n", 
          trade_User[i], trade_Type[i], trade_Tickers[i], trade_Quantity[i], trade_Prices[i]);
      trade_Count++;
  }
  </code></pre>