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!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.