c - Reading from a text file and plotting using GNU libplot -


i've made program reads file, , alphabetically sorts names contained within file. file contains planet names, mass, size, color, primary body, positions x,y,z , velocity x,y,z. i'm trying plot each planet using file, i'm unsure how go doing that. i'm using gnu libplot plot planets. i'm thinking need use loop , fscanfs x , y coordinates (can omit z now) plot each planet.

here's current code:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define names 20  struct planets{ //sets initial characters , doubles place body.   char primarybody[names];   char name[names];   char color[names];   double mass;   double size;   double posx, posy, posz;   double velx, vely, velz; };  struct planets body[100];  int readdata(file* fd){ //this reads file solarsystem.txt , fills array  int current = 0; char line[201]; char cmpline[2] = "#";  do{     fgets(line, 200, fd); // makes sure number of bytes written dont overflow   character. }while((strlen(line)<2)||(strncmp(line, cmpline, 1))==0); sscanf(line, "%s %lf %s %lf %s", body[current].name, &body[current].mass, body[current].color, &body[current].size, body[current].primarybody); fscanf(fd, "%lf %lf %lf", &body[current].posx, &body[current].posy, &body[current].posz); fscanf(fd, "%lf %lf %lf", &body[current].velx, &body[current].vely, &body[current].velz); current++;  while(!feof(fd)){//runs loop until end of file met.   fscanf(fd, "%s %lf %s %lf %s", body[current].name, &body[current].mass, body[current].color, &body[current].size, body[current].primarybody);   fscanf(fd, "%lf %lf %lf", &body[current].posx, &body[current].posy, &body[current].posz);   fscanf(fd, "%lf %lf %lf", &body[current].velx, &body[current].vely, &body[current].velz);   current++; }   return current; }  void sortnames(int planetnames){ //traditional bubble sorting code, slight  modification.  struct planets temp[1];  for(int i=0; i<(planetnames);i++){   for(int j=0; j<(planetnames-1); j++){     if (strcmp(body[j].name, body[j+1].name)>0){       temp[0] = body[j];       body[j] = body[j+1];       body[j+1] = temp[0];     }   } }  void printbodies(int sortnames){// code print out sorted file.       int planetnames = sortnames;    printf("name     mass       color  size  primary body\n");   printf("_____________________________________________\n");    for(int i=0; i<(planetnames); i++){     printf("%-8s %1.4e %-6s %.2lf  %s\n", body[i].name, body[i].mass, body[i].color, body[i].size, body[i].primarybody);   } }  int main(int argc, char *argv[]){/*runs different structs, , if file not available, tell user.*/   file *fd;   fd = fopen (argv[1], "r");    if(!fd){       printf("file not available, or user did not type file name.\n");       return 1;   }   int planetnames=readdata(fd);   sortnames(planetnames);   printbodies(planetnames);   findmax(planetnames);    return 0; } 


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -