c++ - C reading in from a vertical line character delimited file -


new c , trying read in text file, "stocks", delimited vertical line '|' characters. aim open file has string , float value on each line, in format:

tom|149.62 jim|23.25 

i have read other posts commas, colon , tab delimited files, "scanset" suggestion applied here [^|] between %s , %f doesn't seem work. seem have managed store first character of string @ least now, float value saved nonsense. later writing arrays declared after file basic case of displaying string , float value each line means can continue on own. appreciate can give me.

#include <iostream> #include<stdlib.h> #include<stdio.h> 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;      (int = 0; < sizeof (stock_tickers); i++)     {         if (feof(stocks))         {             break;         }         fscanf(stocks, "%s[^|]%f\n",&tempchar,&tempfloat);         cout << tempchar << " " << tempfloat;         cin.get();     }     return 0; } 

update: @michel billaud apologies, have 1 last error here. method worked perfectly, when try on slight variant starts printing rubbish on last floating point , breaks on subsequent loops. looked @ local variables , last array isn't updating. think has new line, when change float integer still doesn't work. can see might doing wrong seems same format me? format time ryan|b|ibm|100|176.10. files read in after this...thanks.

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);     }      (int = 0; < sizeof(trade_tickers); i++)     {         if (feof(trades))         {             break;         }     fscanf(trades, "%19[^|]|%1[^|]|%4[^|]|%d[^|]|%f\n",          &trade_user[i], &trade_type[i], &trade_tickers[i], &trade_quantity[i], &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++; } 

hint: when trying use problematic feature, in small separate program. won't take risk draw wrong conclusions because of unrelated issue.

// playing |-separated fields  #include <stdio.h>  int main(int argc, char **argv) {     char line[]="tom|1234.56";     char name[20];     float value;     sscanf(line, "%[^|]|%f", name,  &value);     printf("name = %s, value = %f\n", name, value);     return 0; } 

by way: seems you're using c++ compiler (because of "using namespace std").


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -