Note that there are some explanatory texts on larger screens.

plurals
  1. POStruggling to understand file pointers?
    primarykey
    data
    text
    <p>Main description of the problem below, where it happens. But simply, I cannot figure out why I get error messages after asking </p> <pre><code>if (outf!=NULL){ printf("Output file already exists, overwrite (y/n):"); scanf("%c",yn); } </code></pre> <p>Where outf is a file pointer to an existing file. Please read description halfway through code.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;float.h&gt; #include &lt;string.h&gt; int main() { /* Declare file pointer */ FILE *inf; FILE *outf; int linenumber,linecounter=0,linepresent; char filename[21]; char detail[21]; char linedetail[21]; char outfilename[21]; char letter,yn='y'; int position; /*INPUT DETAILS Ask user for file name and line number*/ printf("Please enter an input filename and a linenumber: "); //scan filename to char string and line number to int variable scanf("%s %i",&amp;filename,&amp;linenumber); /*OUTPUT DETAILS Ask user for file name, letter &amp; position, etc*/ printf("Please enter an output filename, a letter and a position:"); scanf("%s %c %i",&amp;outfilename,&amp;letter,&amp;position); /* Open file for reading */ inf=fopen (filename,"r"); outf=fopen(outfilename,"r"); /*check that file exists*/ if (inf!=NULL) { </code></pre> <p>Up until here everything works fine! Then I try to find out if the outf file already exists. If outf points to an existing file, it DOES print "Output file already exists, overwrite (y/n):"</p> <p>HOWEVER, as soon as it prints this I get error windows opening! This is probably an extremely rookie mistake - I'm still learning C. If there is no such file the program completes normally and bypasses the if statement okay.</p> <pre><code> if (outf!=NULL){ printf("Output file already exists, overwrite (y/n):"); scanf("%c",yn); } if (yn=='y'){ /*keep reading to end of file*/ while (feof(inf)==0) { linecounter++; /*read each line and store the line number in detail[WORDS GO HERE]*/ fscanf (inf,"%s", &amp;detail); /*If we reach the line selected by the user*/ if (linecounter==linenumber){ strcpy(linedetail,detail); linepresent=1; } } if (linepresent==0) { printf("File only contains %i lines",linecounter); } } else { exit(1); } } else { printf("Input file not found"); } printf("%s",linedetail); /* close the file */ fclose(inf); fclose(outf); return (0); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload