-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab_15.cpp
188 lines (152 loc) · 8.85 KB
/
Lab_15.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Week18.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.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 SortData(char *dataLine);
void PrintContact(contact *theContact);
int NumLines();
int LineLength(int line);
int main()
{
contact *contactPtr;
FILE *fp;
fp = fopen("contacts.txt", "r+");
contactPtr = (contact *)malloc((NumLines()-1) * sizeof(contact));
for (int i = 0; i < NumLines() + 1; i++)
{
int lineLength = LineLength(i + 1) + 2;
char *temp = (char *)malloc(lineLength * sizeof(char));
fgets(temp, lineLength, fp);
if (i > 0)
{
contactPtr[i-1] = SortData(temp);
}
}
for (int i = 0; i < NumLines(); i++)
{
PrintContact((contactPtr+i));
}
return 0;
}
contact SortData(char *dataLine)
{
contact contact;
char *token = strtok(dataLine, ",");
contact.UserName = (char *)malloc(strlen(token) * sizeof(char));
contact.UserName = token;
token = strtok(NULL, ",");
contact.firstName = (char *)malloc(strlen(token) * sizeof(char));
contact.firstName = token;
token = strtok(NULL, ",");
contact.lastName = (char *)malloc(strlen(token) * sizeof(char));
contact.lastName = token;
token = strtok(NULL, ",");
contact.displayName = (char *)malloc(strlen(token) * sizeof(char));
contact.displayName = token;
token = strtok(NULL, ",");
contact.jobTitle = (char *)malloc(strlen(token) * sizeof(char));
contact.jobTitle = token;
token = strtok(NULL, ",");
contact.department = (char *)malloc(strlen(token) * sizeof(char));
contact.department = token;
token = strtok(NULL, ",");
contact.officeNumber = (char *)malloc(strlen(token) * sizeof(char));
contact.officeNumber = token;
token = strtok(NULL, ",");
contact.officePhone = (char *)malloc(strlen(token) * sizeof(char));
contact.officePhone = token;
token = strtok(NULL, ",");
contact.mobilePhone = (char *)malloc(strlen(token) * sizeof(char));
contact.mobilePhone = token;
token = strtok(NULL, ",");
contact.fax = (char *)malloc(strlen(token) * sizeof(char));
contact.fax = token;
token = strtok(NULL, ",");
contact.address = (char *)malloc(strlen(token) * sizeof(char));
contact.address = token;
token = strtok(NULL, ",");
contact.city = (char *)malloc(strlen(token) * sizeof(char));
contact.city = token;
token = strtok(NULL, ",");
contact.state = (char *)malloc(strlen(token) * sizeof(char));
contact.state = token;
token = strtok(NULL, ",");
contact.zip = (char *)malloc(strlen(token) * sizeof(char));
contact.zip = token;
token = strtok(NULL, ",");
contact.country = (char *)malloc(strlen(token) * sizeof(char));
contact.country = token;
return contact;
}
void PrintContact(contact *theContact)
{
printf("User Name: %s\nFirst Name: %s\nLast Name: %s\nDisplay Name: %s\nJob Title: %s\nDepartment: %s\nOffice Number: %s\nOffice Phone: %s\nMobile Phone: %s\nFax: %s\nAddress: %s\nCity: %s\nState or Province: %s\nZip or Post Code: %s\nCountry or Region: %s\n",
theContact->UserName,theContact->firstName, theContact->lastName, theContact->displayName, theContact->jobTitle,
theContact->department, theContact->officeNumber, theContact->officePhone, theContact->mobilePhone, theContact->fax,
theContact->address, theContact->city, theContact->state, theContact->zip, theContact->country);
}
int NumLines()
{
FILE *fp;
int lines = 0;
char ch;
fp = fopen("contacts.txt", "r+");
if (fp == NULL)
{
perror("Error opening file");
}
while (!feof(fp))
{
ch = fgetc(fp);
if (ch == '\n')
{
lines++;
}
}
fclose(fp);
return lines;
}
int LineLength(int line)
{
FILE *fp;
fp = fopen("contacts.txt", "r+");
int lineLength = 0;
int currentLine = 1;
char ch;
while(!feof(fp))
{
ch = fgetc(fp);
if (ch == '\n')
{
currentLine++;
}
if (currentLine == line)
{
lineLength++;
}
}
fclose(fp);
return lineLength;
}