mor
mor

Reputation: 1

How do I read from severals files in C?

I am working on a program which have to read from txt files. I know that there's a function called fopen("myfile.txt","rt"), but what if I have 10 files? Do i need to call the function 10 times (a call for every file)?

Upvotes: 0

Views: 87

Answers (2)

darioo
darioo

Reputation: 47183

Yes. One function call per file is needed.

Upvotes: 0

Matt Joiner
Matt Joiner

Reputation: 118500

Yes. But if you perform the same routines on each of those functions, abstract that behaviour into a function that accepts the name of a file. Now call that function 10 times, once with each file name.

void read_from_text_file(char const *filepath);

read_from_text_file("myfile.txt");
read_from_text_file("myfile2.txt");
...

This is a core concept in computer science. Buzzwords include "abstraction", "routine", "reusability", etc.

Upvotes: 2

Related Questions