C, input from file to 2d array -


can 1 explain me how make c program read input file according following scenario:

12 2-4,7,9; 1,4,11-12; 1,4,10,12; 1,4-8,10-12; 1,8; 1,3-6,8,10-12; 1,3,5-6,8,11; 1,8,10-12; 1-8; ; 2; 2-4,7-10,12; 

the first number (on first line) describes size grid should be, in case 12x12 grid. following lines describe how many cells occupied on each row of grid. example, in first row cells 2 4 , 7 , 9 occupied; in second row, cells 1, 4 , 11 12 occupied , on.

right have code, not solving problem ...

#include <stdio.h> #include <stdlib.h>  void main() {        char content[3000];      int value;     file *ptr_file = fopen("data.txt", "r");     if(!ptr_file)         return 1;     int j;      while(fgets(content, 3000, ptr_file)!=null){         printf("%s", content);         value = atoi(content);             for(j=0; j<3000; j++){             value = content[j];             printf("%i", value);         }     }     return 0; } 

console throws bunch of random numbers ...

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>  enum { eod = -1, eol = -2, err = -3, off = 0, on = 1 };//end of data, end of line  int getvalue(file *fp){     static int on_yield = off, prev, end;      if(on_yield == on && prev < end){         return ++prev;     }     on_yield = off;      int n, ch = fgetc(fp);      if(ch == eof)         return eod;     else if(ch == ';')         return eol;     else if(ch == ',' || ch == '\n')         return getvalue(fp);//or change using loop     else if(isdigit(ch)){         ungetc(ch, fp);         fscanf(fp, "%d", &n);         return prev=n;     } else if(ch == '-'){         on_yield = on;         fscanf(fp, "%d", &n);         end = n;         return getvalue(fp);     }     fprintf(stderr, "(%c) invalid format in file\n", ch);     return err; }  int main(void){     file *ptr_file = fopen("data.txt", "r");     if(!ptr_file){         perror("fopen");         return 1;     }      int size;     fscanf(ptr_file, "%d", &size);//check omitted     char (*cell)[size] = malloc(size * sizeof(*cell));     memset(&cell[0][0], ' ', size*size);      int r = 0, c, value;     while((value=getvalue(ptr_file))!=eod){         if(value == eol){             ++r;             continue;         }         if(value > 0)             cell[r][value-1] = '*';     }     fclose(ptr_file);      for(r = 0; r < size; ++r){         for(c = 0; c < size; ++c){             putchar(cell[r][c]);         }         putchar('\n');     }     free(cell);      return 0; } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -