Jump to content

Recommended Posts

Posted

Boa tarde.

#include <stdio.h>
#include <io.h>
#include <stdlib.h>

int main(void)
{
  FILE *fp;
  char s[80];
  int t;

  if((fp=fopen("test", "w")) == NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }

  printf("Enter a string and a number: ");
  fscanf(stdin, "%s%d", s, &t); /* read from keyboard */

  fprintf(fp, "%s %d", s, t); /* write to file */
  fclose(fp); 

  if((fp=fopen("test","r")) == NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }

  fscanf(fp, "%s%d", s, &t); /* read from file */
  fprintf(stdout, "%s %d", s, t); /* print on screen */

  return 0;
}

Tenho este codigo mas cada vez eu abro o pograma ele substitui o que tem por eu meter de novo por exemplo queria:

introduzir a primeira vez

ola 123

introduzir segunda vez

adeus 456

ou seja ficar

ola 123

adeus 456

alguma ideia?

Posted

ja resolvi obrigado

fp = fopen ( "test", "r" );   
if ( fp != NULL )   
{
    char line [ 10 ];      
    while ( fgets ( line, sizeof line, fp ) != NULL )
    fputs ( line, stdout ); 
}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.