Parsing a formatted text file into an array of structs

This is a discussion on Parsing a formatted text file into an array of structs within the C Programming forums, part of the General Programming Boards category; Hi, I have a text file that looks like this: zoo.txt Code: 1 0 0 90 3 1 1 1 ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    Parsing a formatted text file into an array of structs

    Hi, I have a text file that looks like this:

    zoo.txt
    Code:
    1 0 0 90 3 1
    1 1 0 62 1 2
    1 2 0 64 1 2
    1 3 0 65 1 2
    1 4 0 61 1 2
    1 5 0 62 1 2
    1 6 0 58 1 2
    1 7 1 57 1 1
    The numbers are separated by spaces and newline characters, and I would like to read it into an array of identical structs:

    Code:
    	struct _animal {
    		int zoo, tag, pen, ability, diet, age;
    	};
    
    	struct _animal zoo [numberOfAnimals];
    What file functions should I use?

    I tried fscanf but I have to use a lot of repetition, and I cant use fread because it's a text file not a binary file

    Also, how do I find out the value of numberOfAnimals, besides the method below

    Code:
    	FILE *inputfile;
    
    	inputFile = fopen ("zoo.txt", "r");
    	
    	while ( (c = getc (inputFile) ) != EOF) {
    		numberOfAnimals+= (c == '\n');
    	}
    
    	fclose (inputFile)
    
    	printf ("%d", numberOfAnimals)

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by onus1111 View Post
    The numbers are separated by spaces and newline characters, and I would like to read it into an array of identical structs:

    What file functions should I use?
    I would use fgets() to read the file line by line. One line corresponds to one struct so I would use sscanf() to extract the data from the text buffer filled by fgets().

    I tried fscanf but I have to use a lot of repetition
    That my friend is why the gods of C gave us loops.

    Also, how do I find out the value of numberOfAnimals, besides the method below
    There are multiple methods of making the actual number of animals (i.e. lines) in the file unimportant... You can build a linked list. You can set up an array of structs with malloc() and then expand it with realloc() as needed. You can go "brute force" and create an array bigger than you'll ever need. Etc.
    Last edited by CommonTater; 06-18-2011 at 11:13 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    thanks for the great help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing XML file into C structs
    By jaaaaakke in forum C Programming
    Replies: 7
    Last Post: 04-30-2009, 09:40 PM
  2. How to extract (formatted) text from a PDF file?
    By HeinzB in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2008, 03:46 AM
  3. Reading a formatted text file
    By inspiro in forum C Programming
    Replies: 2
    Last Post: 06-25-2008, 08:39 PM
  4. formatted text from file.?
    By Gugge in forum C Programming
    Replies: 2
    Last Post: 03-08-2002, 05:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21