Storing in structure

This is a discussion on Storing in structure within the C Programming forums, part of the General Programming Boards category; Hi everyone, OK, so I want to read data in from a tab delimited file and then store part of ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    12

    Storing in structure

    Hi everyone,
    OK, so I want to read data in from a tab delimited file and then store part of it in a structure that i've built. The tab delimited file basically consists of 29 different lines of :

    string <tab> string <tab> integer

    It is the strings I want to read, compare and then store (I just want to store them at the moment in the node_data.name part.
    The code will succesfully read the data nad print it straight to screen, but storing it and ten printing it is a different matter!

    The code I've produced follows:
    Code:
    #include <stdio.h>
    
    void main(void)
    {
    
    struct node_data	/*This is the data structure that will contain*/{		/*the information regarding each node*/
    char name;
    int code_name;
    int relax;
    int value;
    int previous_nodes[100];
    };
    
    struct node_data nodes[100];	     
    int i;
    char *sub_string;
    FILE *stream;
    
    
    if( (stream = fopen( "ukcities.txt", "r" )) != NULL )
    {
       if( fgets(nodes[0].name, 100, stream ) == NULL)
       {
            printf( "fgets error\n" );
       }
       else
       {
          printf("%s\n", strtok(nodes[0].name, "	"));
    	
          while ( (sub_string=strtok(NULL, "	")) != NULL)
    	{
    	printf("%s\n", sub_string);
    	}
    	
    	for(i=1;i<100;i++)
    	{
    	if( fgets( nodes[i].name, 100, stream ) == NULL)
    	break;
                    else
    	{
    	printf("%s\n", strtok(nodes[i].name, "	"));
    
    	while ( (sub_string=strtok(NULL, "	")) != NULL)
      	{
    	printf("%s\n", sub_string);
    	}
          }
    }
    fclose(stream);
    }
    }
    }
    The program compiles with the following errrors:

    assignment makes pointer from integer without cast for different lines.

    However, the program does compile succesfully.

    I've been looking at trying different things for ages now, can someone please help me!!

    Thankyou very much.

    Ben

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Stop using void main. It's wrong. Read the FAQ as to why.
    2) This is not a string:
    Code:
    struct node_data {
    char name;
    int code_name;
    int relax;
    int value;
    int previous_nodes[100];
    };
    It is one single char. Not more than one, just one. Either use a pointer to a char, and malloc / free memory for it, or use an array.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 10:28 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Storing in structure
    By bennyboy in forum C Programming
    Replies: 2
    Last Post: 03-02-2005, 07:18 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. Storing Data in a Structure
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2001, 06:43 AM

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