Trouble using gets(var) for struct data

This is a discussion on Trouble using gets(var) for struct data within the C Programming forums, part of the General Programming Boards category; Hey everyone. I'm fairly new to C programming, in fact cprogramming.com tutorials is where I began my education. I've run ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    Trouble using gets(var) for struct data

    Hey everyone. I'm fairly new to C programming, in fact cprogramming.com tutorials is where I began my education. I've run into a bit of trouble however, trying to get a char array input and writing it to a file from a struct pointer data variable.

    Code:
    /*
     * File:   addvictim.c
     * Author: Crypto
     * Purpose: Adds a victim document to the depository.
     * Created on February 28, 2011, 2:24 PM
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct fields {
        int age;
        char *name;
    };
    
    FILE *file;
    char path[256] = "C:\\Users\\Seven\\Depository\\Victims\\";
    char fname[32];
    char ext[5] = ".dat";
    struct fields dox;
    struct fields *ptr;
    
    int main(void) {
        ptr = &dox;
        printf("Enter your desired %s file name : ", ext);
        gets(fname);
        file = fopen(strcat(path, strcat(fname, ext)), "a+");
        printf("Enter the victim full name : ");
        gets(dox.name);
        fprintf(file, "NAME : %s \n", ptr->name);
        getchar();
        return (0);
    }
    Compiles 100% of course but after I get all input and do getchar() and press enter it says the application has stopped working.

    what am I doing wrong?

    ^ This is all an attempt to improve off my horrific original code.

    Code:
    /* 
     * File:   addvictim.c
     * Author: Crypto
     * Purpose : 
     * Created on February 26, 2011, 2:24 PM
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    /*
     * 
     */
    int main(void) {
        FILE *file;
        char filep[256] = "C:/Users/Seven/Depository/Victims/";
        char victim[32];
        char ext[5] = ".dox";
        char name[25];
        char gender[7];
        char dob[12];
        char hometown[32];
        char tele[15];
        char email[256];
        char pass[256];
        char misc[256];
    
        printf("Enter victim name : ");
        gets(victim);
        file = fopen(strcat(filep, strcat(victim, ext)), "a+");
        printf("Enter the victim full name :");
        gets(name);
        fprintf(file, "NAME : %s \n", name);
        printf("Enter the victim gender :");
        gets(gender);
        fprintf(file, "GENDER : %s \n", gender);
        printf("Enter the victim date of birth :");
        gets(dob);
        fprintf(file, "DATE OF BIRTH : %s \n", dob);
        printf("Enter the victim hometown :");
        gets(hometown);
        fprintf(file, "HOMETOWN : %s \n", hometown);
        printf("Enter the victim telephone number :");
        gets(tele);
        fprintf(file, "PHONE # : %s \n", tele);
        printf("Enter the victim email  :");
        gets(email);
        fprintf(file, "EMAIL : %s \n", email);
        printf("Enter the victim password :");
        gets(pass);
        fprintf(file, "PASSWORD : %s \n", pass);
        printf("Enter the victim miscellaneous information:");
        gets(misc);
        fprintf(file, "MISC : %s \n", misc);
        fclose(file);
        printf("\nComplete! \n");
        getchar();
        return (0);
    }
    By using a data structure.
    Last edited by Crypto.; 03-09-2011 at 04:48 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    First, remove gets() from your mind, and never use it again.
    SourceForge.net: Gets - cpwiki

    The big problem with your first attempt was that the name structure was an uninitialised pointer. you need to make it point at some memory before trying to read in data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    struct fields {
        int age;
        char *name;
    };
    The name field is a pointer with no memory allocated.

    Try it like this...
    Code:
    struct fields {
        int age;
        char name[64];
    };

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Yep, I'm super eager to help a newbie whose code is littered with the word "victim" and intends to save said victim's password and contact information.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    Quite - perhaps we're better off letting them carry on using gets() then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Type Trouble
    By bluej322 in forum C Programming
    Replies: 6
    Last Post: 03-02-2011, 04:37 PM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 07:43 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM
  4. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 08:11 PM

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