[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [c] Læse fil med trekolonner



On Sat, 29 May 2004 21:10:13 +0000 (UTC)
Christian Nielsen <sslug@sslug> wrote:
>>> ...
> > Donald:
    ...
> >    3) atof(3), en wrapper for strtof(3)
> 
> Jeg vil helst undgå c++ commandoer.

Det kan jeg godt forstå.


> Jeg har prøvet flg. men der printes intet. Hvad kan der være galt?
> 
>   FILE *fp;
>   char *text;
>   float MW[MAX_COMPONENTS];
>   float w[MAX_COMPONENTS];
>   float Tc[MAX_COMPONENTS];
>   float Pc[MAX_COMPONENTS];
>   int i=0;
>   fp = fopen(argv[1],"r");
>   if (!fp) {
>     sprintf(errormsg, "Kan ikke åbne fil ved navn %s",argv[1]);
>     error(errormsg,2);
>   }
>   for (i=0;i<MAX_COMPONENTS;i++) {
>     while (fscanf(fp,"%s\t%f\t%f\t%f\t\n",*text,&MW[i],&w[i],&Tc[i],&Pc[i])) {
>       printf("Molekyle:%s, MW:%5.2f Omega:%5.2f Tc:%5.2f PPc:%5.2f\n",
                *text,MW[i],w[i],Tc[i],Pc[i]);
>     }
>   }

Jeg måtte lige ombryde koden (pga mail-tvangsformatering?)

> og her er min inputfil:
> 
> CH4 72.15 0.252 469.7 33.7
> C2H6  86.177  0.301 507.6 30.25
> C6  100 -1  -1  -1

Jeg går ud fra, at der er tab mellem felterne i input filen. I så
fald er scanf i orden, næsten - men din variabel char *text er
IKKE i orden.

Tekst variable skal erklæres således:
typisk

#define BUFFERSIZE 890

    char line[BUFFERSIZE];
    char text[180];
    int returnval;
    double talval;

    fgets(line, BUFFERSIZE-1, fp);
    returnval = sscanf(line, "%s\t%f\t\n", text, &talval);

    if (returnval < 2) {
        show_debug();
        error();
        gohome();
    }


Altså groft forsimplet!-)

Budskabet er altså 1) HUSK at text er adressen (pointer) til
text[80] og 2) lad være med at lægge noget i en pointer, hvis
adresse du ikke har sat (in casu erklærer du char *text; som er en
adressevariable, pointer, men du initialiserer ikke pointeren til
at pege på en buffer.)

Der er et buffer overflow problem i koden, som jeg demonstrerer.

Derfor bør man anvende %80s, som i flg.

    rv = sscanf(line,"%80s\t  ...", text, ...);

Husk at text er adresse på første byte af array text[888]; derfor
skal du ikke tage adressen på den.

   %80s giver range checking så du undgår overflow.

   Du kan selvf. også bruge %42s hvis det er nok med 42.







-- 
donald_j_axel snabela get2net.dk -- http://d-axel.dk/


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *