Opening and Reading files fails

<p>I am currently trying to read a file and count the number of instances of a user-specified string in an input file and output them to another file,.</p><p>However when I try to open the file, <code>fopen</code> returns <code>NULL</code>. Here's what I have so far:</p><pre><br /><code>#include <stdio.h><br />#include <stdlib.h><br />#include <string.h><br /><br /><br />int main(int argc, char **argv)<br />{<br /> char in[50];<br /> char out[50];<br /> char target[50];<br /><br /> getIns(in,out,target);<br /> Search(in, out, target);<br /> //printf("string entered: %s\n%s\n%s", in, out, target);<br /><br /><br /><br /> return 0;<br />}<br /><br />int getIns(char *i, char *o, char *t)<br />{<br /> printf("please enter name of input file you wish to search: \n -i ");<br /> fgets(i, 50, stdin);<br /><br /> printf("please enter name of output file you wish to write to: \n -o ");<br /> fgets(o,50,stdin);<br /><br /> printf("Please enter the string you wish to search for \n -c ");<br /> fgets(t, 50, stdin);<br /><br /> return 1;<br />}<br /><br /><br />int Search(char *i, char *o, char *t)<br />{<br /> char*p;<br /> int c = 0;<br /> int start;<br /> char *data = NULL;<br /> FILE*f;<br /><br /> f = fopen(i, "r");<br /><br /> if (f == NULL)<br /> {<br /> printf("file not found \n Quitting...");<br /> exit(1);<br /> } <br /><br /> while(!feof(f))<br /> {<br /> fgets(data, sizeof(data), f);<br /> p = strstr(data,t);<br /> while (p != NULL)<br /> {<br /> c++;<br /> p = strstr(p , t);<br /> }<br /> }<br /><br /> if (c == 0)<br /> {<br /> printf("String not in file\n");<br /> }<br /> if (c > 0)<br /> {<br /> printf("word: %s found: %d times\n", t, c);<br /> }<br /><br /> fclose(f);<br /><br /> return 1;<br />}<br /></code><br /></pre><hr /><p><strong>Edit:</strong></p><p>I've made some changes to the code following the responses and now, my program crashes on reading the file. New code:</p><pre><br /><code>#include <stdio.h><br />#include <stdlib.h><br />#include <string.h><br /><br />int Search(char *i, char *o, char *t)<br />{<br /> char*p;<br /> int c = 0;<br /> int start;<br /> char data[1024];<br /> FILE*f;<br /><br /> f = fopen(i, "r");<br /><br /> if (f == NULL)<br /> {<br /> printf("file not found \n Quitting...");<br /> exit(1);<br /> } <br /><br /> while(fgets(data, sizeof(data), f))<br /> {<br /> fgets(data, sizeof(data), f);<br /> p = strstr(data,t);<br /><br /> while (p != NULL)<br /> c++; p = strstr(p+1 , t);<br /> }<br /><br /> if (c == 0)<br /> {<br /> printf("String not in file\n");<br /> }<br /> if (c > 0)<br /> {<br /> printf("word: %s found: %d times\n", t, c);<br /> }<br /><br /> fclose(f);<br /><br /> return 1;<br />}<br /><br />int getIns(char *i, char *o, char *t)<br />{<br /> printf("please enter name of input file you wish to search: \n -i ");<br /> fgets(i, 50, stdin); i[strcspn(i, "\n")] = 0;<br /><br /> printf("please enter name of output file you wish to write to: \n -o ");<br /> fgets(o, 50, stdin); o[strcspn(o, "\n")] = 0;<br /><br /> printf("Please enter the string you wish to search for \n -c ");<br /> fgets(t, 50, stdin); t[strcspn(t, "\n")] = 0;<br /><br /> return 1;<br />}<br /><br />int main(int argc, char **argv)<br />{<br /> char in[50];<br /> char out[50];<br /> char target[50];<br /><br /> getIns(in,out,target);<br /> Search(in, out, target);<br /><br /> return 0;<br />}<br /></code><br /></pre><br /><h2>Click here view more answers <a href="http://ift.tt/1KG77s9">http://ift.tt/1KG77s9</a></h2>

Nhận xét

Bài đăng phổ biến từ blog này

Align div to bottom of the page in bootstrap

Align elements inside an tag vertically

Javascript is unable to stop page loading