-
Notifications
You must be signed in to change notification settings - Fork 0
/
work_with_param.c
144 lines (125 loc) · 3.62 KB
/
work_with_param.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "work_with_param.h"
const char FILE_NAME[] = "dane.txt";
/*
* Return '0' if string isn't number or '1' if it is number.
* Function supports float and double, signed and unsigned number.
*/
int is_str_number(char *str, int str_len)
{
int is_number = 0;
int counter_coma = 0;
int i = 0;
if (str[0] == '-')
i++;
while ((str[i]>='0' && str[i]<='9') || str[i]=='.')
{
if (str[i] == '.')
{
counter_coma++;
if (counter_coma > 1)
{
printf("to many '.' in number\n");
break;
}
}
i++;
}
if (i == str_len)
is_number = 1;
else
is_number = 0;
return is_number;
}
/*
* Save string to end of file.
*/
FILE* save_to_file(const char file_name[], char *arg)
{
FILE *fp;
fp = fopen(file_name, "a");
fprintf(fp, "%s ", arg);
fclose(fp);
return fp;
}
/*
* Description is at 'work_with_param.h'
*/
void work_with_param(char *arg)
{
int is_number;
int len = strlen(arg);
if (is_str_number(arg,len))
{
if (save_to_file(FILE_NAME, arg))
printf("%s has been saved to %s\n", arg, FILE_NAME);
else
fprintf(stderr,"error while saving to file\n");
}
else
printf("%s\n", arg);
}
/*
* Read number from file FILE_NAME
* Definition in 'work_with_param.h'
*/
int param_read_from_file(double *num)
{
int status = STATUS_OK;
char str_nr[100];
FILE *fp;
double result;
char ch;
char *ptr;
int len = 0; // number of char in some number's string
int num_detection_flag = 0; // Has the program detected any number in the file, going backward?
long int index_of_last_char_in_last_number = 0;
fp = fopen(FILE_NAME, "r");
if (!fp)
return FILE_NO_EXIST;
fseek(fp, 0L, SEEK_SET);
if ((getc(fp)) == EOF) // Is it a empty file?
status = EMPTY_FILE;
else
{
fseek(fp, 0L, SEEK_END); // go to end of file (EOF)
do
{
fseek(fp, -1L, SEEK_CUR); // go to previous char
ch = getc(fp); // get a char
fseek(fp, -1L, SEEK_CUR); // pointer to current char
if ((ch>='0' && ch<='9') || ch=='.' || ch=='-') // check, char can be a part of number
{
len++;
if (num_detection_flag == 0)
{
num_detection_flag = 1;
index_of_last_char_in_last_number = ftell(fp);
}
}
else
{
if (num_detection_flag == 1) // detected a char before the first number's char
break;
}
} while (ftell(fp) != 0L); // while current char isn't the start of file
if(ftell(fp) != 0L) // if this isn't start of file
fseek(fp, 1L, SEEK_CUR); // go to the fist char in number's string
if (len == 0) // program hasn't detected any number in the file
status = NO_DETECTED_NUM;
else // copy number string to result
{
for (int i=0; i < len; i++)
str_nr[i] = getc(fp);
str_nr[len+1] = '\0';
result = strtod(str_nr, &ptr);
}
}
fseek(fp, 0L, SEEK_SET);
fclose(fp);
*num = result;
return status;
}