0% found this document useful (0 votes)
82 views

Lecture 16 PDF

This lecture covered various methods of file input/output (I/O) in C, including reading and writing characters, lines, and formatted data. It discussed opening and closing files, reading and writing one character at a time using fgetc and fputc, reading and writing entire lines using fgets and fputs, checking for end of file using feof, and formatted I/O using fprintf and fscanf. Additional topics included binary stream I/O using fread and fwrite, navigating within files using fseek and ftell, and low-level file I/O functions like open, close, read and write. Code examples were provided for many of these file I/O methods.

Uploaded by

Benson Wang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Lecture 16 PDF

This lecture covered various methods of file input/output (I/O) in C, including reading and writing characters, lines, and formatted data. It discussed opening and closing files, reading and writing one character at a time using fgetc and fputc, reading and writing entire lines using fgets and fputs, checking for end of file using feof, and formatted I/O using fprintf and fscanf. Additional topics included binary stream I/O using fread and fwrite, navigating within files using fseek and ftell, and low-level file I/O functions like open, close, read and write. Code examples were provided for many of these file I/O methods.

Uploaded by

Benson Wang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ECE 220

Lecture 16

March 15, 2016

File I/O in C
Lecture Topics

File I/O in C
o Stream and buffered I/O concepts
o Creating I/O streams: fopen, fclose
o I/O one char at a time: fgetc, fputc
o I/O one line at a time: fgets, fputs
o End of file: EOF & feof
o Formatted I/O: fprintf, fscanf

Lecture materials

Textbook Ch. 18

Additional topics

SP16

Binary stream I/O: fread, fwrite


Navigation inside a file: fseek, ftell
Low-level file I/O: open, close, read, write

V. Kindratenko

ECE 220

Lecture 16

March 15, 2016

I/O one char at a time: fgetc, fputc


#include <stdio.h>
int main()
{
int c;
FILE *f;
/* write to file */
f = fopen("out.txt", "w");
if (f == NULL)
{
printf("Unable to open file out.txt for writing\n");
return -1;
}
c = getchar();
while (c != '\n')
{
fputc(c, f);
c = getchar();
}
fclose(f);
/* read from file */
if ((f = fopen("out.txt", "r")) == NULL)
{
printf("Unable to open file out.txt for reading\n");
return -1;
}
c = fgetc(f);
while (c != EOF) /* EOF is a macro defined in stdio.h */
{
putchar(c);
c = fgetc(f);
}
putchar('\n');
fclose(f);
return 0;
}

SP16

V. Kindratenko

ECE 220

Lecture 16

March 15, 2016

I/O one line at a time: fgets, fputs


int main()
{
FILE *inf;
FILE *outf;
char buf[32];
if ((inf = fopen("inputfile.txt", "r")) == NULL)
{
printf("Unable to open file inputfile.txt for reading\n");
return -1;
}
if ((outf = fopen("outputfile.txt", "w")) == NULL)
{
printf("Unable to open file outoutfile.txt for writing\n");
return -1;
}
fgets(buf, sizeof(buf), inf);
while (!feof(inf)) /* feof tests the
{
fputs(buf, outf);
fgets(buf, sizeof(buf), inf);
}

end-of-file indicator */

fclose(inf);
fclose(outf);
return 0;
}

SP16

V. Kindratenko

ECE 220

Lecture 16

March 15, 2016

Formatted I/O: fscanf, fprintf


#include <stdio.h>
#define N 100
#define M 100
int main()
{
FILE *inf;
FILE *outf;
int n, m;
int i, j;
int array[N][M];
/* write to file */
if ((inf = fopen("array.txt", "r")) == NULL)
{
printf("Unable to open file array.txt for reading\n");
return -1;
}
fscanf(inf, "%i %i", &n, &m);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
fscanf(inf, "%i", &array[i][j]);
fclose(inf);
if ((outf = fopen("outputfile.txt", "w")) == NULL)
{
printf("Unable to open file outputfile.txt for writing\n");
return -1;
}
fprintf(outf, "%i %i\n", m, n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
fprintf(outf, "%i ", array[j][i]);
fprintf(outf, "\n");
}
fclose(outf);
return 0;
}

SP16

V. Kindratenko

ECE 220

Lecture 16

March 15, 2016

Binary stream I/O: fread, fwrite


#include <stdio.h>
#define N 100
#define M 100
int main()
{
FILE *inf;
FILE *outf;
int n, m;
int i, j;
int array[N][M];
/* write to file */
if ((inf = fopen("array.txt", "r")) == NULL)
{
printf("Unable to open file array.txt for reading\n");
return -1;
}
fscanf(inf, "%i %i", &n, &m);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
fscanf(inf, "%i", &array[i][j]);
fclose(inf);
if ((outf = fopen("outputfile.txt", "w")) == NULL)
{
printf("Unable to open file outputfile.txt for writing\n");
return -1;
}
/* mix ascii and binary mode */
fprintf(outf, "%i %i\n", n, m);
for (i = 0; i < n; i++)
fwrite(array[i], sizeof(int), n, outf);
fclose(outf);
return 0;
}

SP16

V. Kindratenko

You might also like