Liste des Groupes | Revenir à cl c |
On Sat, 15 Jun 2024 15:36:22 -0400, DFS wrote:>
I want to read numbers in from a file, say:Others have (and will continue to) address this question.
>
47 185 99 74 202 118 78 203 264 207 19 17 34 167 148 54 297 271 118 245
294 188 140 134 251 188 236 160 48 189 228 94 74 27 168 275 144 245 178
108 152 197 125 185 63 272 239 60 242 56 4 235 244 144 69 195 32 4 54 79
193 282 173 267 8 40 241 152 285 119 259 136 15 83 21 78 55 259 137 297
15 141 232 259 285 300 153 16 4 207 95 197 188 267 164 195 7 104 47 291
>
>
This code:
1 opens the file
2 fscanf thru the file to count the number of data points
3 allocate memory
4 rewind and fscanf again to add the data to the int array
>
>
Any issues with this method?
Any 'better' way?Not so much "better", as "other".
ISTM that you waste an opportunity (and expose a common 'blind-spot')
in your first loop:
Here,while(fscanf(datafile, "%d", &j) != EOF){you discard a lot of work (done for you by fscanf() to determine the
N++;
}
value of each input number) just to be able to count the number of
numbers in your input. What if there were a way to put this (to you)
byproduct of fscanf() to use, and avoid using fscanf() entirely in
the second pass?
You /could/ create a temporary, binary, file, and write the fscanf()'ed
values to it as part of the first loop. Once the first loop completes,
you rewind this temporary file, and load your integer array by reading
the (now converted to native integer format) values from that file.
Still two passes, but using fscanf() in only one of those passes.
(BTW, the 'blind-spot' I mentioned is that we often forget that
we /can/ use temporary files to store intermediary results. Sometimes
we can manipulate a temporary file easier than we can manipulate
malloc()ed (or other) storage. )
Date | Sujet | # | Auteur | |
15 Jun 24 | Whaddaya think? | 96 | DFS | |
15 Jun 24 | Re: Whaddaya think? | 1 | Malcolm McLean | |
16 Jun 24 | Re: Whaddaya think? | 5 | Ben Bacarisse | |
16 Jun 24 | Re: Whaddaya think? | 2 | bart | |
16 Jun 24 | Re: Whaddaya think? | 1 | Ben Bacarisse | |
16 Jun 24 | Re: Whaddaya think? | 2 | DFS | |
17 Jun 24 | Re: Whaddaya think? | 1 | Ben Bacarisse | |
16 Jun 24 | Re: Whaddaya think? | 23 | Keith Thompson | |
16 Jun 24 | Re: Whaddaya think? | 22 | DFS | |
16 Jun 24 | Re: Whaddaya think? | 1 | Keith Thompson | |
17 Jun 24 | Re: Whaddaya think? | 19 | James Kuyper | |
17 Jun 24 | Re: Whaddaya think? | 1 | Tim Rentsch | |
17 Jun 24 | Re: Whaddaya think? | 13 | Kaz Kylheku | |
17 Jun 24 | Re: Whaddaya think? | 1 | Chris M. Thomasson | |
17 Jun 24 | Re: Whaddaya think? | 6 | DFS | |
17 Jun 24 | Re: Whaddaya think? | 5 | Richard Harnden | |
17 Jun 24 | Re: Whaddaya think? | 1 | David Brown | |
23 Jun 24 | Re: Whaddaya think? | 3 | Kaz Kylheku | |
23 Jun 24 | Re: Whaddaya think? | 1 | Keith Thompson | |
23 Jun 24 | Re: Whaddaya think? | 1 | Phil Carmody | |
18 Jun 24 | Re: Whaddaya think? | 5 | Tim Rentsch | |
18 Jun 24 | Re: Whaddaya think? | 4 | Keith Thompson | |
19 Jun 24 | Re: Whaddaya think? | 3 | Tim Rentsch | |
19 Jun 24 | Re: Whaddaya think? | 2 | Keith Thompson | |
19 Jun 24 | Re: Whaddaya think? | 1 | Kaz Kylheku | |
17 Jun 24 | Re: Whaddaya think? | 4 | DFS | |
17 Jun 24 | Re: Whaddaya think? | 3 | Ben Bacarisse | |
18 Jun 24 | Re: Whaddaya think? | 2 | Janis Papanagnou | |
18 Jun 24 | Re: Whaddaya think? | 1 | Keith Thompson | |
17 Jun 24 | Re: Whaddaya think? | 1 | James Kuyper | |
16 Jun 24 | Re: Whaddaya think? | 60 | Michael S | |
16 Jun 24 | Re: Whaddaya think? | 58 | Lawrence D'Oliveiro | |
16 Jun 24 | Re: Whaddaya think? | 57 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 8 | Keith Thompson | |
16 Jun 24 | Re: Whaddaya think? | 1 | Lawrence D'Oliveiro | |
16 Jun 24 | Re: Whaddaya think? | 6 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 5 | DFS | |
16 Jun 24 | Re: Whaddaya think? | 4 | David Brown | |
16 Jun 24 | Re: Whaddaya think? | 2 | bart | |
17 Jun 24 | Re: Whaddaya think? | 1 | David Brown | |
17 Jun 24 | Re: Whaddaya think? | 1 | DFS | |
16 Jun 24 | Re: Whaddaya think? | 45 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 44 | Keith Thompson | |
16 Jun 24 | Re: Whaddaya think? | 43 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 20 | Keith Thompson | |
16 Jun 24 | Re: Whaddaya think? | 19 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 2 | Malcolm McLean | |
16 Jun 24 | Re: Whaddaya think? | 1 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 16 | Keith Thompson | |
17 Jun 24 | Re: Whaddaya think? | 15 | Janis Papanagnou | |
17 Jun 24 | Re: Whaddaya think? | 12 | Keith Thompson | |
17 Jun 24 | Re: Whaddaya think? | 11 | Janis Papanagnou | |
17 Jun 24 | Re: Whaddaya think? | 5 | James Kuyper | |
18 Jun 24 | Re: Whaddaya think? | 1 | Keith Thompson | |
18 Jun 24 | Re: Whaddaya think? | 3 | Janis Papanagnou | |
18 Jun 24 | Re: Whaddaya think? | 1 | James Kuyper | |
18 Jun 24 | Re: Whaddaya think? | 1 | Keith Thompson | |
18 Jun 24 | Re: Whaddaya think? | 5 | Keith Thompson | |
18 Jun 24 | Re: Whaddaya think? | 1 | Janis Papanagnou | |
19 Jun 24 | Re: Whaddaya think? | 3 | Keith Thompson | |
19 Jun 24 | Re: Whaddaya think? | 2 | David Brown | |
19 Jun 24 | Re: Whaddaya think? | 1 | Keith Thompson | |
17 Jun 24 | Re: Whaddaya think? | 2 | James Kuyper | |
17 Jun 24 | Re: Whaddaya think? | 1 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 7 | Michael S | |
16 Jun 24 | Re: Whaddaya think? | 6 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 5 | Michael S | |
16 Jun 24 | Re: Whaddaya think? | 4 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 3 | Michael S | |
16 Jun 24 | Re: Whaddaya think? | 2 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 1 | Michael S | |
17 Jun 24 | Re: Whaddaya think? | 15 | Keith Thompson | |
17 Jun 24 | Re: Whaddaya think? | 11 | Tim Rentsch | |
17 Jun 24 | Re: Whaddaya think? | 8 | Janis Papanagnou | |
17 Jun 24 | Re: Whaddaya think? | 6 | DFS | |
17 Jun 24 | Re: Whaddaya think? | 2 | Chris M. Thomasson | |
18 Jun 24 | Re: Whaddaya think? | 1 | Keith Thompson | |
18 Jun 24 | Re: Whaddaya think? | 2 | Keith Thompson | |
18 Jun 24 | Re: Whaddaya think? | 1 | David Brown | |
18 Jun 24 | Re: Whaddaya think? | 1 | Janis Papanagnou | |
18 Jun 24 | Re: Whaddaya think? | 1 | Tim Rentsch | |
17 Jun 24 | Re: Whaddaya think? | 2 | James Kuyper | |
19 Jun 24 | Re: Whaddaya think? | 1 | Tim Rentsch | |
17 Jun 24 | Re: Whaddaya think? | 3 | Janis Papanagnou | |
17 Jun 24 | Re: Whaddaya think? | 2 | Keith Thompson | |
17 Jun 24 | Re: Whaddaya think? | 1 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 1 | Malcolm McLean | |
16 Jun 24 | Re: Whaddaya think? | 2 | Michael S | |
16 Jun 24 | Re: Whaddaya think? | 1 | Janis Papanagnou | |
16 Jun 24 | Re: Whaddaya think? | 1 | DFS | |
16 Jun 24 | Re: Whaddaya think? | 6 | Lew Pitcher | |
16 Jun 24 | Re: Whaddaya think? | 1 | Malcolm McLean | |
18 Jun 24 | Re: Whaddaya think? | 1 | Lawrence D'Oliveiro | |
25 Jun 24 | Re: Whaddaya think? | 3 | Lew Pitcher | |
25 Jun 24 | Re: Whaddaya think? | 2 | DFS | |
25 Jun 24 | Re: Whaddaya think? | 1 | Lew Pitcher |
Les messages affichés proviennent d'usenet.