从.txt文件中读取浮点类型编号,并在公式中使用它们

问题描述 投票:1回答:1

我正在尝试从.txt文件中读取数字,并将其用于代码中的公式中,并将结果输出到另一个.txt文件中,这样我可以更轻松地使用它们。我的问题是读取3000行数字并为它们分配要在公式中使用的变量。第一列我需要变量dt,第二列我希望它是变量i。无论出于什么原因,我都无法正确阅读它们。这是我的代码。

#include <stdlib.h>
#include <stdio.h>

void GetAcc(const float Acc[], const float Vel[], float Pos[], float dt);
void GetVel(const float Acc[], float Vel[], float dt);

int main()

{
    float Position[3000]={0};
    float Velocity[3000]={0};
    float Acceleration[3000]={0};
    float i;
    float dt;

    FILE *fp;

    fp=fopen("M3 Array File.txt", "r+");
     fscanf(fp,"%f",&dt);
     fscanf(fp,"%f",&i);


    GetVel(Acceleration,Velocity,dt);
    GetAcc(Acceleration,Velocity,Position,dt);

    for(i=0;i<3000;i++)  fprintf(fp,"%3.3f\t%3.3f\t%3.3f\t%3.3f\n",dt*(float)i,Acceleration[i],Velocity[i],Position[i]);
    fclose(fp);

    return  0;
}

void GetVel(const float Acc[], float Vel[], float dt)
{
    int i;

    for(i=1;i<3000;i++)
    {
        Vel[i]=Vel[i-1]+Acc[i]*dt;
    }
}

void GetAcc(const float Acc[], const float Vel[], float Pos[], float dt)
{
    int i;

    for(i=1;i<3000;i++)
    {
        Acc[i]=2*Pos[i]-Pos[i-1]-Vel[i-1];
    }
}

这里是文件外观的示例。

0.000   0.000
0.001   0.000
0.002   0.000
0.003   0.000
0.004   0.000
0.005   0.000
0.006   0.000
0.007   0.000
0.008   0.000
0.009   0.000
0.010   0.000
0.011   0.000
0.012   0.000
0.013   0.000
0.014   0.000
0.015   0.000
0.016   0.000
0.017   0.000
0.018   0.000
0.019   0.000
0.020   0.000
0.021   0.000
0.022   0.000
0.023   0.000
0.024   0.000
0.025   0.000
0.026   0.000
0.027   0.000
0.028   0.000
0.029   0.000
0.030   0.002
0.031   0.003
0.032   0.005
0.033   0.005
0.034   0.005
0.035   0.005
0.036   0.005
0.037   0.006
0.038   0.008
0.039   0.009
0.040   0.011
0.041   0.012
0.042   0.012
0.043   0.012
0.044   0.012
0.045   0.014
0.046   0.015
0.047   0.017
0.048   0.018
0.049   0.020
0.050   0.021
0.051   0.023
0.052   0.025
0.053   0.026
0.054   0.028
0.055   0.029
0.056   0.031
0.057   0.032
0.058   0.034
0.059   0.035
0.060   0.037
0.061   0.040
0.062   0.043
0.063   0.046
0.064   0.049
0.065   0.052
0.066   0.054
0.067   0.055
0.068   0.057
0.069   0.060
0.070   0.063
0.071   0.066
0.072   0.069
0.073   0.072
0.074   0.075
0.075   0.078
0.076   0.081
0.077   0.084
0.078   0.087
0.079   0.091
0.080   0.095
0.081   0.100
0.082   0.104
0.083   0.109
0.084   0.114
0.085   0.117
0.086   0.120
0.087   0.123
0.088   0.127
0.089   0.132
0.090   0.137
0.091   0.141
0.092   0.146
0.093   0.150
0.094   0.155
0.095   0.161
0.096   0.167
0.097   0.173
0.098   0.179
0.099   0.184
0.100   0.189
0.101   0.193
0.102   0.199
0.103   0.206
0.104   0.212
0.105   0.218
0.106   0.224
0.107   0.230
0.108   0.236
0.109   0.242
0.110   0.250
0.111   0.258
0.112   0.265
0.113   0.273
0.114   0.281
0.115   0.288
0.116   0.296
0.117   0.304
0.118   0.311
0.119   0.319
0.120   0.327
0.121   0.334
0.122   0.342
0.123   0.350
0.124   0.359
0.125   0.368
0.126   0.377
0.127   0.387
0.128   0.396
0.129   0.405
0.130   0.414
0.131   0.423
0.132   0.433
0.133   0.442
0.134   0.451
0.135   0.460
0.136   0.471
0.137   0.482
0.138   0.492
0.139   0.503
0.140   0.514
0.141   0.525
0.142   0.535
0.143   0.546
0.144   0.557
0.145   0.569
0.146   0.581
0.147   0.594
0.148   0.606
0.149   0.618
0.150   0.630
0.151   0.643
0.152   0.655
0.153   0.667
0.154   0.680
0.155   0.692
0.156   0.706
0.157   0.719
0.158   0.733
0.159   0.747
0.160   0.761
0.161   0.775
0.162   0.788
0.163   0.802
0.164   0.816
0.165   0.830
0.166   0.845
0.167   0.861
0.168   0.876
0.169   0.891
0.170   0.907
0.171   0.922
0.172   0.937
0.173   0.953
0.174   0.969
0.175   0.986
0.176   1.003
0.177   1.020
0.178   1.037
0.179   1.054
0.180   1.071
0.181   1.088
0.182   1.104
0.183   1.121
0.184   1.140
0.185   1.158
0.186   1.177
0.187   1.195
0.188   1.213
0.189   1.232
0.190   1.250
0.191   1.269
0.192   1.287
0.193   1.307
0.194   1.327
0.195   1.347
0.196   1.367
0.197   1.387
0.198   1.407
0.199   1.427
0.200   1.447
0.201   1.468
0.202   1.489
0.203   1.511
0.204   1.532
0.205   1.554
0.206   1.575
0.207   1.597
0.208   1.618
0.209   1.640
0.210   1.663
0.211   1.686
0.212   1.709
0.213   1.732
0.214   1.755
0.215   1.778
0.216   1.801
0.217   1.824
0.218   1.848
0.219   1.873
0.220   1.898
0.221   1.922
0.222   1.947
0.223   1.971
0.224   1.996
0.225   2.020
0.226   2.045
0.227   2.071
0.228   2.097
0.229   2.123
0.230   2.149
0.231   2.175
0.232   2.201
0.233   2.227
0.234   2.255
0.235   2.283
0.236   2.310
0.237   2.338
0.238   2.365
0.239   2.393
0.240   2.421
0.241   2.448
0.242   2.476
0.243   2.505
0.244   2.534
0.245   2.563
0.246   2.592
0.247   2.622
0.248   2.651
0.249   2.680
0.250   2.709
0.251   2.740
0.252   2.770
0.253   2.801
0.254   2.832
0.255   2.862
0.256   2.893
0.257   2.924
0.258   2.954
0.259   2.987
0.260   3.019
0.261   3.051
0.262   3.083
0.263   3.116
0.264   3.148
0.265   3.180
0.266   3.212
0.267   3.246
0.268   3.280
0.269   3.313
0.270   3.347
0.271   3.381
0.272   3.415
0.273   3.448
0.274   3.482
0.275   3.517
0.276   3.553
0.277   3.588
0.278   3.623
0.279   3.659
0.280   3.694
0.281   3.729
0.282   3.764
0.283   3.800
0.284   3.836
0.285   3.873
0.286   3.910
0.287   3.947
0.288   3.984
0.289   4.021
0.290   4.057
0.291   4.094
0.292   4.133
0.293   4.171
0.294   4.209
0.295   4.248
0.296   4.286
0.297   4.324
0.298   4.363
0.299   4.401
0.300   4.439
0.301   4.479
0.302   4.519
0.303   4.559
0.304   4.599
0.305   4.639
0.306   4.679
0.307   4.719
0.308   4.758
0.309   4.800
0.310   4.841
0.311   4.883
0.312   4.924
0.313   4.965
0.314   5.007
0.315   5.048
0.316   5.090
0.317   5.131
0.318   5.174
0.319   5.217
0.320   5.260
0.321   5.303
0.322   5.346
0.323   5.389
0.324   5.432
0.325   5.475
0.326   5.518
0.327   5.562
0.328   5.607
0.329   5.651
0.330   5.696
0.331   5.740
0.332   5.785
0.333   5.829
0.334   5.874
0.335   5.918
0.336   5.963
0.337   6.009
0.338   6.055
0.339   6.101
0.340   6.147
0.341   6.193
0.342   6.239
0.343   6.285
0.344   6.331
0.345   6.378
0.346   6.426
0.347   6.473
c++ floating-point fstream
1个回答
0
投票

您可以在c ++中使用标记化

将字符串分割成两个空格

#include <vector>
#include <string>
#include <sstream>
#include <iterator>
string line;

istringstream buf(line);
istream_iterator<string> beg(buf), end;
vector<string> tokens(beg, end);
cout << "1st sub String : " << tokens[0];
cout << "2nd sub String : " << tokens[1]; 

将字符串投射到int

int n;
stringstream num(tokens[1]);
num >> n;

这些摘录可能会对您有所帮助,参考:

https://binaramedawatta.blogspot.com/2018/12/supporting-code-snippets-for-oop-take.html

https://www.geeksforgeeks.org/converting-strings-numbers-cc/

http://www.cplusplus.com/forum/beginner/87238/

© www.soinside.com 2019 - 2024. All rights reserved.