-
Notifications
You must be signed in to change notification settings - Fork 0
/
p1Prog
240 lines (223 loc) · 8.02 KB
/
p1Prog
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**********************************************************************
p1elo931.c by Barry, Amadou
(change the previous line for your abc123 ID and your name)
Purpose:
This program reads user data and displays that information in a
human readable format.
Command Line Arguments:
p1 -u usersFileName
Input:
Stream input file which contains many users. There are two
different lines of data for each user:
- User Vehicle Information:
o One line per user (separated by commas)
szMake szModel dMpg dPpg iNumFillups
10s 10s lf lf d
- User Contact Information:
o One line per user (separated by commas)
szUserId szEmail szFullName szPhone szLoginName
6s 30s 20s 13s 12s
o Use caution when trying to convert szFullName.
Although it is a maximum of 20 characters, it may
contain spaces; therefore, you cannot simply use %20s.
Results:
Print the list of users as shown below.
UserID Make Model AvgMPG AvgPPG
Name Phone login email
TOY078 Toyota Corolla 33.33 3.04
Marty Falsex Jr. (210)555-7878 mfsex [email protected]
CHV042 Chevrolet Cruze 34.34 5.23
Klye Lorson (210)555-4242 klorson [email protected]
TOY018 Toyota Yaris 35.35 4.84
Khloe Bush (210)555-1818 kbush [email protected]
FRD002 Ford Focus 34.34 2.86
Nail Keshighski (210)555-0202 nhighski [email protected]
CHV048 Chevrolet Sonic 28.28 2.53
Johnny Jipsom (210)555-4848 jjipsom [email protected]
FRD017 Ford Fiesta 34.43 4.22
Stenny Rickman (210)555-1717 srickman [email protected]
Returns:
0 normal
1 invalid command line syntax
2 show usage only
3 error during processing, see stderr for more information
Notes:
p1 -? will provide the usage information. In some shells,
you will have to type reserve -\?
**********************************************************************/
// If compiling using visual studio, tell the compiler not to give its warnings
// about the safety of scanf and printf
#define _CRT_SECURE_NO_WARNINGS 1
#define ERR_INVALID_INVENTORY_DATA "invalid inventory file data"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cs1713p1.h"
FILE *pFileUsers; // stream Input for User data - Global variable
void processCommandSwitches(int argc, char *argv[], char **ppszUserFileName);
int main(int argc, char *argv[])
{
char *pszUserFileName = NULL;
// Process the command switches
processCommandSwitches(argc, argv, &pszUserFileName);
// open the Users stream data file
if (pszUserFileName == NULL)
exitError(ERR_MISSING_SWITCH, "-t");
pFileUsers = fopen(pszUserFileName, "r");
if (pFileUsers == NULL)
exitError(ERR_USER_FILENAME, pszUserFileName);
// get the Users Data
getUsers();
fclose(pFileUsers);
printf("\n"); // included so that you can put a breakpoint on this line
return 0;
}
/****** you need to document and code this function *****/
/*
o One line per user (separated by spaces)
szMake szModel dAvgMPG dAvgPPG iNumFillups
10s 10s lf lf d
- User Contact Information:
o One line per user (separated by commas)
szUserId szEmail szFullName szPhone szLoginName
6s 30s 20s 13s 12s
*/
/***********************
This fuction reads input from a file strea and prints it out to a formatted standard output
Usage:
./p1 -u p1UserData.txt prints output to screen
./p1 -u p1UserData.txt > p1UserDataOutput.txt prints out output to the text file p1UserDataOutput.txt
************************/
void getUsers(void)
{
char szInptBuffer[100];
int iScanfCnt;
int iUserCnt = 0;
char *pszUserResult;
User user;
// Print headings for the output
printf("%6s \t%4s \t%5s \t%6s \t%6s\n","UserID","Make","Model","AvgMPG","AvgPPG");
printf("\t%4s \t\t%5s \t\t%5s \t\t%5s\n","Name","Phone","login","email");
// Read user data until reach EOF
while (fgets(szInptBuffer, 100, pFileUsers) != NULL)
{
iScanfCnt = sscanf(szInptBuffer, "%10s %10s %lf %lf %d\n"
//,user.szUserId
,user.szMake
,user.szModel
,&user.dAvgMPG
,&user.dAvgPPG
,&user.iNumFillups);
//if (iScanfCnt < 5)
//exitError("Bad data file Error 2","");
iScanfCnt = sscanf(szInptBuffer, "%6s, %30s, %[^, ], %13s, %12s\n"
,user.szUserId
,user.szEmailAddr
,user.szFullName
,user.szPhone
,user.szLoginName);
//if (iScanfCnt < 5)
//exitError("Bad data file Error2","");
printf("%6s \t%10s \t%10s \t%.2lf \t%.2lf\n"
,user.szUserId
,user.szMake
,user.szModel
,user.dAvgMPG
,user.dAvgPPG);
printf("\t\t%20s \t\t%13s \t\t%12s \t\t%30s\n"
,user.szFullName
,user.szPhone
,user.szLoginName
,user.szEmailAddr);
//Counter to continu reading until EOF
iUserCnt++;
}
}
/******************** processCommandSwitches *****************************
void processCommandSwitches(int argc, char *argv[], char **ppszUserFileName)
Purpose:
Checks the syntax of command line arguments and returns the filenames.
If any switches are unknown, it exits with an error.
Parameters:
I int argc Count of command line arguments
I char *argv[] Array of command line arguments
O char **ppszUserFileName User file name
Notes:
If a -? switch is passed, the usage is printed and the program exits
with USAGE_ONLY.
If a syntax error is encountered (e.g., unknown switch), the program
prints a message to stderr and exits with ERR_COMMAND_LINE_SYNTAX.
**************************************************************************/
void processCommandSwitches(int argc, char *argv[], char **ppszUserFileName)
{
int i;
// Examine each command argument (except argument 0 which is the executable name)
for (i = 1; i < argc; i++)
{
// check for a switch
if (argv[i][0] != '-')
exitUsage(i, ERR_EXPECTED_SWITCH, argv[i]);
// determine which switch it is
switch (argv[i][1])
{
case 'u': // User File Name
if (++i >= argc)
exitUsage(i, ERR_MISSING_ARGUMENT, argv[i - 1]);
else
*ppszUserFileName = argv[i];
break;
case '?':
exitUsage(USAGE_ONLY, "", "");
break;
default:
exitUsage(i, ERR_EXPECTED_SWITCH, argv[i]);
}
}
}
/******************** exitError *****************************
void exitError(char *pszMessage, char *pszDiagnosticInfo)
Purpose:
Prints an error message and diagnostic to stderr. Exits with
ERROR_PROCESSING.
Parameters:
I char *pszMessage error message to print
I char *pszDiagnosticInfo supplemental diagnostic information
Notes:
This routine causes the program to exit.
**************************************************************************/
void exitError(char *pszMessage, char *pszDiagnosticInfo)
{
fprintf(stderr, "Error: %s %s\n"
, pszMessage
, pszDiagnosticInfo);
exit(ERROR_PROCESSING);
}
/******************** exitUsage *****************************
void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo)
Purpose:
If this is an argument error (iArg >= 0), it prints a formatted message
showing which argument was in error, the specified message, and
supplemental diagnostic information. It also shows the usage. It exits
with ERR_COMMAND_LINE_SYNTAX.
If this is just asking for usage (iArg will be -1), the usage is shown.
It exits with USAGE_ONLY.
Parameters:
I int iArg command argument subscript
I char *pszMessage error message to print
I char *pszDiagnosticInfo supplemental diagnostic information
Notes:
This routine causes the program to exit.
**************************************************************************/
void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo)
{
if (iArg != 2)
fprintf(stderr, "Error: bad argument #%d. %s %s\n"
, iArg
, pszMessage
, pszDiagnosticInfo);
fprintf(stderr, "p1 -u userFileName\n");
if (iArg != 2)
exit(1);
else
exit(2);
}