Question

You are required to open a file for input and read the data in that file...

You are required to open a file for input and read the data in that file into an array (named data). Use the variable size to count how many elements in the file. Create the file yourself in notepad and call it data.txt so that it has the following numbers in order: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0

Please help with this. In C please

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "r");
    if (fp) {
        int data[1000], size = 0, num, i;
        while (fscanf(fp, "%d", &num) != EOF) {
            data[size++] = num;
        }
        // print data in array
        printf("Numbers read from file are:\n");
        for (i = 0; i < size; ++i) {
            printf("%d\n", data[i]);
        }
        fclose(fp);
    } else {
        printf("data.txt does not exists!\n");
    }
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
You are required to open a file for input and read the data in that file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT