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.