2

I am writing a code in C to read a text file containing words followed by numbers in each line.

Here is my code :

#include <stdio.h>

#define HEIGHT 2
#define WIDTH  3

int main (void) {

#define MAX_WORDS 1000     /* Maximum words in a file */
#define MAX_VECTOR 500     /* Maximum length of vector */
#define WORD_LENGTH 100    /* Maximum length of a word */

    char words[MAX_WORDS][WORD_LENGTH];
    double numbers[MAX_WORDS][MAX_VECTOR];
    char ch, ch1;
    FILE *fp = fopen("test.txt", "r");
    int i = 0, j = 0;

    while (fscanf(fp, "%s%c", &words[i][j], &ch1) != EOF) {
        printf("line %d Read word %s \n", i, words[i]);
        while (!feof(fp) && (fscanf(fp, "%lf%c", &numbers[i][j], &ch) > 0)) {
            j++;
        }
        j = 0;
        i++;
    }    
    return 0;
}

Here is the text file I am reading from:

one 0.055901 -0.118006 0.165313 0.180452 0.026175 0.061411 0.066986 0.193535 -0.083337 -0.225704 -0.060372 0.052417 -0.158475 0.037001 -0.120630 0.033111 -0.120100 0.067668 0.008704 -0.011523 0.046506 0.049087 -0.008813 0.127401 0.046158 -0.042573 -0.069881 0.003825 -0.028860 0.240443 0.035992 0.224830 -0.090131 -0.119056 -0.110844 -0.111364 0.084449 0.140994 0.094664 0.075708 -0.133953 0.062725 0.059535 -0.058782 0.029325 -0.042819 -0.106856 0.061583 0.031914 0.306222 -0.110337 -0.309405 -0.114899 -0.010856 -0.014989 0.151228 0.096698 0.146314 -0.157354 -0.126845 0.066820 -0.073393 -0.154290 -0.052042 -0.203586 0.187294 -0.122756 0.017946 0.016923 0.017999 0.185696 0.177917 0.044500 -0.117416 -0.232413 0.108233 -0.119608 -0.007704 0.127663 -0.092061 0.175927 0.079500 0.041586 0.099287 -0.033317 -0.093927 -0.026158 0.135621 0.008939 0.000643 0.117516 -0.118172 -0.209071 0.204900 0.049810 -0.004155 0.062825 -0.106713 0.057214 0.160900
in 0.119589 -0.101502 0.157670 -0.056303 0.093967 -0.040834 -0.020207 0.137023 -0.039777 -0.219717 -0.158505 -0.039380 -0.175083 -0.157390 0.012432 -0.041457 -0.042300 0.145984 0.069490 -0.150424 -0.053245 0.114765 0.061037 -0.151042 -0.065063 0.018581 -0.124555 0.066553 -0.026960 0.077988 -0.082175 0.187142 -0.124159 -0.039581 -0.034646 -0.109485 -0.060480 -0.011689 0.056724 0.165933 0.122378 0.203326 -0.010104 -0.098575 0.137054 -0.088888 0.053554 0.109095 0.020053 0.041943 -0.102528 -0.121394 0.041362 0.082621 -0.123843 0.051251 0.200228 -0.039035 0.090276 -0.108120 -0.052834 -0.014503 -0.052226 0.048963 0.109746 0.021199 -0.032484 -0.105986 0.083844 0.255063 -0.073283 0.082236 -0.106431 -0.117873 -0.151227 0.070045 -0.124128 0.026318 0.065747 -0.084784 0.207990 0.032585 0.038751 0.006927 -0.087952 -0.007880 0.015686 0.084194 -0.004951 0.017214 0.046750 -0.015060 -0.052989 0.002429 -0.153604 0.140979 -0.059959 0.056583 0.094336 0.043655
the 0.067143 -0.085567 0.093489 -0.031480 0.088760 -0.114515 -0.073492 0.104196 -0.044105 -0.164238 -0.042719 -0.009412 -0.038835 -0.167318 0.049335 -0.067349 0.033283 0.166929 0.117321 -0.153897 -0.050847 0.045416 0.099851 -0.194546 -0.206180 0.007511 -0.184491 0.004285 -0.063807 0.037658 -0.146942 0.199758 -0.028947 -0.044207 0.033893 -0.075525 -0.056374 0.039984 0.072331 0.118431 0.027936 0.151556 -0.080778 -0.072760 0.143183 -0.112461 -0.071912 0.093536 -0.071345 0.108646 0.046915 -0.206292 0.054857 0.190562 -0.098070 0.065259 0.075209 0.002478 0.103516 -0.120264 0.095588 -0.090496 -0.011566 -0.083615 0.059932 0.131636 0.018365 -0.034486 -0.002322 0.213339 -0.018544 -0.053778 -0.010300 -0.060720 -0.022378 0.003257 -0.074792 -0.016189 0.159581 -0.071515 0.079529 -0.002136 0.148633 0.063139 -0.022998 -0.003740 -0.184454 0.048515 -0.019371 0.049397 -0.060432 -0.011129 -0.041152 -0.031710 -0.074927 0.115143 -0.125259 0.068263 0.066361 0.093564
zero 0.234512 -0.121474 0.109792 0.170790 -0.045752 -0.071400 0.019738 0.158155 0.021511 -0.166053 -0.150880 -0.006694 -0.043727 0.093643 -0.097609 0.084147 -0.145428 -0.056139 0.028827 -0.093471 -0.044423 0.141048 -0.134931 0.221719 -0.016770 0.122846 -0.183889 -0.057073 -0.172779 0.309650 0.063630 0.149560 -0.095836 0.018020 -0.116167 -0.009918 0.111300 0.223023 0.157299 0.039720 -0.154298 0.043226 0.101416 -0.049199 0.048870 -0.045169 -0.156941 0.116830 0.173794 0.383597 -0.100274 -0.321975 -0.124036 0.023479 0.080748 0.046896 0.012085 -0.009434 -0.007503 -0.117414 0.179445 -0.094763 -0.151722 -0.060418 -0.226912 0.208830 -0.194392 0.042646 -0.008948 -0.085489 0.201878 0.262699 -0.038696 -0.048645 -0.217569 0.085613 -0.150584 -0.038554 0.088378 -0.171745 0.144290 0.032441 0.012133 0.043000 -0.189177 -0.082102 -0.081147 0.034110 0.085813 -0.005733 0.070215 -0.159489 -0.234402 0.187624 -0.011026 -0.060671 0.006454 -0.171911 -0.013589 0.156288
two 0.163218 -0.162495 0.107668 0.189965 0.021145 -0.059619 0.069969 0.227159 -0.104848 -0.211165 -0.097430 -0.036023 -0.167298 0.105771 -0.122580 0.073573 -0.120958 -0.052100 0.016527 -0.104523 -0.017716 0.130014 -0.138620 0.226781 -0.005818 0.040699 -0.133840 -0.113423 -0.079806 0.225052 0.038002 0.171414 -0.102285 -0.097279 -0.077814 -0.001372 0.128911 0.192054 0.172628 0.022217 -0.207471 -0.010214 0.075996 -0.160409 0.052846 -0.102724 -0.205455 0.078162 0.145891 0.244547 -0.100726 -0.285166 -0.109984 -0.010353 0.032096 0.055797 0.131779 -0.022093 -0.059075 -0.104025 0.065460 -0.079490 -0.158377 -0.059249 -0.161154 0.211828 -0.223842 0.000820 -0.020411 -0.022983 0.251198 0.203735 -0.023917 -0.043052 -0.172147 -0.031201 -0.103603 -0.040366 0.055646 -0.122588 0.192388 0.048838 0.070381 0.089683 -0.133047 -0.032403 -0.054312 -0.012779 0.056384 0.091236 0.061532 -0.155145 -0.129997 0.175614 0.017461 -0.028298 -0.058765 -0.081411 -0.072236 0.189735
of -0.001951 -0.015085 0.084471 0.040791 -0.009868 -0.168987 -0.067308 0.213923 -0.086569 -0.333235 -0.112222 -0.041374 -0.092798 -0.031317 -0.020553 0.014663 -0.007836 0.269409 -0.010085 0.015774 -0.012206 0.028835 -0.018442 -0.199499 -0.201803 -0.011843 -0.144433 0.049827 -0.070247 0.069916 -0.211196 0.241704 -0.012689 -0.004990 0.042760 -0.036157 -0.017820 0.074232 0.033176 0.157031 -0.000485 0.088111 -0.026371 -0.067741 0.142190 -0.244216 -0.179696 0.070536 -0.103531 -0.014362 0.032295 -0.175132 0.102773 0.071197 -0.059370 -0.053416 0.054228 0.029710 0.155319 -0.209673 -0.009975 -0.111585 0.075680 -0.038655 0.032363 0.098720 -0.017634 -0.089254 0.083416 0.113366 0.017834 -0.015202 -0.065379 -0.056012 -0.045715 0.062862 -0.014786 -0.054141 0.210261 -0.058731 0.136597 -0.082168 0.084586 0.167382 0.125936 0.000861 -0.224336 0.089994 -0.091892 0.073959 -0.052474 0.121917 -0.039723 -0.083876 -0.055097 0.118483 0.058847 0.065449 0.008484 0.234890
nine -0.010725 -0.135403 0.207712 0.113996 0.028221 0.044365 0.043820 0.104305 -0.110333 -0.261671 -0.077261 -0.002075 -0.166750 -0.076413 -0.108014 0.006192 -0.117700 0.062126 0.016570 0.084561 0.052687 0.008349 -0.009415 0.082214 -0.049914 0.029446 -0.044039 -0.002531 -0.153363 0.305136 0.108226 0.284866 -0.146100 -0.096593 -0.143897 -0.081603 0.109941 0.101400 0.013379 0.016425 -0.068308 0.108234 -0.026083 0.041452 0.007674 -0.048208 0.006072 0.063190 0.114552 0.341608 -0.104171 -0.352664 -0.104702 -0.098595 -0.077682 0.103937 0.191864 0.227647 -0.090261 -0.122575 0.013941 -0.036474 -0.165873 -0.061009 -0.293947 0.181088 -0.133013 0.011490 0.049958 0.025505 0.142349 0.173667 0.007782 -0.252370 -0.266985 0.203502 -0.124187 0.022042 0.086614 -0.199419 0.210526 0.029766 -0.033012 0.014718 -0.079054 -0.108224 -0.109321 0.254357 -0.059781 -0.009854 0.126696 -0.143998 -0.226338 0.204981 0.079745 0.005007 0.116490 -0.141156 0.091475 0.146884
three 0.064841 -0.148413 0.144022 0.312825 -0.092667 0.010864 0.065698 0.167000 -0.045551 -0.224670 -0.076799 0.060682 -0.110617 0.144019 -0.137788 0.122421 -0.090556 0.041400 0.000787 -0.052565 0.080413 0.098228 -0.094225 0.209475 0.047809 0.037572 -0.119885 -0.052269 -0.024237 0.240604 0.006669 0.164075 -0.135630 -0.103809 -0.033470 -0.043257 0.076736 0.138235 0.103390 0.066088 -0.138050 0.011918 0.055177 -0.155095 0.085061 -0.064821 -0.170126 0.081735 0.096985 0.314806 -0.071335 -0.299790 -0.144218 -0.091587 0.010849 0.125377 0.149589 0.064134 -0.110915 -0.131961 0.063371 -0.109666 -0.215024 -0.035662 -0.185111 0.283482 -0.158863 0.064007 -0.031212 -0.057628 0.309444 0.236663 -0.029492 -0.082301 -0.244228 0.105325 -0.105852 -0.000318 0.088838 -0.121898 0.221658 0.150066 0.016648 0.172529 -0.075737 -0.043057 -0.047071 0.130021 0.024525 0.095203 0.064450 -0.119124 -0.197285 0.153680 0.012265 -0.087556 0.010506 -0.114373 0.014617 0.200275
and 0.015529 -0.036379 0.049296 -0.062052 0.021820 0.026468 -0.111432 0.007336 0.023165 -0.286446 -0.120580 -0.040351 -0.006140 -0.005968 0.108602 -0.010103 0.012874 0.209411 0.063205 -0.191001 -0.003015 0.107911 -0.097780 -0.073129 -0.008813 0.000912 -0.072629 -0.019062 -0.140533 0.017184 -0.162806 0.163511 -0.091244 0.059017 0.026109 -0.108640 0.055647 0.027877 0.010004 0.092852 -0.006883 0.016780 -0.077931 -0.046273 -0.036286 -0.180693 0.008362 0.024238 -0.021224 -0.042070 0.001111 -0.285467 0.025983 0.075629 -0.098480 0.006279 0.202481 -0.001996 0.036146 -0.152560 0.089948 -0.135093 0.028528 -0.076588 0.087000 0.177579 -0.012121 -0.119873 0.035132 0.184795 0.014542 -0.095826 -0.126894 -0.032745 -0.012016 0.106000 0.036118 -0.035248 0.046313 0.033656 0.204436 0.064991 0.108424 -0.044001 0.101860 -0.051185 -0.077767 0.140233 -0.157882 0.072773 0.160326 -0.019374 -0.059815 -0.080882 -0.025106 0.058795 -0.026581 -0.009323 0.011035 0.084858
four 0.118349 -0.162800 0.110744 0.340172 -0.022487 -0.019767 0.042030 0.161410 -0.112582 -0.209458 -0.053698 0.087612 -0.092919 0.162207 -0.174956 0.063242 -0.102680 0.032908 0.039262 0.011627 0.081248 0.076689 -0.088509 0.224808 0.023220 -0.024466 -0.136222 -0.075133 -0.104798 0.215218 0.053896 0.157784 -0.125113 -0.135654 0.011165 -0.054542 0.111526 0.119698 0.140533 0.052979 -0.181148 0.005903 0.028846 -0.113431 0.035944 -0.042162 -0.141869 0.104678 0.072759 0.316858 -0.036328 -0.327109 -0.169543 -0.054346 0.029164 0.120060 0.149542 0.109558 -0.074448 -0.124541 0.077755 -0.109614 -0.187328 -0.005012 -0.221655 0.236480 -0.149209 0.067970 -0.093467 -0.053515 0.280123 0.238634 -0.038969 -0.128666 -0.254481 0.167051 -0.065434 0.039131 0.118049 -0.172411 0.266380 0.086710 0.003595 0.140948 -0.055426 -0.056563 0.008428 0.130907 -0.011732 0.089420 0.123265 -0.174317 -0.246915 0.199815 0.001433 -0.088947 -0.004834 -0.053181 -0.002317 0.202105
five 0.079032 -0.124585 0.097004 0.315801 -0.041387 -0.000491 0.026458 0.131614 -0.040007 -0.225021 -0.043700 0.074494 -0.064386 0.130155 -0.181631 0.042613 -0.068803 0.050538 -0.011700 -0.019512 0.089360 0.118171 -0.154423 0.148352 0.056862 -0.001226 -0.123316 -0.011918 -0.092314 0.259075 0.088867 0.207309 -0.078464 -0.136953 -0.087909 -0.170365 0.098693 0.200012 0.156852 0.075736 -0.145202 0.064268 -0.008703 -0.002906 0.052095 -0.057744 -0.142530 0.096010 0.076482 0.301905 -0.122250 -0.330067 -0.200123 -0.042094 0.042398 0.105123 0.053732 0.076568 -0.045556 -0.096323 0.109578 -0.082138 -0.228469 -0.039445 -0.191996 0.229464 -0.094697 0.076695 -0.045100 -0.109369 0.266649 0.241430 0.019100 -0.108580 -0.295336 0.129930 -0.094996 -0.013525 0.135974 -0.189913 0.200923 0.068997 0.027637 0.122714 -0.123436 -0.077794 -0.019687 0.166606 0.050702 0.042525 0.176070 -0.151441 -0.233004 0.194048 0.028686 -0.090625 0.019997 -0.130009 0.040090 0.236055

And I get the following output:

line 0 Read word one 
line 1 Read word 0.119589 
line 2 Read word the 
line 3 Read word zero 
line 4 Read word two 
line 5 Read word of 
line 6 Read word ne 
line 7 Read word three 
line 8 Read word and 
line 9 Read word four 
line 10 Read word five 

Clearly for line 1 the word read is incorrect (it is supposed to be in but instead it is a number). I am unable to understand why it is not working.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
amanwalia
  • 23
  • 1
  • 5

1 Answers1

3

You are facing a very subtle problem:

Your test for the end of the sequence of numbers is failure to parse a number. At the end of the first line, fscanf skips the \n and attempts to parse the next number. It consumes in because it is trying to match inf which is a valid number, namely +Infinity. The matching fails when it reads the space character, but fscanf cannot unget more than one character, so the next word read will be the number 0.119589. If your file does not contain trailing spaces, here is a way to fix the program:

#include <stdio.h>

#define HEIGHT 2
#define WIDTH  3

int main(void) {

#define MAX_WORDS 1000     /* Maximum words in a file */
#define MAX_VECTOR 500     /* Maximum length of vector */
#define WORD_LENGTH 100    /* Maximum length of a word */

    char words[MAX_WORDS][WORD_LENGTH];
    double numbers[MAX_WORDS][MAX_VECTOR] = { 0 };
    char ch;
    FILE *fp = fopen("test.txt", "r");
    int i, j;

    if (fp == NULL)
        return 1;

    for (i = 0; i < MAX_WORDS &&
                fscanf(fp, "%99s%c", words[i], &ch) == 2; i++) {
         printf("line %d Read word %s\n", i, words[i]);
         for (j = 0; j < MAX_VECTOR && 
                     ch != '\n' &&
                     fscanf(fp, "%lf%c", &numbers[i][j], &ch) == 2); j++) {
             continue;
         }
         /* ignore extra numbers on a line */
         while (ch != '\n' && (ch = getc(fp)) != EOF)
             continue;
     }
     return 0;
}

Also note that you are allocating 4.5 megabytes of automatic variables (aka on the stack). This may fail on many systems. Consider using global storage.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Thanks alot for the implementation, I was just wondering what is while statement doing. I tried running code without it and it is running fine. – amanwalia Jan 17 '16 at 09:41
  • @user3439142: as the comment says, the `while` loop is there in case there are more than `MAX_VECTOR` numbers on the line after the initial word. It consumes any extra characters until it sees the end of line or the end of file. – chqrlie Jan 17 '16 at 15:51