Answer to Question #41451 in C++ for b.neha reddy

Question #41451
write a C program to create a file and to display the contents and delete the file
1
Expert's answer
2014-05-14T10:08:51-0400
#include <stdio.h>

int main() {
FILE *out = fopen("file.txt", "w+");
FILE *in = fopen("file.txt", "r");

int temp = 0;

for ( int i = 0; i < 15; i++ ) {
fprintf(out, "%d\n", i);// filliing the file with values as we have created it and it contains EOF only
}
for ( int j = 0; j < 15; j++ ) {
fscanf(in, "%d", temp);
printf("%d\n", temp);//showing the contents
}
if( remove( "file.txt" ) != 0 ) {
perror( "Error deleting file" );
} else {
puts( "File successfully deleted" );
}

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS