Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is my code. #include <stdio.h> #include <stdlib.h> int main() { FILE *inFil

ID: 3534745 • Letter: H

Question

Here is my code.


#include <stdio.h>

#include <stdlib.h>


int main()

{

FILE *inFile;

char fileName[15];


printf(" Enter a file name: ");

gets(fileName);

inFile = fopen(fileName, "r");

if (inFile == NULL) perror ("Error opening file");

else

{

while ( ! feof (inFile) )

{

if ( fgets (fileName , 15 , inFile) == NULL ) break;

fputs (fileName , stdout);

}


fclose (inFile);

}

return 0;

}


I also get an error, Run-Time Check Failure #2 - Stack around variable 'fileName' was corrupted after I run it without debugging.

Explanation / Answer

Works great. Checked it

Make a file name hi.txt


write your name inside. and some more contents.

the file and this C file should be in same directory.

Run this C code.

enter file name - hi.txt

output will be your content os hi.txt

You can code the take from below or http://ideone.com/ajbWlI

Prefer taking from the link. There will be no errors.



#include <stdio.h>

#include <stdlib.h>

int main()

{

FILE *inFile;

char fileName[15];

printf(" Enter a file name: ");

gets(fileName);

inFile = fopen(fileName, "r");

if (inFile == NULL) perror ("Error opening file");

else

{

while ( ! feof (inFile) )

{

if ( fgets (fileName , 15 , inFile) == NULL ) break;

fputs (fileName , stdout);

}

fclose (inFile);

}

return 0;

}


Dont forget to rate.


Cheers!!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote