Like Tree1Likes
  • 1 Post By Tclausex

Homework Help (structs and such)

This is a discussion on Homework Help (structs and such) within the C Programming forums, part of the General Programming Boards category; Hey, I've run into a bind with a homework problem. I'm trying to learn structs but they are confusing me. ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    Homework Help (structs and such)

    Hey, I've run into a bind with a homework problem. I'm trying to learn structs but they are confusing me. Essentially, the homework problem is to accept a text file with 7 elements in each line,

    "Term, Student ID, "Last Name", "First Name", Subject, Catalog Number, ="Section"

    and then sort it alphabetically in a table format. I'm supposed to use structs and makefile but I'm still a beginner in those things. To use structs to read the data from the file, would I have to make a struct for each of those elements with their own arrays or would I make a struct for each line essentially.

    main code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char *argv[]){
    
    
        char text[500];
        char line[100][100];
        FILE *ifp;
        char names[30];
    
    
        ifp = fopen("names", "r");
        fscanf(ifp, "%s", names);
        printf(names);
    
    
        return 0;
    }
    .h structs

    Code:
    struct student{
    	int id;
    	char name[100];
    }
    struct student s{
    	s.term;
    	s.id;
    	s.fname;
    	s.lname;
    	s.subject;
    	s.cat;
    	s.sec
    
    
    fscanf
    
    
    struct student list[100];
    	list[i].id
    	list[i].name
    What I have so far.
    Thanks
    Last edited by Langard; 03-04-2013 at 11:49 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    You see here some code to copy from one file to another. In your case you need only to read, so pick up the things you need from the link.

    Now about structs, read here.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    Thanks, those examples helped alot (I think). Since I have to have multiple source files, I have a header file that contains all of my structs. Would it be easier to open the file in the main program and then send it to the header file to input the data or would it be easier to open the file in the header file and then do all the data allocation there. Also, how would I go about that? I know the command for the program to read the text file is ifp, so do I use that as an array?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Hmm it seems that you don't have it clear in your mind about file scattering. I would suggest you to write everything in one file (main.c) and then we can divide the code into files
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    Well, this is what I've got so far. I don't know where to initialize the struct so i just threw it underneath int i. I have no idea how to get the file to be read and then transferred into my struct.

    Code:
    
    
    
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    //#include "Student.h"
    
    
    struct student{
        int term;
        int id;
        char fname[15];
        char lname[15];
        char sub[3];
        int cat;
        int sec;
    }
    typedef struct student student;
    
    
    void input(student*, int);
    
    
    int main(int argc, char *argv[]){
    
    
        FILE *ifp;
        char text[500];
        char line[100][100];
        char names[30];
        student stdArray[100]
    
    
        if (argc != 3){
            printf("Error");
            return 0;
        }
    
    
        ifp = fopen(argv[1], "rb");
        fwrite(
        input(std, 100);
        return 0;
    }
    
    
    void input(student* stdArray, int N){
        int i;
        struct student s;
        s.term;
        s.id;
        s.fname;
        s.lname;
        s.sub;
        s.cat;
        s.sec
    
    
        for(i=0;i<N;i++){
    .

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    input is wrong. I didn't use another struct, i used the one of main.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    So something like this? I'm really lost right now (Mega beginner). I'm supposed to write the file into the stdArray array and I'm not so sure how to do that.

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    //#include "Student.h"
    
    
    struct student{
    	int term;
    	int id;
    	char fname[15];
    	char lname[15];
    	char sub[3];
    	int cat;
    	int sec;
    }
    typedef struct student student;
    
    
    void input(student*, int);
    void sort(student*, int);
    
    
    int main(int argc, char *argv[]){
    
    
    	FILE *ifp;
    	char text[500];
    	char line[100][100];
    	char names[30];
    	student stdArray[100]
    
    
    	if (argc != 3){
    		printf("Error");
    		return 0;
    	}
    
    
    	ifp = fopen(argv[1], "rb");
    	fwrite(
    	input(stdArray, 100);
    	sort(stdArray, 100);
    	return 0;
    }
    
    
    void input(student* stdArray, int N){
    	int i;
    	for(i=0;i<N;i++){
    		fwrite(&student, sizeof(text), ifp);
    	}
    	fclose(ifp);
    	return student;
    }

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Copying and pasting can be dangerous coding. Why are you fwrite'ing?


    You stated it was a text file, so open it as a text file. Also, you should always check that the file open was successful:
    Code:
    if( (ifp = fopen( argv[1], "r") == NULL  )
    {
       printf("Could not open file %s for reading.\n", argv[1]) ;
       exit(EXIT_FAILURE);   //stdlib.h
    }

    I would then read in one record at a time using fgets(). Then parse the string from fgets and populate the appropriate struct's members. Once you have this, then you can worry about sorting.


    One thing you should ask yourself is - do we really need to store term, id, and such as integers or can we leave them as strings? If your spec says store as int, or you have operations to do on them that requires them to be int, then int it is. Otherwise, from what you've said, you'll just be outputting the alphabetically sorted records in some formatted fashion and strings are fine.


    Note: You need a semicolon at the end of your struct definition. And, IMHO, sub, cat and sec are lousy names. I'm about as lazy as they come, but I would use clearer names like subject, catalog_no, and section.

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    I got something like this. I'm kinda confused as to what goes into the place where I got "&names" because I'm getting an error there saying "undeclared identifier". I presume I'm using it incorrectly. Also, can I store the entire line in a char string? Or do I have to manually do a variety of %s and %d in the while and sscanf functions. Thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //#include "Student.h"
    
    
    struct student;
    	int term;
    	int id;
    	char fname[15];
    	char lname[15];
    	char subject[3];
    	int catalog;
    	int section;
    
    
    /*
    //typedef struct student student;
    
    
    //void input(student*, int);
    //void sort(student*, int);
    
    
    int main(int argc, char *argv[]){
    */
    int main(){
    	FILE *ifp;
    	//char text[500];
    	//char line[100][100];
    	char list[100];
    	//student stdArray[100];
    	
    	ifp = fopen("names.txt", "r");
    	while(fgets(list, 100, ifp) != NULL){
    		sscanf(list, "%s", &);
    	}
    	fclose(ifp);
    	//input(stdArray, 100);
    	//sort(stdArray, 100);
    	return 0;
    }
    
    
    /*void input(student* stdArray, int N){
    	int i;
    	for(i=0;i<N;i++){
    	
    	fclose(ifp);
    	return student;
    }*/
    Coding is frustrating, but I know that if I can get this, I can taste that sweet sweet victory.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by Langard View Post
    Or do I have to manually do a variety of %s and %d in the while and sscanf functions. Thanks
    Yes, and no. You'll need to use the negated scanset instead of %s because you don't want the commas. %d will work as is, and you need literal commas after each specifier to eat up the comma. And a space before each negated scanset to skip whitespace.

    I don't know why you have names[30]. All you need is your array of student structs, stdArray. And you'll be assigning the values from the record string into each struct member with sscanf. You will, of course, need to move through the array somehow during your while loop.

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    Code:
    int main(){
    	FILE *ifp;
    	//char text[500];
    	//char line[100][100];
    	char list[50];
    	student stdArray[50];
    	
    	ifp = fopen("names.txt", "r");
    	while(fgets(list, 50, ifp) != NULL){
    		sscanf(list, "%[^50]", &stdArray);
    	}
    	fclose(ifp);
    	//input(stdArray, 100);
    	//sort(stdArray, 100);
    	return 0;
    }
    I'm really confused here. I think the reading part of the file is right but the scanf is definitely wrong. I'm confused on how to get that line to work because all i'm getting are error messages. The array (stdArray) is apparently too large and it also doesn't seem to work. Array of structs are killin' me haha.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Whoa. Your fundamental misunderstanding of structs and how to use format strings with scanf functions is really killing you here.

    I'm not sure where you are getting the number 100 or 50, but let's just assume that 50 is enough, that is, there are 50 or fewer records in the file.

    So then you have an array of 50 student structs, and each struct has the member variables term, id, lname,...

    fgets() reads 1 line at time from your file. Read the description of the function. So, each iteration of the while loop, you read 1 line of the file.

    Then you use sscanf to break up that line (string) into the different fields, term, id, lname,... and store them into the member variables.

    You don't just try to stuff a string into a struct - that just doesn't even make sense. I'll give you an example - but DONT just copy what I do. Understand what is going on and then apply it to your problem.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    #define MAX_NAME 30
    #define BAR_MAX 5
    #define REC_MAX 50
    
    
    typedef struct foo FOO ;
    
    
    struct foo{
        int   number ;
        char  name[MAX_NAME] ;
        float score ;
        } ;
    
    
    int main()
    {
      int  i = 0 ;
      FOO  bar[BAR_MAX] ;
      char record[REC_MAX + 1] = {0};
      FILE *fp ;
    
    
    
    
    
    
      if( (fp = fopen("foo.txt", "r")) == NULL )
      {
        printf("Could not open foo.txt for reading.\n");
        exit(1) ;
      }
    
    
    
    
      while( i < BAR_MAX  &&  fgets( record, REC_MAX, fp ) )
      {
        sscanf( record, "%d: %[^:]: %f", &bar[i].number,
                                          bar[i].name,
                                         &bar[i].score ) ;
        ++i ;
      }
    
    
      for( i = 0; i < BAR_MAX; ++i )
        printf("%5d %10s %3.1f\n", bar[i].number, bar[i].name, bar[i].score) ;
    
    
    return 0 ;
    }
    With foo.txt
    Code:
    1:Frank:3.0
    2: Bob:  2.1
    3:Sue:4.0
    4:Dave:3.2
    5:    Anne:2.9
    AndiPersti likes this.

  13. #13
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    Thanks for all the help guys. I wish I had more time but I am an awful procrastinator and the homework's due. I think I understand structs a bit more which is probably the only plus from this wonderful experience haha. Seriously though, thanks a lot for all the help. I'll definitely start my homework earlier next time haha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2013, 06:55 AM
  2. Typedef Structs inside Typdef structs
    By gremory in forum C Programming
    Replies: 21
    Last Post: 12-30-2011, 06:48 PM
  3. Structs homework
    By nikosthegreat in forum C Programming
    Replies: 2
    Last Post: 12-01-2010, 04:29 PM
  4. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM

Tags for this Thread


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