[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 tre kolonner



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. 
Tallene læses korreket men teksten volder problemer. Jeg prøvet at lægge 
dem i et 2-dimensionalt array.

Chr.

#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;
  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("flashinput.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:%c, 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


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