Jump to content

Array Struct Example

- - - - -

  • Please log in to reply
9 replies to this topic

#1
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
I am doing this to practice on so I can get better understanding on this - but once completed with the coding part - an error came up which I am not famailar with, and I did some search on this error but I have come to no solution, can you?


#include <stdlib.h>

#include <string.h>

#include <stdio.h>


int main()

{

	typedef struct 

	{

		int n_adults;

		int n_kids;

		char day[10];

		char weather[10];

	} daydata;


	daydata record[30];

	FILE*f;

	char line[121];


	char*item;

	int reccount = 0;

	int k;


	/*Open file*/

	

	f = fopen("newstuff.txt", "r");

	

	/*read file line by line*/


	while(fgets(line,120,f))

	{

	printf("%s", line);

	

	item = strtok(line, " ");

	record[reccount].n_adults = atoi(item);

	

	item = strtok(NULL, " ");

	record[reccount].n_kids = atoi(item);


	item = strtok(NULL, " ");

	strcopy(record[reccount].day, item);

	

	item = strtok(NULL, "\n");

	strcpy(record[reccount].weather, item);

	

	printf("%s\n", record[reccount].day);

	reccount++;

	}

	

	/* close file */

	

	fclose(f);

	

	/*loop through and report data*/

	

	printf("Weather Record\n");

	for(k = 0; k < reccount; k++)

	{

		printf("it is %s\n", record[k].weather);

	}



}

	



Error as below:

Quote

*****@ubuntu:~/week10$ gcc -o ArrayStruct1 ArrayStruct1.c
/tmp/ccLtl4bl.o: In function `main':
ArrayStruct1.c:(.text+0x15e): undefined reference to `strcopy'
collect2: ld returned 1 exit status


#2
Zer033

Zer033

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
You're gonna hate yourself, but it is just a simple typo.

    item = strtok(NULL, " ");
    strcopy(record[reccount].day, item);
strcpy not strcopy.

That error just means it can't find the function you're trying to use, it is not defined. So the usual cause can either be a typo like it is here or including the wrong headers.

#3
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
Ohhhhh, how embarrassing was that! :o
it worked now however when I ran after amending the code - this is what I got - the text is correct from the text file but 'Segemantion fault'!?

Quote

5 0 Wednesday Sunny
Wednesday
6 2 Thursday Wet
Thursday
4 3 Friday Cloudy
Friday

Segmentation fault


Zer033 said:

You're gonna hate yourself, but it is just a simple typo.


    item = strtok(NULL, " ");

    strcopy(record[reccount].day, item);

strcpy not strcopy.

That error just means it can't find the function you're trying to use, it is not defined. So the usual cause can either be a typo like it is here or including the wrong headers.


#4
Zer033

Zer033

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
The error has to do with memory allocation problems with your pointers and arrays. I don't really know C all that well, I program in C++ so I can't immediately tell the problem, and I have to go for now, but later on I'll see if I can find where the problem is.

#5
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
I had a good look at the code and now the segmentation fault for this code has to do something with this part of the code


/*loop through and report data*/

	

	printf("Weather Record\n");

	for(i = 0; i < reccount; i++)

	{

		printf("it is %s\n\n", record[i].weather);

	}


latest modified code


#include <stdlib.h>

#include <string.h>

#include <stdio.h>


int main()

{

	typedef struct 

	{

		int n_adults;

		int n_kids;

		char day[10];

		char weather[10];

	} daydata;


	daydata record[30];

	FILE*f;

	char line[121];


	char*item;

	int reccount = 0;

	int k;


	/*Open file*/

	

	f = fopen("newstuff.txt", "r");

	

	/*read file line by line*/


	while(fgets(line,121,f))

	{

	printf("%s", line);

	

	item = strtok(line, " ");

	record[reccount].n_adults = atoi(item);

	

	item = strtok(NULL, " ");

	record[reccount].n_kids = atoi(item);


	item = strtok(NULL, " ");

	strcpy(record[reccount].day, item);

	

	item = strtok(NULL, "\n");

	strcpy(record[reccount].weather, item);

	

	//printf("%s\n", record[reccount].day);

	reccount++;

	}

	

	/* close file */

	

	fclose(f);

	

	/*loop through and report data*/

	

	printf("Weather Record\n");

	for(k = 0; k < reccount; k++)

	{

		printf("it is %s\n\n", record[k].weather, item);

	}



}



#6
Zer033

Zer033

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
I modified your code and I think I got what you were looking for, but I'm not sure. As I said I haven't really programmed in C and I haven't used these functions before so just went to look at quick references to see what they're supposed to do. I changed the adults and kids to chars as well because I wasn't sure how to use atoi and it kept hanging the program when I tried to use it as you were. I'm not entirely sure what your segment fault was, but I think it has something to do with using fgets in the while loop. As far as I could tell fgets returns a char pointer, not any kind of evaluation return value. Maybe you have to use feof(FILE) in the while loop, I didn't try that, but it returns 0 or 1 so you can test relational loops with it. I just hardcoded the number of lines to do in the for loop, but you can come up with some better way to do that to read the entire file.


#include <stdlib.h>
#include <string.h>
#include <stdio.h>



int main()
{
    typedef struct 
    {
        char n_adults[10];
        char n_kids[10];
        char day[10];
        char weather[10];
    } daydata;

    daydata record[30];
    FILE *f;
    char line[121];
    char *item;

    int reccount = 0;
    int k;

    /*Open file*/
    
    f = fopen("newstuff.txt", "r");



    /*read file line by line*/
    for (int i = 0; i < 3; i++)
    {
        fgets(line,121,f);

        item = strtok(line, " ");
        strcpy(record[i].n_adults, item);
        item = strtok(NULL, " ");
        strcpy(record[i].n_kids, item);
        item = strtok(NULL, " ");
        strcpy(record[i].day, item);
        item = strtok(NULL, "\n");
        strcpy(record[i].weather, item);


        printf("it is %s %s %s %s \n", record[i].n_adults, record[i].n_kids, record[i].day, record[i].weather);
    }

    fclose(f);
}


#7
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
Not too bad - unfortunately, this code resulted in errors - see for your reference, and I am working out which I have made my other similar code work great so will compare and see what I am missing

Quote

jacob@ubuntu:~/week11$ cd week10
bash: cd: week10: No such file or directory
jacob@ubuntu:~/week11$ cd
jacob@ubuntu:~$ cd week10
jacob@ubuntu:~/week10$ gcc -o ArrayStruct2 ArrayStruct2.c
ArrayStruct2.c: In function ‘main’:
ArrayStruct2.c:31: error: ‘for’ loop initial declarations are only allowed in C99 mode
ArrayStruct2.c:31: note: use option -std=c99 or -std=gnu99 to compile your code



Zer033 said:

I modified your code and I think I got what you were looking for, but I'm not sure. As I said I haven't really programmed in C and I haven't used these functions before so just went to look at quick references to see what they're supposed to do. I changed the adults and kids to chars as well because I wasn't sure how to use atoi and it kept hanging the program when I tried to use it as you were. I'm not entirely sure what your segment fault was, but I think it has something to do with using fgets in the while loop. As far as I could tell fgets returns a char pointer, not any kind of evaluation return value. Maybe you have to use feof(FILE) in the while loop, I didn't try that, but it returns 0 or 1 so you can test relational loops with it. I just hardcoded the number of lines to do in the for loop, but you can come up with some better way to do that to read the entire file.



#include <stdlib.h>

#include <string.h>

#include <stdio.h>




int main()

{

    typedef struct 

    {

        char n_adults[10];

        char n_kids[10];

        char day[10];

        char weather[10];

    } daydata;


    daydata record[30];

    FILE *f;

    char line[121];

    char *item;


    int reccount = 0;

    int k;


    /*Open file*/

    

    f = fopen("newstuff.txt", "r");




    /*read file line by line*/

    for (int i = 0; i < 3; i++)

    {

        fgets(line,121,f);


        item = strtok(line, " ");

        strcpy(record[i].n_adults, item);

        item = strtok(NULL, " ");

        strcpy(record[i].n_kids, item);

        item = strtok(NULL, " ");

        strcpy(record[i].day, item);

        item = strtok(NULL, "\n");

        strcpy(record[i].weather, item);



        printf("it is %s %s %s %s \n", record[i].n_adults, record[i].n_kids, record[i].day, record[i].weather);

    }


    fclose(f);

}



#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Can you provide us a sample file that you are currently working with, so that we may test your code against it? I am mentioning the contents of newstuff.txt.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#9
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts

Quote

5 0 Wednesday Sunny
6 2 Thursday Wet
4 3 Friday Cloudy


Just save this as newstuff.txt

#10
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
If you know the file's format, why don't you use formatted reading then (fscanf) ?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users