Write a program to Read Integer value from the file and write into another file.
C Program
#include <stdio.h>int main(){FILE *readFile;int num;char buffer[32] = {0};// creating a FILE pointerFILE *fptrWrite;// open the file in write modefptrWrite = fopen("integersWrite.txt", "w");if (fptrWrite != NULL){printf("File created successfully!\n");}else{printf("Failed to create the file.\n");// exit status for OS that an error occurredreturn -1;}readFile = fopen("integers.txt", "r");if (readFile == NULL){printf("Can't open file for integers.txt file.\n");return -1;}else{printf("\nRead Numbers from file:\n");while (!feof(readFile)){fscanf(readFile, "%d", &num);sprintf(buffer, "%d\n", num);fputs(buffer, fptrWrite);}printf("\nEnd of file.\n");//close filesfclose(readFile);fclose(fptrWrite);}return 0;}
Input File: integers.txt
12345678910
Output File: integersWrite.txt
12345678910
Output:
File created successfully!
Read Numbers from file:
End of file.
No comments:
Post a Comment