Thread: fscanf won't work for long files

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    fscanf won't work for long files

    Hello I'm having problems using fscanf. I'm using it to read data from a file into an array and for short files it works fine i.e. with about 12 bits of data but now I have a file with say 200 bits of data and this just causes the program to crash does anyone know what I'm doing wrong please. I've included a copy of the program below followed by the data from one of the long files I'm trying to read.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    char file[10];
    
    FILE *data;
    
    int main ()
    
    
    {
            {
                do
                {
                printf ("Type in the name of the data file: ");
                scanf ("%s", &file);
    
                //Open file
    
                data = fopen(file, "r");
    
                if (data == NULL)
                printf("\n ERROR - Cannot open the designated file \n") ;
    
                }
                while (data==NULL);
    
            }
    
        int count;
        int count1;
        float a[count];
    
        count=0;
    
                while(!feof(data)) {
                fscanf(data,"%f", &a[count]);
                count++;}
    
            // Test to print values from file
            for(count1=0; count1<count-1; count1++)
            printf("Is = %f\n", a[count1]);
    }
    File

    0
    0.381
    0.584
    0.813
    0.991
    1.067
    1.219
    1.448
    1.651
    1.677
    1.372
    0.965
    0.711
    0.508
    0.457
    0.508
    0.483
    0.127
    -0.482
    -0.965
    -1.295
    -1.448
    -1.905
    -2.616
    -3.353
    -4.419
    -5.867
    -7.213
    -8.28
    -9.195
    -10.007
    -11.023
    -12.344
    -13.563
    -14.122
    -13.995
    -13.462
    -13.081
    -13.005
    -12.878
    -12.776
    -12.928
    -12.954
    -13.106
    -13.335
    -13.411
    -13.411
    -13.513
    -13.513
    -13.386
    -13.259
    -12.878
    -12.395
    -12.09
    -11.76
    -11.227
    -10.846
    -10.668
    -10.617
    -10.261
    -9.728
    -9.398
    -8.991
    -8.534
    -8.331
    -8.128
    -7.798
    -7.747
    -7.671
    -7.34
    -6.985
    -6.553
    -6.248
    -5.994
    -5.893
    -6.02
    -6.223
    -6.655
    -7.061
    -7.315
    -7.544
    -7.62
    -7.772
    -8.026
    -8.382
    -8.839
    -9.347
    -9.652
    -9.703
    -9.753
    -9.804
    -10.134
    -10.49
    -10.592
    -10.769
    -11.125
    -11.404
    -11.252
    -10.871
    -10.668
    -10.49
    -10.185
    -10.134
    -10.363
    -10.465
    -10.414
    -10.185
    -9.931
    -9.906
    -10.007
    -10.033
    -9.88
    -9.626
    -9.449
    -9.322
    -9.093
    -8.814
    -8.61
    -8.534
    -8.483
    -8.483
    -8.28
    -7.798
    -7.417
    -7.036
    -6.655
    -6.197
    -5.715
    -5.41
    -4.877
    -3.911
    -3.2
    -3.2
    -3.353
    -3.175
    -2.921
    -2.641
    -2.26
    -1.905
    -1.498
    -0.889
    -0.228
    0.457
    1.042
    1.448
    1.575
    1.702
    2.235
    3.074
    3.836
    4.547
    5.487
    6.63
    7.112
    6.325
    5.309
    5.309
    5.944
    6.274
    6.223
    6.02
    5.664
    5.283
    5.029
    4.725
    4.394
    4.166
    3.836
    3.328
    3.124
    3.226
    3.455
    3.886
    4.115
    4.115
    4.14
    4.369
    4.801
    5.106
    5.131
    5.258
    5.588
    6.02
    6.172
    5.995
    5.918
    5.969
    6.02
    6.198
    6.477
    6.909
    7.417
    7.849
    8.027
    7.849
    7.544
    7.392
    7.671
    8.281
    8.611
    8.839
    8.941
    8.789
    8.56
    8.433
    8.433
    8.611
    9.246
    9.525
    8.509
    7.163
    6.579
    6.503
    6.604
    6.579
    6.579
    6.858
    7.468
    8.179
    8.789
    9.246
    9.652
    9.805
    9.551
    9.246
    9.195
    9.297
    9.398
    9.576
    10.008
    10.084
    9.83
    9.906
    10.16
    10.389
    10.795
    10.948
    10.465
    9.627
    8.712
    7.849
    7.239
    6.731
    6.249
    5.918
    5.563
    5.106
    4.471
    3.836
    3.531
    3.302
    2.947
    2.616
    2.515
    2.54
    2.566
    2.718
    2.972
    3.074
    2.947
    2.87
    2.896
    2.972
    3.251
    3.505
    3.759
    3.988
    3.937
    3.734
    3.861
    4.217
    4.598
    4.928
    5.233
    5.487
    5.512
    5.588
    6.071
    6.553
    6.909
    7.392
    7.925
    8.484
    8.89
    9.017
    8.789
    8.484
    8.535
    8.839
    9.144
    9.195
    9.271
    9.5
    9.678
    9.805
    10.008
    10.338
    10.643
    10.821
    11.024
    11.405
    11.837
    12.319
    12.903
    13.64
    14.224
    14.554
    15.088
    15.824
    16.764
    17.907
    18.796
    19.33
    19.939
    20.65
    21.311
    22.047
    22.835
    23.343
    23.343
    22.886
    22.632
    22.632
    22.403
    22.124
    22.2
    22.124
    21.844
    21.438
    20.879
    20.396
    19.99
    19.507
    18.923
    18.339
    17.856
    17.45
    16.891
    16.002
    15.113
    14.453
    14.072
    13.843
    13.64
    13.437
    13.31
    13.157
    12.802
    12.167
    11.481
    10.948
    10.44
    9.932
    9.449
    8.916
    8.408
    7.874
    7.417
    7.112
    6.706
    6.096
    5.461
    5.029
    4.801
    4.598
    4.217
    3.759
    3.226
    2.82
    2.642
    2.439
    1.905
    1.245
    0.686
    0.051
    -0.787
    -1.549
    -2.184
    -2.616
    -2.819
    -3.073
    -3.581
    -4.013
    -4.013
    -3.988
    -4.14
    -4.191
    -4.216
    -4.292
    -4.242
    -3.886
    -3.327
    -2.87
    -2.895
    -3.226
    -3.759
    -4.267
    -4.521
    -4.826
    -5.054
    -5.105
    -5.385
    -5.766
    -5.969
    -5.943
    -5.893
    -5.689
    -5.766
    -6.299
    -6.502
    -6.553
    -6.553
    -6.578
    -6.782
    -6.985
    -6.934
    -6.756
    -6.959
    -7.366
    -7.391
    -7.137
    -7.036
    -7.061
    -6.858
    -6.655
    -6.604
    -6.553
    -6.705
    -6.985
    -6.985
    -6.782
    -6.553
    -6.121
    -5.74
    -5.918
    -6.172
    -6.147
    -5.842
    -5.435
    -4.851
    -4.318
    -4.013
    -3.683
    -3.378
    -3.073
    -2.946
    -2.997
    -2.743
    -2.311
    -1.829
    -1.371
    -1.143
    -0.914
    -0.813
    -1.016
    -1.422
    -1.803
    -2.032
    -2.311
    -2.514
    -2.591
    -2.565
    -2.514
    -2.565
    -2.616
    -2.616
    -2.845
    -3.276
    -3.505
    -3.505
    -3.505
    -3.607
    -3.835
    -3.962
    -4.013
    -4.242
    -4.673
    -5.029
    -5.258
    -5.486
    -5.588
    -5.588
    -5.689
    -5.867
    -5.918
    -5.816
    -5.664
    -5.512
    -5.308
    -5.004
    -4.978
    -5.131
    -5.308
    -5.258
    -5.08
    -4.927
    -4.75
    -4.572
    -4.724
    -4.927
    -4.927
    -4.75
    -4.623
    -4.47
    -4.242
    -4.089
    -4.038
    -4.292
    -4.47
    -4.419
    -4.267
    -4.038
    -3.886
    -3.962
    -4.064
    -4.013
    -3.784
    -3.581
    -3.505
    -3.429
    -3.53
    -3.683
    -3.657
    -3.48
    -3.429
    -3.378
    -3.2
    -3.099
    -3.099
    -2.921
    -2.54
    -2.514
    -2.743
    -2.616
    -2.26
    -1.905
    -1.575
    -1.448
    -1.498
    -1.346
    -0.889
    -0.381
    -0.051
    0.127
    0.254
    0.28
    0.229
    0.229
    0.254
    0.381
    0.407
    0.051
    -0.559
    -1.041
    -1.092
    -0.889
    -0.584
    -0.203
    0.153
    0.432
    0.584
    0.737
    1.092
    1.473
    1.778
    2.21
    2.439
    2.312
    2.312
    2.515
    2.566
    2.718
    3.074
    3.429
    3.658
    3.531
    3.277
    3.328
    3.658
    3.836
    3.988
    4.267
    4.293
    4.115
    3.937
    3.937
    4.14
    4.344
    4.394
    4.293
    4.166
    4.115
    4.09
    4.013
    4.039
    4.293
    4.826
    5.385
    5.69
    5.893
    6.02
    6.02
    6.172
    6.376
    6.376
    6.35
    6.401
    6.452
    6.274
    5.969
    5.893
    5.791
    5.588
    5.563
    5.614
    5.36
    5.029
    5.004
    5.233
    5.639
    6.172
    6.858
    7.265
    6.757
    6.045
    6.02
    6.553
    7.087
    7.646
    8.179
    8.179
    8.027
    7.925
    7.849
    7.595
    7.087
    6.706
    6.198
    5.715
    5.385
    5.106
    5.131
    5.233
    5.233
    4.877
    3.988
    3.15
    2.972
    3.251
    3.505
    3.455
    3.201
    2.997
    2.921
    2.769
    2.515
    2.134
    1.753
    1.524
    1.296
    1.042
    0.864
    0.864
    0.762
    0.432
    0.254
    0.559
    0.965
    1.194
    1.346
    1.473
    1.245
    0.965
    0.94
    0.915
    0.483
    -0.076
    -0.178
    -0.101
    -0.178
    -0.203
    -0.076
    -0.051
    0.102
    0.254
    0.229
    0.051
    -0.101
    -0.203
    -0.152
    -0.178
    -0.584
    -1.041
    -1.473
    -1.879
    -1.93
    -1.956
    -2.26
    -2.591
    -2.87
    -3.073
    -3.226
    -2.997
    -2.718
    -3.53
    -5.715
    -7.442
    -7.645
    -7.34
    -7.239
    -7.29
    -7.188
    -7.213
    -7.34
    -7.518
    -7.95
    -8.433
    -8.687
    -8.839
    -9.22
    -9.677
    -9.931
    -9.83
    -9.398
    -8.966
    -9.017
    -9.906
    -10.82
    -11.125
    -11.252
    -11.379
    -11.43
    -11.277
    -10.769
    -10.439
    -10.515
    -10.642
    -10.693
    -10.795
    -11.023
    -11.252
    -11.43
    -11.404
    -11.354
    -11.277
    -11.125
    -10.947
    -10.871
    -11.023
    -11.252
    -11.557
    -11.76
    -11.633
    -11.15
    -10.642
    -10.363
    -10.185
    -10.109
    -10.211
    -10.515
    -10.795
    -10.82
    -10.769
    -10.82
    -10.82
    -10.719
    -10.668
    -10.642
    -10.465
    -10.287
    -10.058
    -9.626
    -9.195
    -8.737
    -8.483
    -8.636
    -8.585
    -8.356
    -8.255
    -8.28
    -8.229
    -7.95
    -7.747
    -7.747
    -7.798
    -7.848
    -7.899
    -7.899
    -8.077
    -8.433
    -8.636
    -8.687
    -8.534
    -8.28
    -8.153
    -8.229
    -8.331
    -8.356
    -8.61
    -8.89
    -8.966
    -8.966
    -9.042
    -9.144
    -9.22
    -9.22
    -9.245
    -9.372
    -9.499
    -9.652
    -9.626
    -9.499
    -9.372
    -9.347
    -9.601
    -9.779
    -9.703
    -9.525
    -9.271
    -9.093
    -8.89
    -8.458
    -8.077
    -7.696
    -7.34
    -7.036
    -6.832
    -6.629
    -6.299
    -5.867
    -5.41
    -5.004
    -4.953
    -4.978
    -4.724
    -3.962
    -3.124
    -2.591
    -2.235
    -2.083
    -2.159
    -2.286
    -2.235
    -2.032
    -1.981
    -1.956
    -2.032
    -2.337
    -2.54
    -2.489
    -2.286
    -2.083
    -2.032
    -2.159
    -2.286
    -2.286
    -2.032
    -1.778
    -1.6
    -1.498
    -1.524
    -1.625
    -1.752
    -1.879
    -1.854
    -1.651
    -1.575
    -1.6
    -1.549
    -1.346
    -1.244
    -1.346
    -1.448
    -1.397
    -1.041
    -0.305
    0.33
    0.508
    0.534
    0.457
    0.33
    0.305
    0.203
    -0.076
    -0.178
    -0.33
    -0.533
    -0.457
    -0.203
    0.102
    0.559
    1.042
    1.321
    1.448
    1.473
    1.423
    1.423
    1.448
    1.473
    1.346
    1.118
    1.118
    1.372
    1.524
    1.372
    1.118
    0.94
    0.813
    0.965
    1.499
    2.083
    2.439
    2.235
    1.702
    1.524
    1.981
    2.693
    3.328
    3.886
    4.445
    4.953
    5.385
    5.817
    5.842
    5.258
    4.902
    4.928
    5.233
    5.766
    6.147
    6.325
    6.553
    6.782
    6.909
    6.833
    6.782
    6.731
    6.528
    6.249
    6.096
    5.918
    5.766
    5.766
    5.588
    5.131
    4.547
    4.166
    4.217
    4.496
    4.826
    4.979
    4.877
    4.496
    4.14
    4.242
    4.547
    4.852
    5.258
    5.436
    5.207
    5.029
    5.004
    5.004
    5.004
    4.826
    4.445
    4.064
    3.582
    3.15
    2.769
    2.439
    2.312
    2.337
    2.261
    2.159
    2.032
    1.778
    1.753
    2.083
    2.413
    2.54
    2.54
    2.693
    2.921
    3.048
    3.277
    3.556
    3.683
    3.505
    3.251
    3.251
    3.353
    3.226
    3.15
    3.378
    3.404
    3.175
    3.023
    2.845
    3.226

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > float a[count];
    This does NOT create an array which magically expands as you increase the value of count.

    All that you're getting is an array whose size is based on the uninitialised value of count (at the point you declare the array).

    Investigate the use of malloc and possibly realloc to create space you can extend at run-time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or, if you don't really care about saving every single byte possible, guess on a huge number that your file won't exceed - say 10000 - and create an array of that size. Just make sure that you do fail gracefully if the file happens to be bigger.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. How do you work with resource files???
    By Finchie_88 in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2004, 02:39 PM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM