Professional Documents
Culture Documents
Laborator nr. 9
Tipuri definite de utilizator
Sistemul I/O ofer utilizatorului trei "fiiere" standard de lucru:
fiierul standard de intrare (stdin);
fiierul standard de ieire (stdout);
fiierul standard de afiare a mesajelor (stderr).
Pentru a se putea face o referire la aceste fiiere orice program C trebuie s conin
fiierul stdio.h, care se include printr-o linie de forma:
#include <stdio.h>
dac acest fiier se afl n biblioteca standard.
Funciile pentru fiiere sunt sintetizate n tabelul de mai jos, aa cum pot fi gsite n
sistemul de asistent (help):
Description
Wide
character
opens a file
opens a different file with an existing stream
synchronizes an output stream with the actual
file
closes a file
sets the buffer for a file stream
sets the buffer and its size for a file stream
switches a file stream between wide character
I/O and narrow character I/O
reads from a file
writes to a file
Fgetwc getwc
reads a byte/wchar_t from a file stream
Byte character
File access
fopen
freopen
fflush
fclose
setbuf
setvbuf
fwide
Direct
input/output
Unformatted
input/output
fread
fwrite
fgetc
getc
fgets
fputc
putc
fputs
getchar
gets
fgetws
Fputwc putwc
fputws
getwchar
N/A
putchar
putwchar
MatrixRom 2013
ISBN: 978-973-755-897-8
Formatted
input/output
File
positioning
puts
ungetc
scanf
fscanf
sscanf
vscanf
vfscanf
vsscanf
printf
fprintf
sprintf
snprintf
vprintf
vfprintf
vsprintf
vsnprintf
ftell
fgetpos
fseek
fsetpos
rewind
Error
handling
clearerr
feof
ferror
perror
Operations
on files
remove
rename
tmpfile
tmpnam
9.1.
N/A
ungetwc
wscanf
fwscanf
swscanf
vwscanf
vfwscanf
vswscanf
wprintf
fwprintf
swprintf
vwprintf
vfwprintf
vswprintf
Exemplu de problem
Problema 9.1.
#include <stdio.h>
69
MatrixRom 2013
ISBN: 978-973-755-897-8
#include <stdlib.h>
void main( void )
{
char buffer[5] = {0}; /* initialized to zeroes */
int i, rc;
FILE *fp = fopen("myfile", "rb");
if (fp == NULL) {
perror("Failed to open file \"myfile\"");
}
for (i = 0; (rc = getc(fp)) != EOF && i < 5; buffer[i++] = rc) { }
fclose(fp);
if (i == 5)
{
puts("The bytes read were...");
printf("%x %x %x %x %x\n", buffer[0], buffer[1], buffer[2], buffer[3],
buffer[4]);
}
else
fputs("An error occurred while reading the file.\n", stderr);
}
9.2.
Probleme
70
MatrixRom 2013
ISBN: 978-973-755-897-8