-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc.html
203 lines (140 loc) · 10.1 KB
/
c.html
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
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<!-- Set the viewport width to device width for mobile -->
<meta name="viewport" content="width=device-width" />
<title>Programming Journey - C</title>
<link rel="stylesheet" href="http://hsiangholin.github.io/blog/theme/css/normalize.css" />
<link rel="stylesheet" href="http://hsiangholin.github.io/blog/theme/css/foundation.min.css" />
<link rel="stylesheet" href="http://hsiangholin.github.io/blog/theme/css/style.css" />
<link rel="stylesheet" href="http://hsiangholin.github.io/blog/theme/css/pygments.css" />
<script src="http://hsiangholin.github.io/blog/theme/js/custom.modernizr.js"></script>
</head>
<body>
<!-- Nav Bar -->
<nav>
<div class="top-bar">
<div class="row">
<div class="large-9 large-centered columns">
<h1><a href="http://hsiangholin.github.io/blog">Programming Journey</a></h1>
</div>
</div>
</div>
<!-- Show menu items and pages -->
<div class="row">
<div class="large-9 columns">
<ul class="button-group navigation">
</ul>
</div>
</div>
</nav>
<!-- End Nav -->
<!-- Main Page Content and Sidebar -->
<div class="row">
<!-- Main Blog Content -->
<div class="large-9 columns">
<article>
<a href="http://hsiangholin.github.io/blog/posts/2014/02/how-many-characters-are-read.html"><h3 class="article-title">fgets: How Many Characters/Bytes Are Read?</h3></a>
<h6 class="subheader" title="2014-02-15T10:31:00">Sat 15 February 2014
</h6>
<p>If you have ever used fgets, you probably know we check its return value to know if the reading is done, by comparing the return value with NULL.</p>
<p>But apparently fgets itself doesn't tell you how many characters or bytes are read every time you call it.</p>
<p>To do this, we will need <a href="http://www.cplusplus.com/reference/cstdio/ftell/">ftell</a>. Every time after you called fgets to read from a "FILE", the position indicator of the "FILE" stream is moved to the position next to the end of what you have read, i.e. the beginning of the rest of the stream. ftell simply returns the current value of the position indicator of the "FILE" stream, so by comparing the positions you get before and after a fgets call, you will be able to know how many characters or bytes are just read.</p>
<p>Test it:</p>
<div class="highlight"><pre><span class="cp">#include <stdio.h></span>
<span class="cp">#include <stdlib.h></span>
<span class="cp">#include <string.h></span>
<span class="cp">#define BUFF_SIZE 1024</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="kt">FILE</span><span class="o">*</span> <span class="n">bs_fp</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="kt">char</span> <span class="n">buff</span><span class="p">[</span><span class="n">BUFF_SIZE</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
<span class="kt">int</span> <span class="n">last_pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">curr_pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">bs_fp</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="s">"test.txt"</span><span class="p">,</span> <span class="s">"r"</span><span class="p">);</span>
<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">bs_fp</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">goto</span> <span class="n">exit</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">while</span><span class="p">(</span><span class="n">fgets</span><span class="p">(</span><span class="n">buff</span><span class="p">,</span> <span class="n">BUFF_SIZE</span><span class="p">,</span> <span class="n">bs_fp</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">curr_pos</span> <span class="o">=</span> <span class="n">ftell</span><span class="p">(</span><span class="n">bs_fp</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"read size = %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="n">curr_pos</span> <span class="o">-</span> <span class="n">last_pos</span><span class="p">);</span>
<span class="n">last_pos</span> <span class="o">=</span> <span class="n">curr_pos</span><span class="p">;</span>
<span class="p">}</span>
<span class="nl">exit:</span>
<span class="k">if</span><span class="p">(</span><span class="n">bs_fp</span><span class="p">)</span>
<span class="n">fclose</span><span class="p">(</span><span class="n">bs_fp</span><span class="p">);</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div><p class="subheader">Category: <a href="http://hsiangholin.github.io/blog/category/c-language.html">C Language</a>
Tagged:
<a href="http://hsiangholin.github.io/blog/tag/c.html">C </a>
</p>
<p><a href="http://hsiangholin.github.io/blog/posts/2014/02/how-many-characters-are-read.html#disqus_thread">comments</a></p> </article>
<hr class="gradient"/>
<article>
<a href="http://hsiangholin.github.io/blog/posts/2014/01/crzinput.html"><h3 class="article-title">CrzInput: Spelling Correction with Gesture</h3></a>
<h6 class="subheader" title="2014-01-11T14:42:00">Sat 11 January 2014
</h6>
<p>One day I was suddenly curious about how Apple implemented the English auto-correction feature so I started wondering that might be something with Viterbi Algorithm. Then I went on github for some search, then I found this repo, <a href="https://github.com/HsiangHoLin/crzinput">CrzInput</a>. And the effect is similar to <a href="http://fleksy.com/">Flesky</a>.</p><p class="subheader">Category: <a href="http://hsiangholin.github.io/blog/category/algorithm.html">Algorithm</a>
Tagged:
<a href="http://hsiangholin.github.io/blog/tag/c.html">C </a>
<a href="http://hsiangholin.github.io/blog/tag/algorithm.html">Algorithm </a>
</p>
<p><a href="http://hsiangholin.github.io/blog/posts/2014/01/crzinput.html#disqus_thread">comments</a></p> <a class="button radius secondary small right" href="http://hsiangholin.github.io/blog/posts/2014/01/crzinput.html">Read More</a>
<hr class="gradient"/>
</article>
<article>
<a href="http://hsiangholin.github.io/blog/posts/2013/11/usaco-broken-necklace.html"><h3 class="article-title">USACO Section 1.1 Broken Necklace</h3></a>
<h6 class="subheader" title="2013-11-30T14:42:00">Sat 30 November 2013
</h6>
<p>Let's describe the problem briefly here. Given a string a necklace composed with Red, Blue and White beans, we are going to find the maximum number of beans we can collected if we can choose to break the necklace at a certain point. By "maximum number of beans we can collected", I mean starting from the break point, we collect beads of the same color from one end until you reach a bead of a different color, and do the same for the other end.</p><p class="subheader">Category: <a href="http://hsiangholin.github.io/blog/category/algorithm.html">Algorithm</a>
Tagged:
<a href="http://hsiangholin.github.io/blog/tag/c.html">C </a>
<a href="http://hsiangholin.github.io/blog/tag/usaco.html">USACO </a>
<a href="http://hsiangholin.github.io/blog/tag/algorithm.html">Algorithm </a>
</p>
<p><a href="http://hsiangholin.github.io/blog/posts/2013/11/usaco-broken-necklace.html#disqus_thread">comments</a></p> <a class="button radius secondary small right" href="http://hsiangholin.github.io/blog/posts/2013/11/usaco-broken-necklace.html">Read More</a>
<hr class="gradient"/>
</article>
<!-- /#posts-list -->
<div class="pagination-centered">
<h6 class="subheader">Page 1 of 1</h6>
<p>
</p>
</div>
</div>
<!-- End Main Content -->
<!-- Sidebar -->
<aside class="large-3 columns">
<h5 class="sidebar-title">Site</h5>
<ul class="side-nav">
<li><a href="http://hsiangholin.github.io/blog/archives.html">Archives</a>
<li><a href="http://hsiangholin.github.io/blog/tags.html">Tags</a>
</ul>
<h5 class="sidebar-title">Categories</h5>
<ul class="side-nav">
<li><a href="http://hsiangholin.github.io/blog/category/misc.html">MISC</a></li>
<li><a href="http://hsiangholin.github.io/blog/category/c-language.html">C Language</a></li>
<li><a href="http://hsiangholin.github.io/blog/category/algorithm.html">Algorithm</a></li>
<li><a href="http://hsiangholin.github.io/blog/category/about.html">About</a></li>
</ul>
<h5 class="sidebar-title">Links</h5>
<ul class="side-nav">
<li><a href="http://github.com">Github</a></li>
<li><a href="http://www.ntu.edu.tw">National Taiwan University</a></li>
</ul>
</aside> <!-- End Sidebar -->
</div> <!-- End Main Content and Sidebar -->
<!-- Footer -->
<footer class="row">
<div class="large-12 columns">
<hr />
<div class="row">
<div class="large-6 columns">
<p>Programming Journey by Hsiang-Ho Lin</p>
</div>
</div>
</div>
</footer>