Read Integer value from file and write into another file

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 pointer
    FILE *fptrWrite;

    // open the file in write mode
    fptrWrite = 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 occurred
        return -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(bufferfptrWrite);
        }
        printf("\nEnd of file.\n");

        //close files
        fclose(readFile);
        fclose(fptrWrite);
    }
    return 0;
}
Input File: integers.txt
1
2
3
4
5
6
7
8
9
10
Output File: integersWrite.txt
1
2
3
4
5
6
7
8
9
10

Output:

File created successfully!

Read Numbers from file:

End of file.


No comments:

Post a Comment