write a c program to copy array of 5 integers into a file using fprintf with electro4u.net

24 Dec 2022 Balmiki Mandal 0 C Programming

Efficient Array Copying in C: Learn to Use fprintf for File Handling

"Write a C program to copy an array of 5 integers into a file using fprintf" means that you need to write a program in the C programming language that copies an array of 5 integers into a file using the fprintf function.

The program should create an integer array of size 5, fill it with some values, and then open a file in write mode using the fopen function. Then, the program should use the fprintf function to write each element of the array to the file, separated by a space. Finally, the program should close the file using the fclose function.

The fprintf function is used to write formatted data to a file. It takes a file pointer as its first argument, followed by a format string that specifies how to format the output and additional arguments that provide the data to be formatted. In this case, we will use %d a format specifier to print integer values.

C program to copy an array of 5 integers into a file using fprintf

#include<stdio.h>
void main()
{
    int a[5]={10,20,30,40,50};
    int i;
    FILE *fp=fopen("data","w");
    for(i=0;i<5;i++)
    fprintf(fp,"%d",a[i]);
}

When the program runs, it will create a new file named "data" and write the following output to the file:

10
20
30
40
50

You can then open the file "data" in a text editor to view the output.

Reverse  c program to copy an array of 5 integers into a file using fprintf

#include<stdio.h>
void main()
{
    int a[5];
    int i;
    FILE *fp=fopen("data","r");
    for(i=0;i<5;i++)
    fscanf(fp,"%",&a[i]);
    for(i=0;i<5;i++)
    printf("%d",a[i]);
    printf("\n");
}

When the program runs, it will open the file "data" and read the following input from the file:

10
20
30
40
50

The program will then store the values in the array a[]. Finally, the program will print the values of the array a[] to the console.

Note: If the file "data" does not exist or does not contain the correct number of integers, the program will produce unexpected results.

C program that copies an array of 5 integers into a file using the fprintf function:

#include<stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    FILE *fp;
    int i;

    fp = fopen("output.txt", "w");

    if (fp == NULL) {
        printf("Error opening file\n");
        return 1;
    }

    for (i = 0; i < 5; i++) {
        fprintf(fp, "%d ", arr[i]);
    }

    fclose(fp);

    return 0;
}

When the program runs, it will create a new file named "output.txt" and write the following output to the file:

1 2 3 4 5

You can then open the file "output.txt" in a text editor to view the output.

In this program, we first declare an integer array arr of size 5 and initialize it with some values. We then declare a FILE pointer fp that we will use to access the file we want to write to.

Next, we use the fopen function to open a file named output.txt in write mode ("w"). If fopen returns NULL, we print an error message and exit the program with an error code.

Inside the for loop, we use the fprintf function to write each element of the arr array to the file, separated by a space. The first argument of fprintf is the file pointer fp, followed by the format string that specifies how to format the output. In this case, we use the %d format specifier to print each integer value.

Finally, we close the file using fclose and return 0 to indicate that the program ran successfully.

Further Reading:

For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]


Top Resources

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.