Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.99 KB | None | 0 0
  1. // ConsoleApplication17.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "string.h"
  8.  
  9. //#include <mem.h>
  10.  
  11. typedef struct
  12. {
  13.     char *userName;
  14.     char *firstName;
  15.     char *lastName;
  16.     char *displayName;
  17.     char *jobTitle;
  18.     char *department;
  19.     char *officeNumber;
  20.     char *officePhone;
  21.     char *mobilePhone;
  22.     char *fax;
  23.     char *address;
  24.     char *city;
  25.     char *state;
  26.     char *zip;
  27.     char *country;
  28. } contact;
  29.  
  30. contact data[50];
  31. FILE * fptr;
  32. char lineRead[300];
  33. int i=0;
  34. int strLength;
  35. char *word;
  36.  
  37.  
  38. void fileOpen(){
  39.     fptr = fopen("c:\\Users\\o_molloy\\contacts.txt", "r");
  40.     if (fptr == NULL) {
  41.         printf("Error opening file ! \nWe couldn't find contacts.txt Program will close now.\nSorry :-(");
  42.         exit(0);
  43.     }
  44. }
  45.  
  46. void fileClose(){
  47.     fclose(fptr);
  48. }
  49.  
  50. void getWord(){
  51.     word=strtok(NULL,",");
  52.     strLength=strlen(word) + 1; //here
  53. }
  54.  
  55. void aFuckingFunction(){
  56.     word=strtok(lineRead,",");
  57.     if(*word=='\n') return;
  58.     if(lineRead!=NULL){
  59.         strLength=strlen(word) + 1; //here
  60.         data[i].userName = (char*) malloc(strLength*sizeof(char));
  61.         strcpy(data[i].userName, word);
  62.         getWord();
  63.         data[i].firstName = (char*) malloc(strLength*sizeof(char));
  64.         strcpy(data[i].firstName, word);
  65.         getWord();
  66.         data[i].lastName = (char*) malloc(strLength*sizeof(char));
  67.         strcpy(data[i].lastName ,word);
  68.         getWord();
  69.         data[i].displayName = (char*) malloc(strLength*sizeof(char));
  70.         strcpy(data[i].displayName, word);
  71.         getWord();
  72.         data[i].jobTitle = (char*) malloc(strLength*sizeof(char));
  73.         strcpy(data[i].jobTitle, word);
  74.         getWord();
  75.         data[i].department = (char*) malloc(strLength*sizeof(char));
  76.         strcpy(data[i].department, word);
  77.         getWord();
  78.         data[i].officeNumber = (char*) malloc(strLength*sizeof(char));
  79.         strcpy(data[i].officeNumber, word);
  80.         getWord();
  81.         data[i].officePhone= (char*) malloc(strLength*sizeof(char));
  82.         strcpy(data[i].officePhone, word);
  83.         getWord();
  84.         data[i].mobilePhone = (char*) malloc(strLength*sizeof(char));
  85.         strcpy(data[i].mobilePhone, word);
  86.         getWord();
  87.         data[i].fax = (char*) malloc(strLength*sizeof(char));
  88.         strcpy(data[i].fax, word);
  89.         getWord();
  90.         data[i].address = (char*) malloc(strLength*sizeof(char));
  91.         strcpy(data[i].address, word);
  92.         getWord();
  93.         data[i].city = (char*) malloc(strLength*sizeof(char)); //
  94.         strcpy(data[i].city, word);
  95.         getWord();
  96.         data[i].state = (char*) malloc(strLength*sizeof(char));
  97.         strcpy( data[i].state, word);
  98.         getWord();
  99.         data[i].zip = (char*) malloc(strLength*sizeof(char));
  100.         strcpy(data[i].zip, word);
  101.         getWord();
  102.         data[i].country = (char*) malloc(strLength*sizeof(char));
  103.         strcpy(data[i].country, word);
  104.     }
  105. }
  106.  
  107. //void printContact(contact *theContact){
  108. //    printf("%s", theContact);
  109. //}
  110.  
  111. void getLine(){
  112.     while(!feof(fptr))
  113.     {
  114.         if (fgets(lineRead, 300, fptr) == NULL) continue;
  115.         aFuckingFunction();
  116.         i++;
  117.     }
  118. }
  119.  
  120. void freeMemory(){
  121.     i--;
  122.  
  123.     while(i>-1){
  124.         free(data[i].country);
  125.         free(data[i].zip);
  126.         free(data[i].state);
  127.         free(data[i].city);
  128.         free(data[i].address);
  129.         free(data[i].fax);
  130.         free(data[i].mobilePhone);
  131.         free(data[i].officePhone);
  132.         free(data[i].officeNumber);
  133.         free(data[i].department);
  134.         free(data[i].jobTitle);
  135.         free(data[i].displayName);
  136.         free(data[i].lastName);
  137.         free(data[i].firstName);
  138.         free(data[i].userName);
  139.         i--;
  140.     }
  141.  
  142. }
  143. int main()
  144. {
  145.     fileOpen();
  146.     fgets(lineRead, 300, fptr);// ignore line
  147.     getLine();
  148.     fileClose();
  149.     freeMemory();
  150.  
  151.  
  152.  
  153.     return 0;
  154. }
  155. //by Jaroslaw Janas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement