-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog5.c
41 lines (38 loc) · 834 Bytes
/
prog5.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
if(argv[1] == NULL || argv[2] != NULL)
{
printf("Error: Invalid input arguments. Must pass one file as argument to program.\n");
exit(1);
}
FILE * fp;
fp = fopen(argv[1], "r+");
if(fp == NULL)
{
printf("Error: File cannot be opened.\n");
exit(1);
}
else
{
fseek(fp, 0L, SEEK_END);
long int fileSize = ftell(fp);
printf("File size is %ld\n", fileSize);
fseek(fp, 0L, SEEK_SET);
char* pointer = (char*)malloc(fileSize);
fread(pointer, fileSize+1, 1, fp);
pointer[fileSize+1] = '\0';
fclose(fp);
const char* delim = " \t , \n";
char* tokenize = strtok(pointer, delim);
while (tokenize != NULL)
{
printf("%s\n", tokenize);
tokenize = strtok(NULL, delim);
}
free(pointer);
}
return(0);
}