|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Dec 2004
Messaggi: 783
|
[C] Leggere dati da file in formato scientifico
Salve a tutti, ho il segunete problema. Dovrei leggere dei dati da un file .dat che contiene una serie di numeri in formato scientifico. Al momento sto facendo cosi ma mi butta fuori numeri un po strani:
Codice:
double mat[] /*Le matrici sono double*/ while(fscanf(fp,"%E %E %E %E %E %E",&time[i],&mat1[i],&mat2[i],....,&matn[i])>0) { printf("time= %E, mat1[i]= %E, WIND= %E\n",time[i],XBOAT[i],WIND[i]); i++; if(blablabla....) break; } Codice:
0.00000E+00 0.00000E+00 0.36077E+01 0.00000E+00 0.47400E+02 0.39976E+01 0.90246E+00 0.20000E+00 0.72153E+00 0.36076E+01 -0.15438E-03 0.47395E+02 0.39975E+01 0.90247E+00 0.40000E+00 0.14431E+01 0.36076E+01 -0.18149E-03 0.47395E+02 0.39975E+01 0.90246E+00 0.60000E+00 0.21646E+01 0.36076E+01 -0.18065E-03 0.47395E+02 0.39975E+01 0.90245E+00 0.80000E+00 0.28861E+01 0.36075E+01 -0.17686E-03 0.47395E+02 0.39978E+01 0.90238E+00 1 2 3 4 5 6 .... va tutto bene. Dove sbaglio? ![]() Grazie.
__________________
"May the wind always be at your back and the sun upon your face. And may the wings of destiny carry you aloft to dance with the stars...." |
![]() |
![]() |
![]() |
#2 |
Moderatore
Iscritto dal: Nov 2003
Messaggi: 16211
|
Prova a mettere una "L" tra il % e la E.
Non vorrei sbagliare, ma %E è il formattatore per un float in notazione scientifica. Ma man è un array di double, e i puntatori a double non vengono convertiti "su due piedi" in puntatori a float. Per cui, quando la tua scanf legge i dati, li interpreta come float, ma li scrive in variabili di tipo double; e non si sa cosa viene fuori. Ah, letta adesso su Wikipedia: prova a usare %LG invece di %LE.
__________________
Ubuntu è un'antica parola africana che significa "non so configurare Debian" ![]() Scienza e tecnica: Matematica - Fisica - Chimica - Informatica - Software scientifico - Consulti medici REGOLAMENTO DarthMaul = Asus FX505 Ryzen 7 3700U 8GB GeForce GTX 1650 Win10 + Ubuntu Ultima modifica di Ziosilvio : 14-12-2007 alle 09:52. |
![]() |
![]() |
![]() |
#3 | ||
Senior Member
Iscritto dal: Dec 2004
Messaggi: 783
|
Quote:
Codice:
while(fscanf(fp,"%LE %LE %LE %LE %LE %LE",&time[i],&mat[i],....,&mat[i])>0) Codice:
Dati letti: time= 0.000000E+000 X= 0.000000E+000 W= 3.997600E+000 time= 9.024600E-001 X= 2.000000E-001 W= 4.739500E+001 time= 3.997500E+000 X= 9.024700E-001 W= -1.814900E-004 time= 4.739500E+001 X= 3.997500E+000 W= 3.607600E+000 time= -1.806500E-004 X= 4.739500E+001 W= 2.886100E+000 time= 3.607500E+000 X= -1.768600E-004 W= 1.000000E+000 time= 3.607600E+000 X= 3.607500E+000 W= 9.022800E-001 time= 1.200000E+000 X= 4.329100E+000 W= 3.999000E+000 time= 9.020900E-001 X= 1.400000E+000 W= 4.740500E+001 time= 4.000100E+000 X= 9.018400E-001 W= 4.567200E-005 time= 4.741200E+001 X= 4.001600E+000 W= 3.607500E+000 Premere un tasto per continuare . . . 0.0 0.2 .... alla quinta posizione ho giustamente 1.0 ma nella colonna errata mentre nella sesta tutto torna in modo corretto t =1.2 Quote:
![]() ![]() Grazie mille.
__________________
"May the wind always be at your back and the sun upon your face. And may the wings of destiny carry you aloft to dance with the stars...." |
||
![]() |
![]() |
![]() |
#4 | |
Senior Member
Iscritto dal: Dec 2004
Messaggi: 783
|
Quote:
![]() ![]() ![]() ![]() ![]() C'era una colonna in più. Tutto risolto. Grazie mille di nuovo.
__________________
"May the wind always be at your back and the sun upon your face. And may the wings of destiny carry you aloft to dance with the stars...." |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 15:49.