Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication17.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include "string.h"
- //#include <mem.h>
- typedef struct
- {
- char *userName;
- char *firstName;
- char *lastName;
- char *displayName;
- char *jobTitle;
- char *department;
- char *officeNumber;
- char *officePhone;
- char *mobilePhone;
- char *fax;
- char *address;
- char *city;
- char *state;
- char *zip;
- char *country;
- } contact;
- contact data[50];
- FILE * fptr;
- char lineRead[300];
- int i=0;
- int strLength;
- char *word;
- void fileOpen(){
- fptr = fopen("c:\\Users\\o_molloy\\contacts.txt", "r");
- if (fptr == NULL) {
- printf("Error opening file ! \nWe couldn't find contacts.txt Program will close now.\nSorry :-(");
- exit(0);
- }
- }
- void fileClose(){
- fclose(fptr);
- }
- void getWord(){
- word=strtok(NULL,",");
- strLength=strlen(word) + 1; //here
- }
- void aFuckingFunction(){
- word=strtok(lineRead,",");
- if(*word=='\n') return;
- if(lineRead!=NULL){
- strLength=strlen(word) + 1; //here
- data[i].userName = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].userName, word);
- getWord();
- data[i].firstName = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].firstName, word);
- getWord();
- data[i].lastName = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].lastName ,word);
- getWord();
- data[i].displayName = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].displayName, word);
- getWord();
- data[i].jobTitle = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].jobTitle, word);
- getWord();
- data[i].department = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].department, word);
- getWord();
- data[i].officeNumber = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].officeNumber, word);
- getWord();
- data[i].officePhone= (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].officePhone, word);
- getWord();
- data[i].mobilePhone = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].mobilePhone, word);
- getWord();
- data[i].fax = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].fax, word);
- getWord();
- data[i].address = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].address, word);
- getWord();
- data[i].city = (char*) malloc(strLength*sizeof(char)); //
- strcpy(data[i].city, word);
- getWord();
- data[i].state = (char*) malloc(strLength*sizeof(char));
- strcpy( data[i].state, word);
- getWord();
- data[i].zip = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].zip, word);
- getWord();
- data[i].country = (char*) malloc(strLength*sizeof(char));
- strcpy(data[i].country, word);
- }
- }
- //void printContact(contact *theContact){
- // printf("%s", theContact);
- //}
- void getLine(){
- while(!feof(fptr))
- {
- if (fgets(lineRead, 300, fptr) == NULL) continue;
- aFuckingFunction();
- i++;
- }
- }
- void freeMemory(){
- i--;
- while(i>-1){
- free(data[i].country);
- free(data[i].zip);
- free(data[i].state);
- free(data[i].city);
- free(data[i].address);
- free(data[i].fax);
- free(data[i].mobilePhone);
- free(data[i].officePhone);
- free(data[i].officeNumber);
- free(data[i].department);
- free(data[i].jobTitle);
- free(data[i].displayName);
- free(data[i].lastName);
- free(data[i].firstName);
- free(data[i].userName);
- i--;
- }
- }
- int main()
- {
- fileOpen();
- fgets(lineRead, 300, fptr);// ignore line
- getLine();
- fileClose();
- freeMemory();
- return 0;
- }
- //by Jaroslaw Janas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement