have a very simple question:
how can i write something into a .txt file?
could write only few lines of code and got stuck. have no idea what to do next. can someone help?

#include <stdio.h>

int main(){
	FILE *textfile;
	textfile = fopen("test.txt", "w");
	return 0;
8 Years
Discussion Span
Last Post by ahspats

Most of the functions in the stdio library will take a pointer to FILE. scanf and printf assume stdin and stdout, respectively, so you might not have had cause to jump to something like fscanf and fprintf. The idea is to open the file and close the file, but in between use it just as you would stdin, stdout, or stderr:

#include <stdio.h>

int main ( void )
  FILE *out = fopen("myfile", "w");

  /* fopen returns NULL if it can't open the file */
  if ( out != NULL ) {
    char buffer[BUFSIZ];

    /* Read from stdin and write to out */
    while ( fgets ( buffer, sizeof buffer, stdin ) != NULL )
      fputs ( buffer, out );
    /* Always close the file! */
    fclose ( out );

  return 0;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.