[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 Sun, 30 May 2004 09:59:57 +0000 (UTC)
Christian Nielsen <sslug@sslug> wrote:

> Mange tak for dit svar. Det har været en stor hjælp. Det virker næsten nu. 
> Det eneste problem er, at jeg får ikke læst strengen ind i mit array. 

Selv Tak!  Nå til sagen. Du fik mig til at falde med begge
ben i dit eksempel her! Det er aldrig helt ligetil, når man ikke
sidder og arbejder med det selv.

>   while (fgets(line, BUFFERSIZE-1, fp)) {
>     returnval = sscanf(line, 
>        "%s\t%f\t%f\t%f\t%f\n",&text[i],&MW[i],&w[i],&Tc[i],&Pc[i]);
>     if (returnval < 2) {
>        error("Hov",0);
>     }

Det er igen dit text-array, som forvirrer. Navnet på et character
array er simpelthen en pointer til den første byte. Så der skal ikke 
være en adresseoperator (&) foran!

Her er den færdige version (bortset fra navnet på inputfilen):

============>8===============klip-her==================================


#include <stdio.h>
#include <stdlib.h>

#define MAX_COMPONENTS 10
#define BUFFERSIZE 890


void error(char *message, int exitcode)
{
  fprintf(stderr,"%s\n",message);
  exit(exitcode);
}

int main(int argc, char *argv[])
{

  int Components = 0;  // so far unused, ignore warning. 
  char errormsg[80];
  FILE *fp;
  float MW[MAX_COMPONENTS];
  float w[MAX_COMPONENTS];
  float Tc[MAX_COMPONENTS];
  float Pc[MAX_COMPONENTS];
  int i=0;
  char line[BUFFERSIZE];
  char text[MAX_COMPONENTS][180];
  int returnval;

  fp = fopen("Molecul-inp.txt","r");
  if (!fp) {
    sprintf(errormsg, "Kan ikke åbne fil ved navn %s",argv[1]);
    error(errormsg,2);
  }

  while (fgets(line, BUFFERSIZE-1, fp)) {
    returnval = sscanf(line, 
      "%s\t%f\t%f\t%f\t%f\n",text[i],&MW[i],&w[i],&Tc[i],&Pc[i]);
    if (returnval < 2) {
      error("Hov",0);
    }
    printf("Molekyle:%s, MW:%5.2f Omega:%5.2f Tc:%5.2f Pc:%5.2f\n",
      text[i],MW[i],w[i],Tc[i],Pc[i]);
    i++;
  }
  return 0;
}

/*
 *

output:
Molekyle: , MW:72.15 Omega: 0.25 Tc:469.70 Pc:33.70
Molekyle:Ô, MW:86.18 Omega: 0.30 Tc:507.60 Pc:30.25
Molekyle:, MW:100.00 Omega:-1.00 Tc:-1.00 Pc:-1.00

input:
CH4     72.15   0.252   469.7   33.7
C2H6    86.177  0.301   507.6   30.25
C6      100     -1      -1      -1

 *
 */








-- 
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] *