Syntax

C Open a File Syntax

In C, open a file: opens, writes and closes a file — FILE *f = fopen("out.txt", "w"); fprintf(f, "hi\n"); fclose(f);

Quick Answer

Syntax
FILE *f = fopen("out.txt", "w"); fprintf(f, "hi\n"); fclose(f);
CSyntax

Open a File

Code example
FILE *f = fopen("out.txt", "w"); fprintf(f, "hi\n"); fclose(f);

Opens, writes and closes a file.

Learn More

How to open a file in C

  1. Open your C file or editor.
  2. Copy the example, then replace its sample names and values for your code.

C Open a File Syntax

Use FILE *f = fopen("out.txt", "w"); fprintf(f, "hi\n"); fclose(f);.

Related Syntax References

Frequently asked questions

What is the C syntax for open a file?

Use this pattern: FILE *f = fopen("out.txt", "w"); fprintf(f, "hi\n"); fclose(f);

Can I copy this C code example?

Yes. Use Copy code and adapt the example to your program.

Is this C syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify