Ficheiros
Ficheiros
Ficheiros
-
- remove rename
2
•
•
•
•
3
• FILE *
• FILE stdio.h
•
• FILE *fp1, *fp2;
4
• stdio.h
stdio.h
5
• stdio.h
•
•
•
•
• EOF
•
•
6
•
• 32767
3276 7
7
#include <stdio.h>
#include <stdlib.h>
if (fp == NULL) {
• filename
exit(EXIT_FAILURE);
}
• mode
return 0;
8
“r” fopen() NULL
“w”
“a”
“w+”
“a+”
“wb”
“ab”
“w+b” ou “wb+”
“a+b” ou “ab+”
9
• fopen
\
• fopen("c:\estgf\test1.dat", "r") \t
• \\ \
fopen("c:\\folder\\test.dat", "r")
• / \
fopen("c:/folder/test.dat", "r")
10
#include <stdio.h>
#include <stdlib.h>
int main() {
if (fp == NULL) {
• exit(EXIT_FAILURE);
• 0 }
• EOF
printf(“Aberto com sucesso %s", FILENAME);
fclose(fp);
return 0;
11
#include <stdio.h>
#include <stdlib.h>
int main() {
char ch;
int fgetc(FILE *stream)
FILE *fp = fopen(FILENAME, "r");
if (fp == NULL) {
• exit(EXIT_FAILURE);
• }
• EOF
while ((ch = getc(fp)) != EOF) {
putchar(ch);
}
fclose(fp);
return 0;
}
12
#include <stdio.h>
#include <stdlib.h>
•
if (fgets(str, STR_SIZE, fp) != NULL) {
• str
puts(str);
}
• NULL
fclose(fp);
return 0;
}
13
#include <stdio.h>
#include <stdlib.h>
• if (fp == NULL) {
exit(EXIT_FAILURE);
•
}
• 0
fscanf(fp, "%s %d", str, &num);
• EOF
printf("%s %d", str, num);
fclose(fp);
return 0;
}
14
#include <stdio.h>
#include <stdlib.h>
int main() {
int fputc(int character, char letra;
FILE *stream)
FILE *fp = fopen(FILENAME, "w");
if (fp == NULL) {
• exit(EXIT_FAILURE);
}
• EOF
for (letra = 65; letra < 91; letra++) {
fputc(letra, fp);
}
fclose(fp);
return 0;
}
15
#include <stdio.h>
#include <stdlib.h>
fclose(fp);
return 0;
}
16
#include <stdio.h>
#include <stdlib.h>
•
if (fprintf(fp, "%s %d", "test", 1) < 0) {
•
puts("Aconteceu um erro");
}
•
fclose(fp);
return 0;
}
17
#include <stdio.h>
#include <stdlib.h>
int main() {
char ch;
int feof(FILE *stream)
FILE *fp = fopen(FILENAME, "r");
if (fp == NULL) {
•
exit(EXIT_FAILURE);
• 0
}
• 0
while ((ch = getc(fp)) && !feof(fp)) {
putchar(ch);
}
fclose(fp);
return 0;
}
18
#include <stdio.h>
#include <stdlib.h>
int main() {
char ch;
• EOF }
rewind(fp);
fclose(fp);
return 0;
}
19
#include <stdio.h>
#include <stdlib.h>
• int main() {
FILE *fp = fopen(FILENAME, "r");
if (fp == NULL) {
• 0
exit(EXIT_FAILURE);
}
20
•
•
•
•
•
21
#include <stdio.h>
#include <stdlib.h>
int main() {
ALUNO alunos[ALUNOS_MAX] = {
• ptr {.numero = 1, .media = 10},
{.numero = 2, .media = 15},
{.numero = 3, .media = 12}
• size };
• count
FILE *fp = fopen(FILENAME, “wb+");
• stream if (fp == NULL) {
exit(EXIT_FAILURE);
• }
•
fwrite(alunos, sizeof(ALUNO), ALUNOS_MAX, fp);
• count
fclose(fp);
return 0;
}
22
#include <stdio.h>
size_t count,
typedef struct {
int main() {
• ptr int i, num_alunos;
23
#include <stdio.h>
#include <stdlib.h>
int fseek(FILE *file_ptr,
#define FILENAME "meu-ficheiro.bin"
long numbytes, #define ALUNOS_MAX 3
• SEEK_SET }
24
#include <stdio.h>
int main() {
• old_fn new_fn int returnValue;
• 0
returnValue = rename(FILENAME, NEW_FILENAME);
remove if (returnValue == 0) {
rename }
printf(“Renomeado com sucesso.");
return 0;
}
25
•
26