Re: Whaddaya think?

Liste des GroupesRevenir à l c 
Sujet : Re: Whaddaya think?
De : nospam (at) *nospam* dfs.com (DFS)
Groupes : comp.lang.c
Date : 25. Jun 2024, 20:09:23
Autres entêtes
Message-ID : <667b07d3$0$2873017$882e4bbb@reader.netnews.com>
References : 1 2 3
User-Agent : Betterbird (Windows)
On 6/25/2024 1:37 PM, Lew Pitcher wrote:
On Sun, 16 Jun 2024 15:52:16 +0000, Lew Pitcher wrote:
 
On Sat, 15 Jun 2024 15:36:22 -0400, DFS wrote:
>
I want to read numbers in from a file, say:
>
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
>
>
[snip]
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.
[snip]
 For what it's worth, here's an example of what I suggest:
 /*
The following code provides two examples of the approach I suggested.
 Example 1: while counting input numbers, write temp file with int values
   malloc() a buffer big enough for that count of int values
   fread() the temp file into the malloc()'ed buffer
   Note: conformant to ISO Standard C.
 Example 2: while counting input numbers, write temp file with int values
   mmap() the temp file, starting at the beginning, and sized to
   include all the int values in the file.
   Note: conformant to POSIX C extensions to ISO Standard C.
 Note: compile with -DUSE_MMAP to obtain mmap() variant, otherwise
       this will compile the malloc()/fread() variant
*/
 #include <stdio.h>
#include <stdlib.h>
 #ifdef USE_MMAP
   #include <sys/mman.h>
   #define BANNER "Example of array loading using mmap()"
   #define FREEALLOC(x)
#else
   #define BANNER "Example of array loading using malloc() and fread()"
   #define FREEALLOC(x) free((x))
#endif
 static int *LoadIntArray(FILE *fp, size_t *Count);
 int main(void)
{
   int status = EXIT_FAILURE, *array;
   size_t count;
    puts(BANNER);
    if ((array = LoadIntArray(stdin,&count)))
   {
     printf("%zu elements loaded\n",count);
     for (size_t index = 0; index < count; ++index)
       printf("array[%3zu] == %d\n",index,array[index]);
      FREEALLOC(array); /* if necessary, free() the malloc()'ed array */
     status = EXIT_SUCCESS;
   }
   return status;
}
 static int *LoadIntArray(FILE *fp,size_t *Count)
{
   FILE *tmp;
   int *array = NULL;
   size_t count = 0;
    if ((tmp = tmpfile()))
   {
     int buffer;
      for (count = 0; fscanf(fp,"%d",&buffer) == 1; ++count)
       fwrite(&buffer,sizeof buffer, 1,tmp);
      if (count)
     {
#ifdef USE_MMAP
       /*
       ** USE mmap() to map temp_file data into process memory
       */
       array = mmap(NULL,
                    count * sizeof *array,
                    PROT_READ,MAP_PRIVATE,
                    fileno(tmp),
                    0);
       if (array == MAP_FAILED)
       {
array = NULL;
fprintf(stderr,"FAIL: Cannot mmap %zu element array\n",count);
       }
#else
       /*
       ** USE malloc() to reserve a big enough heap-space buffer,
       ** then fread() the temp_file data into that buffer
       */
       if ((array = malloc(count * sizeof *array)))
       {
rewind(tmp);
if (fread(array,sizeof *array,count,tmp) != count)
{
  free(array);
  array = NULL;
  fprintf(stderr,"FAIL: Cannot load %zu element array\n",count);
}
       }
       else fprintf(stderr,"FAIL: Cant malloc() %zu element array\n",count);
#endif
     }
     fclose(tmp);
    }
   else fprintf(stderr,"FAIL: Cannot allocate temporary work file\n");
    *Count = count; /* byproduct value that caller might find useful */
   return array; /* either NULL (on failure) or pointer to array */
}
 
$ gcc -Wall LewPitcher_readnums.c -o lprn
$ (no compile errors)
$ ./lprn nums.txt
Example of array loading using malloc() and fread()
(then it just hung)
$ gcc -Wall LewPitcher_readnums.c -o lprn -DUSE_MMAP
$ (no compile errors)
$ ./lprn nums.txt
Example of array loading using mmap()
(then it just hung)
Am I supposed to hardcode the filename in there somewhere?

Date Sujet#  Auteur
15 Jun 24 * Whaddaya think?96DFS
15 Jun 24 +- Re: Whaddaya think?1Malcolm McLean
16 Jun 24 +* Re: Whaddaya think?5Ben Bacarisse
16 Jun 24 i+* Re: Whaddaya think?2bart
16 Jun 24 ii`- Re: Whaddaya think?1Ben Bacarisse
16 Jun 24 i`* Re: Whaddaya think?2DFS
17 Jun 24 i `- Re: Whaddaya think?1Ben Bacarisse
16 Jun 24 +* Re: Whaddaya think?23Keith Thompson
16 Jun 24 i`* Re: Whaddaya think?22DFS
16 Jun 24 i +- Re: Whaddaya think?1Keith Thompson
17 Jun 24 i +* Re: Whaddaya think?19James Kuyper
17 Jun 24 i i+- Re: Whaddaya think?1Tim Rentsch
17 Jun 24 i i+* Re: Whaddaya think?13Kaz Kylheku
17 Jun 24 i ii+- Re: Whaddaya think?1Chris M. Thomasson
17 Jun 24 i ii+* Re: Whaddaya think?6DFS
17 Jun 24 i iii`* Re: Whaddaya think?5Richard Harnden
17 Jun 24 i iii +- Re: Whaddaya think?1David Brown
23 Jun 24 i iii `* Re: Whaddaya think?3Kaz Kylheku
23 Jun 24 i iii  +- Re: Whaddaya think?1Keith Thompson
23 Jun 24 i iii  `- Re: Whaddaya think?1Phil Carmody
18 Jun 24 i ii`* Re: Whaddaya think?5Tim Rentsch
18 Jun 24 i ii `* Re: Whaddaya think?4Keith Thompson
19 Jun 24 i ii  `* Re: Whaddaya think?3Tim Rentsch
19 Jun 24 i ii   `* Re: Whaddaya think?2Keith Thompson
19 Jun 24 i ii    `- Re: Whaddaya think?1Kaz Kylheku
17 Jun 24 i i`* Re: Whaddaya think?4DFS
17 Jun 24 i i `* Re: Whaddaya think?3Ben Bacarisse
18 Jun 24 i i  `* Re: Whaddaya think?2Janis Papanagnou
18 Jun 24 i i   `- Re: Whaddaya think?1Keith Thompson
17 Jun 24 i `- Re: Whaddaya think?1James Kuyper
16 Jun 24 +* Re: Whaddaya think?60Michael S
16 Jun 24 i+* Re: Whaddaya think?58Lawrence D'Oliveiro
16 Jun 24 ii`* Re: Whaddaya think?57Janis Papanagnou
16 Jun 24 ii +* Re: Whaddaya think?8Keith Thompson
16 Jun 24 ii i+- Re: Whaddaya think?1Lawrence D'Oliveiro
16 Jun 24 ii i`* Re: Whaddaya think?6Janis Papanagnou
16 Jun 24 ii i `* Re: Whaddaya think?5DFS
16 Jun 24 ii i  `* Re: Whaddaya think?4David Brown
16 Jun 24 ii i   +* Re: Whaddaya think?2bart
17 Jun 24 ii i   i`- Re: Whaddaya think?1David Brown
17 Jun 24 ii i   `- Re: Whaddaya think?1DFS
16 Jun 24 ii +* Re: Whaddaya think?45Janis Papanagnou
16 Jun 24 ii i`* Re: Whaddaya think?44Keith Thompson
16 Jun 24 ii i `* Re: Whaddaya think?43Janis Papanagnou
16 Jun 24 ii i  +* Re: Whaddaya think?20Keith Thompson
16 Jun 24 ii i  i`* Re: Whaddaya think?19Janis Papanagnou
16 Jun 24 ii i  i +* Re: Whaddaya think?2Malcolm McLean
16 Jun 24 ii i  i i`- Re: Whaddaya think?1Janis Papanagnou
16 Jun 24 ii i  i `* Re: Whaddaya think?16Keith Thompson
17 Jun 24 ii i  i  `* Re: Whaddaya think?15Janis Papanagnou
17 Jun 24 ii i  i   +* Re: Whaddaya think?12Keith Thompson
17 Jun 24 ii i  i   i`* Re: Whaddaya think?11Janis Papanagnou
17 Jun 24 ii i  i   i +* Re: Whaddaya think?5James Kuyper
18 Jun 24 ii i  i   i i+- Re: Whaddaya think?1Keith Thompson
18 Jun 24 ii i  i   i i`* Re: Whaddaya think?3Janis Papanagnou
18 Jun 24 ii i  i   i i +- Re: Whaddaya think?1James Kuyper
18 Jun 24 ii i  i   i i `- Re: Whaddaya think?1Keith Thompson
18 Jun 24 ii i  i   i `* Re: Whaddaya think?5Keith Thompson
18 Jun 24 ii i  i   i  +- Re: Whaddaya think?1Janis Papanagnou
19 Jun 24 ii i  i   i  `* Re: Whaddaya think?3Keith Thompson
19 Jun 24 ii i  i   i   `* Re: Whaddaya think?2David Brown
19 Jun 24 ii i  i   i    `- Re: Whaddaya think?1Keith Thompson
17 Jun 24 ii i  i   `* Re: Whaddaya think?2James Kuyper
17 Jun 24 ii i  i    `- Re: Whaddaya think?1Janis Papanagnou
16 Jun 24 ii i  +* Re: Whaddaya think?7Michael S
16 Jun 24 ii i  i`* Re: Whaddaya think?6Janis Papanagnou
16 Jun 24 ii i  i `* Re: Whaddaya think?5Michael S
16 Jun 24 ii i  i  `* Re: Whaddaya think?4Janis Papanagnou
16 Jun 24 ii i  i   `* Re: Whaddaya think?3Michael S
16 Jun 24 ii i  i    `* Re: Whaddaya think?2Janis Papanagnou
16 Jun 24 ii i  i     `- Re: Whaddaya think?1Michael S
17 Jun 24 ii i  `* Re: Whaddaya think?15Keith Thompson
17 Jun 24 ii i   +* Re: Whaddaya think?11Tim Rentsch
17 Jun 24 ii i   i+* Re: Whaddaya think?8Janis Papanagnou
17 Jun 24 ii i   ii+* Re: Whaddaya think?6DFS
17 Jun 24 ii i   iii+* Re: Whaddaya think?2Chris M. Thomasson
18 Jun 24 ii i   iiii`- Re: Whaddaya think?1Keith Thompson
18 Jun 24 ii i   iii+* Re: Whaddaya think?2Keith Thompson
18 Jun 24 ii i   iiii`- Re: Whaddaya think?1David Brown
18 Jun 24 ii i   iii`- Re: Whaddaya think?1Janis Papanagnou
18 Jun 24 ii i   ii`- Re: Whaddaya think?1Tim Rentsch
17 Jun 24 ii i   i`* Re: Whaddaya think?2James Kuyper
19 Jun 24 ii i   i `- Re: Whaddaya think?1Tim Rentsch
17 Jun 24 ii i   `* Re: Whaddaya think?3Janis Papanagnou
17 Jun 24 ii i    `* Re: Whaddaya think?2Keith Thompson
17 Jun 24 ii i     `- Re: Whaddaya think?1Janis Papanagnou
16 Jun 24 ii +- Re: Whaddaya think?1Malcolm McLean
16 Jun 24 ii `* Re: Whaddaya think?2Michael S
16 Jun 24 ii  `- Re: Whaddaya think?1Janis Papanagnou
16 Jun 24 i`- Re: Whaddaya think?1DFS
16 Jun 24 `* Re: Whaddaya think?6Lew Pitcher
16 Jun 24  +- Re: Whaddaya think?1Malcolm McLean
18 Jun 24  +- Re: Whaddaya think?1Lawrence D'Oliveiro
25 Jun 24  `* Re: Whaddaya think?3Lew Pitcher
25 Jun 24   `* Re: Whaddaya think?2DFS
25 Jun 24    `- Re: Whaddaya think?1Lew Pitcher

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal