Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can I access txt file in C?

+10 votes
asked Nov 10, 2022 by Jay Chen (220 points)
I tried to access a file in C by using fopen but it is not working for me.

2 Answers

0 votes
answered Nov 10, 2022 by Peter Minarik (86,160 points)

Can you share the code you used? See here how it's supposed to work.

0 votes
answered Dec 21, 2022 by Ericsson Lin (600 points)
I think you may have used the wrong function.

You should use freopen.

It takes three parameters: the file name as a string literal, what you want to do to the file, and the stream.

You can find more specific documentation here: https://cplusplus.com/reference/cstdio/freopen/

For example, if you want to read input from a file called a.in:

freopen("a.in", "r", stdin);
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...