-
Notifications
You must be signed in to change notification settings - Fork 0
/
writing.erl
139 lines (127 loc) · 4.14 KB
/
writing.erl
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
-module(writing).
-export([read_Pattern/1, start/1, write_position/2]).
start(NAME) ->
{ok, File} = file:open(NAME, [write]),
spawn(writing, write_position, [1.1213213413e+1, File]),
spawn(writing, write_position, [2.1213423541e+1, File]),
spawn(writing, write_position, [3.1213412341e+1, File]),
timer:sleep(1000),
file:position(File, 0),
%%spawn(writing,foo2,[4,File]),
timer:sleep(1000),
file:close(File),
{ok, File2} = file:open(NAME, [read]),
%%spawn(writing,read_Pattern,[File2]),
%%spawn(writing,read_Pattern,[File2]),
%%spawn(writing,read_Pattern,[File2])
COO = read_Pattern(File2),
io:format("I've read ~w~n", [COO]),
{ok, File3} = file:open("experiment.dat", [write]),
Stringa = posToStr2([1.1, 2.2]),
io:format("string \t to \t print ~p ~n", [Stringa]),
file:close(File3).
posToStr2([]) -> "\n";
posToStr2([HEAD | TALE]) ->
io:format("mando: ~f\t ~n", [HEAD]),
string:sub_string(float_to_list(HEAD), 0, 8) ++
" " ++ posToStr2(TALE).
write_position(N, File) ->
io:format(File, "~w\t~w\t~w~n", [N, N, N]),
write_position(N + 1,
File). %file:write(File,"data to be written ~n")
% %% recursive, reads Patterns till the end of the file
% read_Patterns(File) ->
%
% case io:get_line(File, "")
% of
% eof -> file:close(File);
% Line -> io:format( "I've read ~s~n",[Line]),
% io:format( "I've read ~w~n",[Line]),
% Pattern= get_coordinates(Line),
%
% io:format( "READ : ~w~n",[Pattern]),
% read_Patterns(File)
% end
%
% .
% %% Reads jsut one pattern
% %% + returns a List of Coordinates
% read_Pattern(File) ->
%
% Read=io:get_line(File, "") ,
% io:format( "I've read ~w~n",[Read]),
%
% case Read
% of
% eof -> file:close(File);
% Line -> io:format( "I've read ~s~n",[Line]),
% get_coordinates(Line)
%
% end
%
% .
% %%%% get_coordinates(LINE) %%%%%%%%%%
% %% given as input a Line containing indefinete number of colons, reads the coordinates a puts them in a List
% %%
% %% + returns a List of coordinates
%
% get_coordinates(LINE) -> get_coordinates(LINE,[], []).
%
% get_coordinates([],TEMP, _X) -> TEMP;
% get_coordinates([HEAD|TAIL],TEMP, PARTIALNUMBER) ->
% if
% HEAD == 9; HEAD ==10 ->
% io:format("found tab or new line~n"),
% get_coordinates(TAIL, lists:append(TEMP, PARTIALNUMBER),[]);
%
% HEAD == 46 ->
% %%io:format("I found a point , i continue to add to partial: ~w ~w~n",[TEMP, [list_to_([HEAD])] ]),
% qardinates(TAIL, TEMP, lists:append(PARTIALNUMBER, list_to_binary([HEAD] ) ));
% %%io:format("New number added : ~w~n",[TEMP]);
%
% true->
% io:format("New number added : ~w ~w~n",[TEMP, list_to_integer( [HEAD] ) ] ),
% get_coordinates(TAIL, TEMP, lists:append(PARTIALNUMBER,list_to_integer([HEAD])))
% %%io:format("New number added : ~w~n",[TEMP])
%
% end
% .
% str_to_float(Str) ->
% try float(list_to_integer(Str))
% catch error:_ -> list_to_float(Str)
% end.
%
%%%%%% read_Pattern(File)
%% reads a new line (a string) form the file descriptor File, separates the substring separated by " ", and converts them into a list of numbers %%
%% (integesr or float)
%%
%% + returns: a list of values
%% + calls: convert(Line)
%%
read_Pattern(File) ->
case io:get_line(File, "") of
eof -> file:close(File);
Line ->
io:format("I've read ~s~n", [Line]),
Pattern = string:tokens(Line, "\t"),
%%X=lists:nth(1,Line),
Numbers = convert(Pattern),
io:format("READ : Pattern: ~s Nombers :~w~n, ",
[Pattern, Numbers]),
read_Pattern(File)
end.
%%%% convert(LINE) %%%%%%%%%%
%% given as input a List of Tokens (String) containing the BitString form of a floating point or a integer, returns a List of corrisponding integers or %%% float
%%
%% es: convert(["22.2","1","33.241235"]) -> [22.2, 1, 33.241235]
%%
%% + returns a List of coordinates
convert(P) -> convert(P, []).
convert([], TEMP) -> TEMP;
convert([HEAD | TAIL], TEMP) ->
case string:to_float(HEAD) of
{error, _} ->
convert(TAIL,
TEMP ++ [element(1, string:to_integer(HEAD))]);
{Number, _Empty} -> convert(TAIL, TEMP ++ [Number])
end.