Guys, I really, REALLY need your help getting info into a structure. (Please help!)

This is a discussion on Guys, I really, REALLY need your help getting info into a structure. (Please help!) within the C Programming forums, part of the General Programming Boards category; I had this posted at the C++ board, but than I realized this is a C problem! Ok first I ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    38

    Unhappy Guys, I really, REALLY need your help getting info into a structure. (Please help!)

    I had this posted at the C++ board, but than I realized this is a C problem!

    Ok first I have a notpad called "family.in" saved to my a drive with 5 family members info that looks just like this:

    Joe Redfield
    6220 Culebra
    San Antonio, TX 78228
    Self
    10-03-56
    45

    (plus 4 other people)

    Now I have to make a program that gets this info. Prints it to a screen and than makes an out file called "family.out" with the info turning into this (gotta show it as code, so the spaces will show):

    Code:
    Joe Redfield				Relation: 	Self
    6220 Culebra				Birthday:  10-03-56
    San Antonio, TX 78228			Age:        	45
    I do have a program running, so Im not asking anyone to do the work for me. I just need help getting the info and making it print out like the way its suppose to above. Instead, it prints out to the screen like the way it looks in the "family.in" file. But in the "family.out" file it looks like this:

    Joe Redfield

    6220 Culebra

    San Antonio, TX 78228

    Self

    10-03-56

    45

    I have no idea how to grap individual bits of info and I have no idea how Im suppose to get it to look the way my damn teacher wants it. My code is below, and I really hope theres someone here who can help me out. Thank you very much and its very appreciated.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
     struct family {	    
                    char name [50]; 		/* person's name */
                    char street [50]; 		/* street address */ 
                    char csz [50];		/* city, state, zip */
                    char relation [50];		/* relation to you */
                    char birthday [50];		/* mm-dd-yy */
                    char age [50];                         /*person's age */
                   };
    
    struct family PEOPLE[5], *member;
      
    void main (void)
    {
    char linein[50];  
    char getdata[50];
    int index;
    
      FILE *in, *out;
      index = 0;
    
      in = fopen ("a:\\family.in", "r");
      out = fopen ("a:\\family.out", "w"); 
      while ( !feof(in) )
         {
         fgets(linein, 50, in);
         getdata[index]=linein[50];
         printf("%s", linein);
         index++;
         fprintf(out, " %s \n", linein);
         }
    
    printf("\n\nThe value is: %s\n\n", getdata[1]);
    
     fclose(in);
     fclose(out);
    
    }
    Last edited by Desperado; 12-04-2001 at 10:34 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If the formatting of the file is so simple as to put one line into each data member, then you're all set. Just do this to enter in the info:
    Code:
    int inputRecord(struct *people, int i){
    if(fgets(people[i].name, sizeof(people[i].name), in) == NULL)
    return NULL;
    fgets(people[i].street, sizeof(people[i].street), in);
    fgets(people[i].csz, sizeof(people[i].csz);
    .
    .
    .etc...
    return 1;
    }
    Then to print it out, a simple fprintf:
    Code:
    void outputRecord(struct *people, int i){
    fprintf(out, "%s\t\t\tRelation:\t%s", people[i].name, people[i].relation);
    fprintf(out, "%s\t\t\tBirthday:\t%s", people[i].street, people[i].birthday);
    fprintf(out, "%s\t\t\tAge:\t%s", people[i].csz, people[i].age);
    }
    >in = fopen ("a:\\family.in", "r");
    Be sure to check and make sure that this actually worked
    Code:
    if((in = fopen("a:\\family.in", "r")) == NULL)
    puts("Error: Open File Failed!");
    >void main (void)
    Don't use void main, the only implementation of main that the standard accepts is int main(). Explicitly say that it's int and return 0 at the end of the function.

    >while ( !feof(in) )
    This could cause problems because by the time the program realizes that the end of file has been reached it has already done another read. Instead try reading in the first data entry of each structure in your array, or call the inputRecord function and have it return NULL if the end of file has been reached. That way you don't read in garbage at the end.
    Code:
    while(inputRecord(PEOPLE, index) != NULL)
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    38
    Cool! Thanks for your help dude. I modified my code with your suggestions and Im getting alot of errors. Heres my code below so you can check it out and see if I should have taken out somehting, or added somehting, or put somehting in the wrong place, something. lol Thanks for your help man. I cant tell youhow much I appreciate this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
     struct family {	    
                    char name [50]; 		/* person's name */
                    char street [50]; 		/* street address */ 
                    char csz [50];		        /* city, state, zip */
                    char relation [50];		/* relation to you */
                    char birthday [50];		/* mm-dd-yy */
                    char age [50];                  /*person's age */
                   };
    
    struct family PEOPLE[5], *member;
      
    void main (void)
    {
    char linein[50];  
    char getdata[50];
    int index;
    
      FILE *in, *out;
      index = 0;
    
      in = fopen ("a:\\family.in", "r");
    /*  out = fopen ("a:\\family.out", "w"); */
      while(inputRecord(PEOPLE, index) != NULL)
         {
         fgets(linein, 50, in);
         getdata[index]=linein[50];
         printf("%s", linein);
         index++;
         fprintf(out, " %s \n", linein);
         }
    
    printf("\n\nThe value is: %s\n\n", getdata[1]);
    
    int inputRecord(struct *people, int i){
    if(fgets(people[i].name, sizeof(people[i].name), in) == NULL)
    return NULL;
    fgets(people[i].street, sizeof(people[i].street), in);
    fgets(people[i].csz, sizeof(people[i].csz);
    fgets(people[i].relation, sizeof(people[i].relation);
    fgets(people[i].birthday, sizeof(people[i].birthday);
    fgets(people[i].age, sizeof(people[i].age);
    
    return 1;
    }
    
    void outputRecord(struct *people, int i){
    fprintf(out, "%s\t\t\tRelation:\t%s", people[i].name, people[i].relation);
    fprintf(out, "%s\t\t\tBirthday:\t%s", people[i].street, people[i].birthday);
    fprintf(out, "%s\t\t\tAge:\t%s", people[i].csz, people[i].age);
    }
    
     
     fclose(in);
     fclose(out);
    
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Okay, first you are passing a global array to a function, you don't need to do this. Move your PEOPLE declaration inside main. This is why i set up the functions to take a struct family parameter, if at all possible, avoid global variables of any type.

    Second, your out file opening has been commented out, so when you make a call to it in your print function it will most surely cause an error.

    And last, be sure to put in prototypes for your functions before the main function, unless you have a legacy compiler, this will cause an error.

    That's all i can see right off hand, it will be a little while today before i can get to a computer with a decent compiler.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Dynamic Structure in C
    By SomeNath in forum C Programming
    Replies: 1
    Last Post: 03-19-2005, 08:11 AM
  2. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 02:12 PM
  3. Controlling Repetition in a Structure
    By Silence in forum C Programming
    Replies: 2
    Last Post: 08-23-2002, 05:35 PM
  4. Function to return pointer to structure
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 06-25-2002, 06:10 AM
  5. nested structure help
    By whistler in forum C Programming
    Replies: 1
    Last Post: 05-17-2002, 10:48 AM

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