Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualrip-off

Posted 21 January 2013 - 05:48 AM

A couple of points:

  • Your code contains no error handling.

    • What if the file is missing?

    • What if the file is corrupt?

  • When dynamically allocating a structure, simply malloc(sizeof(some_structure)).

    • Your code tries to mallocate 3 times the size of a pointer to the structure, which makes no sense unless you are trying to dynamically allocate an array of 3 pointers to structures.

  • You are trying to strcpy() into an uninitialised pointer - tehMap->map_title.

  • It might be easier to use fscanf() than string tokenisation, at least for the file "header".

 

Consider building your program in smaller chunks. First write a function that will print the contents of a file to the screen. Test this function, ensure it works and handles any edge cases (file not found) gracefully.

 

Next write a function to parse the file format, but continue to print it to the screen. Test it with a minimal correct file. Harden the function against unexpected input (e.g. an empty file, not enough fields, non-numeric data in numeric field, etc, etc). Test it with more advanced files.

 

Next, create the structure to represent the data in memory. Test this by filling the structure in code, and print it to the screen.

 

As the final step, try to fill the structure from the data in the file.


#1rip-off

Posted 21 January 2013 - 05:30 AM

Your code contains no error handling. When dynamically allocating a structure, simply malloc(sizeof(some_structure)). Your code tries to mallocate 3 times the size of a pointer to the structure. You are trying to strcpy() into an uninitialised pointer - tehMap->map_title.

 

It might be easier to use fscanf() than string tokenisation, at least for the file "header".

 

Consider building your program in smaller chunks. First write a function that will print the contents of a file to the screen. Test this function, ensure it works and handles any edge cases (file not found) gracefully.

 

Next write a function to parse the file format, but continue to print it to the screen. Test it with a minimal correct file. Harden the function against unexpected input (e.g. an empty file, not enough fields, non-numeric data in numeric field, etc, etc). Test it with more advanced files.

 

Next, create the structure to represent the data in memory. Test this by filling the structure in code, and print it to the screen.

 

As the final step, try to fill the structure from the data in the file.


PARTNERS