Skip to content

Instantly share code, notes, and snippets.

@kgashok
Last active December 5, 2016 17:56
Show Gist options
  • Select an option

  • Save kgashok/6cc2371012382cc610e3e3a177bed044 to your computer and use it in GitHub Desktop.

Select an option

Save kgashok/6cc2371012382cc610e3e3a177bed044 to your computer and use it in GitHub Desktop.
fileOperations.md
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>fileOperations.md</title>
<link rel="stylesheet" href="https://stackedit.io/res-min/themes/base.css" />
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body><div class="container"><h3 id="table-of-contents">Table of Contents</h3>
<p><div class="toc">
<ul>
<li><ul>
<li><ul>
<li><a href="#table-of-contents">Table of Contents</a></li>
<li><a href="#read-from-one-file-and-write-to-another-file">Read from one file and write to another file</a></li>
<li><a href="#count-number-of-lines-in-a-file">Count number of lines in a file</a></li>
<li><a href="#find-and-replace-tabs-with-spaces">“Find and Replace” tabs with spaces</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</p>
<h3 id="read-from-one-file-and-write-to-another-file">Read from one file and write to another file</h3>
<pre class="prettyprint"><code class="language-c hljs ">
<span class="hljs-preprocessor">#include &lt;stdio.h&gt;</span>
<span class="hljs-keyword">int</span> main(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span>** argv) {
<span class="hljs-comment">/* STEP 1 */</span>
<span class="hljs-comment">//open the necessary file pointers</span>
FILE* fileIn = fopen(argv[<span class="hljs-number">1</span>], <span class="hljs-string">"r"</span>);
FILE* fileOut = fopen(argv[<span class="hljs-number">2</span>], <span class="hljs-string">"w"</span>);
<span class="hljs-comment">/* STEP 2 */</span>
<span class="hljs-comment">// read from 'fileIn' and write to 'fileOut'</span>
<span class="hljs-keyword">char</span> c;
<span class="hljs-keyword">while</span>((c = fgetc(fileIn)) != EOF) {
fputc (c, fileOut);
}
<span class="hljs-comment">/* STEP 3 */</span>
<span class="hljs-comment">// clean up by closing all the file pointers</span>
fflush(fileOut);
fclose(fileOut);
fclose(fileIn);
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}</code></pre>
<h3 id="count-number-of-lines-in-a-file">Count number of lines in a file</h3>
<pre class="prettyprint"><code class="language-c hljs "><span class="hljs-preprocessor">#include&lt;stdio.h&gt;</span>
<span class="hljs-keyword">void</span> main(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span>** argv)
{
FILE *fp;
<span class="hljs-keyword">char</span> ch;
<span class="hljs-keyword">int</span> character = <span class="hljs-number">0</span>, space = <span class="hljs-number">0</span>, tab = <span class="hljs-number">0</span>, line = <span class="hljs-number">0</span>;
fp = fopen(argv[<span class="hljs-number">1</span>],<span class="hljs-string">"r"</span>);
<span class="hljs-keyword">if</span>(fp == NULL) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"File Not Found\n"</span>);
<span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
}
<span class="hljs-keyword">else</span> {
<span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) {
ch = fgetc(fp);
<span class="hljs-keyword">if</span>(ch == EOF)
<span class="hljs-keyword">break</span>;
character++;
<span class="hljs-keyword">if</span>(ch == <span class="hljs-string">' '</span>)
space++;
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(ch == <span class="hljs-string">'\t'</span>)
tab++;
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(ch == <span class="hljs-string">'\n'</span>)
line++;
}
}
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nNumber of Characters = %d\n"</span>, character);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nNumber of Tabs = %d\n"</span>, tab);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nNumber of New Lines = %d\n"</span>, line);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nNumber of Spaces = %d\n"</span>, space);
<span class="hljs-comment">// Using fseek and ftell to find size of file</span>
fseek(fp, <span class="hljs-number">0</span>, SEEK_END); <span class="hljs-comment">// SEEK_SET, SEEK_CUR are other options</span>
<span class="hljs-keyword">int</span> len = ftell(fp);
fclose(fp);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nSize of file = %d\n"</span>, len);
}
</code></pre>
<h3 id="find-and-replace-tabs-with-spaces">“Find and Replace” tabs with spaces</h3>
<p>This is an excellent example for use of <code>fseek</code> and <code>ftell</code> to accomplish random access into a file to replace all tabs into spaces. </p>
<pre class="prettyprint"><code class="language-c hljs "><span class="hljs-preprocessor">#include &lt;stdio.h&gt;</span>
<span class="hljs-keyword">int</span> main(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span>** argv)
{
FILE* filePtr = fopen(argv[<span class="hljs-number">1</span>], <span class="hljs-string">"r+"</span>);
<span class="hljs-keyword">char</span> c;
<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>, j = <span class="hljs-number">0</span>;
<span class="hljs-keyword">int</span> loc[<span class="hljs-number">100</span>]; <span class="hljs-comment">// to remember where tabs were found</span>
<span class="hljs-keyword">while</span>((c = fgetc(filePtr)) != EOF) {
<span class="hljs-keyword">if</span>(c == <span class="hljs-string">'\t'</span>) {
loc[j++] = ftell(filePtr)-<span class="hljs-number">1</span>;
}
}
<span class="hljs-comment">// print the offsets in the file where </span>
<span class="hljs-comment">// tabs were located </span>
<span class="hljs-keyword">int</span> k;
<span class="hljs-keyword">for</span> (k=<span class="hljs-number">0</span>; k &lt; j;k++) <span class="hljs-built_in">printf</span> (<span class="hljs-string">"%d "</span>, loc[k]);
<span class="hljs-built_in">printf</span> (<span class="hljs-string">"\n"</span>);
<span class="hljs-comment">// replace all tabs with spaces </span>
<span class="hljs-keyword">for</span> (k = <span class="hljs-number">0</span>; k &lt; j; k++) {
fseek (filePtr, loc[k], <span class="hljs-number">0</span>);
fputc (<span class="hljs-string">' '</span>, filePtr);
}
fflush(filePtr);
fclose(filePtr);
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}</code></pre></div></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment