Jump to content

Recommended Posts

Posted (edited)

Boas, a minha dúvida consiste:

- O programa deve pedir ao utilizador para fornecer o nome do ficheiro. Depois, deve abrir esse ficheiro e ler cada linha para um elemento de uma lista de linhas de texto.

Alguém que me possa ajudar nisto?

Edited by Nuno Alexandre
  • 3 weeks later...
Posted

Boas tardes,

Será isto que queres:

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

int main() {
 char ch, file_name[25]; /*lsferreira programming */
 FILE *fp;
 printf("NAME OF FILE?: \n");
 gets(file_name);
 fp=fopen(file_name,"r");
 if (fp==NULL) {
   perror("ERROR WHILE OPENING THE FILE\n");
   exit(EXIT_FAILURE);
 }
 printf("FILE &s HAVE:\n\n", file_name);
 while((ch=fgetc(fp))!= EOF)
   printf("%c",ch);
 fclose(fp);
 return 0;
}

Cumps 😉

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.