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;
}

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;
}

Thanx Narue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.