You are on page 1of 3

Elemente de baz ale programrii n C - Aplicaii de Laborator

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

reads a byte/wchar_t string from a file stream


writes a byte/wchar_t to a file stream

fputws
getwchar
N/A

putchar

putwchar

writes a byte/wchar_t string to a file stream


reads a byte/wchar_t from stdin
reads a byte string from stdin (deprecated in
C99, obsoleted in C11)
writes a byte/wchar_t to stdout
68

MatrixRom 2013

ISBN: 978-973-755-897-8

Elemente de baz ale programrii n C - Aplicaii de Laborator

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

writes a byte string to stdout


puts a byte/wchar_t back into a file stream
reads formatted byte/wchar_t input from stdin,
a file stream or a buffer
reads formatted input byte/wchar_t from stdin,
a file stream or a buffer using variable argument
list
prints formatted byte/wchar_t output to stdout,
a file stream or a buffer

prints formatted byte/wchar_t output to stdout,


a file stream, or a buffer using variable argument
list
returns the current file position indicator
gets the file position indicator
moves the file position indicator to a specific
location in a file
moves the file position indicator to a specific
location in a file
moves the file position indicator to the
beginning in a file
clears errors
checks for the end-of-file
checks for a file error
displays a character string corresponding of the
current error to stderr
erases a file
renames a file
returns a pointer to a temporary file
returns a unique filename

Exemplu de problem

Problema 9.1.
#include <stdio.h>
69
MatrixRom 2013

ISBN: 978-973-755-897-8

Elemente de baz ale programrii n C - Aplicaii de Laborator

#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

9.2.1. Care este fiierul standard de intrare?


9.2.2. Unde se gsesc fiierele standard de intrare/ieire?
9.2.3. Scriei dou programe care s foloseasc funcii de intrare/ieire pentru fiiere.

70
MatrixRom 2013

ISBN: 978-973-755-897-8

You might also like