Sie sind auf Seite 1von 46

Streams

File Handling in C

File Input and Output


CS 11 - Introduction to Computer Programming I

Jan Michael C. Yap


janmichaelyap@gmail.com
Department of Computer Science
University of the Philippines Diliman

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

1 Streams

2 File Handling in C
File Pointer
File I/O
Some Notes

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

Topics

1 Streams

2 File Handling in C
File Pointer
File I/O
Some Notes

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

Streams

http://thumbs.dreamstime.com/z/binary-data-stream-19093118.jpg
JMC Yap CS 11 - File Input and Output
Streams
File Handling in C

More on streams

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

More on streams

Three standard streams

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

More on streams

Three standard streams


Standard input stream

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

More on streams

Three standard streams


Standard input stream
Standard output stream

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

More on streams

Three standard streams


Standard input stream
Standard output stream
Standard error stream

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

More on streams

Three standard streams


Standard input stream
Standard output stream
Standard error stream
Output and error streams are typically associated with the
monitor.

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

More on streams

Three standard streams


Standard input stream
Standard output stream
Standard error stream
Output and error streams are typically associated with the
monitor.
Another type of output/error stream is a file.

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

Kinds of streams

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

Kinds of streams

Text stream

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

Kinds of streams

Binary stream

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Topics

1 Streams

2 File Handling in C
File Pointer
File I/O
Some Notes

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Topics

1 Streams

2 File Handling in C
File Pointer
File I/O
Some Notes

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

FILE pointer and preprocessing

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

FILE pointer and preprocessing

File pointer data type


FILE *<file pointer name>;

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

FILE pointer and preprocessing

File pointer data type


FILE *<file pointer name>;
Opening a file
fopen(<(optional) path to file + filename>, <file handling mode>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

FILE pointer and preprocessing

File pointer data type


FILE *<file pointer name>;
Opening a file
fopen(<(optional) path to file + filename>, <file handling mode>);

"r" - read-only mode


"w" - (over)write mode
"a" - append mode
"rb", "wb", "ab" - binary mode

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

FILE pointer and preprocessing

File pointer data type


FILE *<file pointer name>;
Opening a file
fopen(<(optional) path to file + filename>, <file handling mode>);

"r" - read-only mode


"w" - (over)write mode
"a" - append mode
"rb", "wb", "ab" - binary mode
Closing a file
fclose(<file pointer name>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

File pointer and preprocessing: example

/* Windows style file path */


FILE *windows_input_file = fopen("C:\\Documents and Settings\\file.txt", "r");

/* Unix style file path */


FILE *unix_input_file = fopen("/home/user/practice.c", "w");

FILE *input_file;
...

input_file = fopen("/home/user/practice2.c", "a");

if (input_file == NULL) {
printf(" File /home/user/practice2.c does not exist!\n");
}
...

fclose(windows_input_file);
fclose(unix_input_file);
fclose(input_file);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Topics

1 Streams

2 File Handling in C
File Pointer
File I/O
Some Notes

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Reading data from a file

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Reading data from a file

Reading a character from a file


fgetc(<file pointer name>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Reading data from a file

Reading a character from a file


fgetc(<file pointer name>);
Reading a string/line from a file
fgets(<string variable name>, <maximum string length>,
<file pointer name>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Reading data from a file

Reading a character from a file


fgetc(<file pointer name>);
Reading a string/line from a file
fgets(<string variable name>, <maximum string length>,
<file pointer name>);
Reading from a file on a per byte basis (for binary files)
fread(<variable or array pointer name>, <no. of bytes per element>,
<max no. of elements>, <file pointer name>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Reading data from a file

Reading a character from a file


fgetc(<file pointer name>);
Reading a string/line from a file
fgets(<string variable name>, <maximum string length>,
<file pointer name>);
Reading from a file on a per byte basis (for binary files)
fread(<variable or array pointer name>, <no. of bytes per element>,
<max no. of elements>, <file pointer name>);
Reading from a file, generalized
fscanf(<file pointer name>, "%<data type dependent placeholder>",
&<variable name>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Reading data from a file: example

#include <stdio.h>

main() {
FILE *input_file = fopen("/home/user/practice.c", "r");
char file_char;

/*EOF is a constant/keyword signifying the end of file */


while((file_char = fgetc(input_file)) != EOF) {
printf("%c\n", file_char);
}

fclose(input_file);
}

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Reading data from a file: example

#include <stdio.h>

main() {
FILE *input_file = fopen("/home/user/practice.c", "r");
char file_line[101];

while(fgets(file_line, 100, input_file) !=NULL) {


printf("%s\n", file_line);
}

fclose(input_file);
}

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Writing to a file

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Writing to a file

Writing a character to a file


fputc(<character>, <file pointer name>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Writing to a file

Writing a character to a file


fputc(<character>, <file pointer name>);
Writing a string/line to a file
fputs(<string>, <file pointer name>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Writing to a file

Writing a character to a file


fputc(<character>, <file pointer name>);
Writing a string/line to a file
fputs(<string>, <file pointer name>);
Writing to a file on a per byte basis (for binary files)
fwrite(<variable or array pointer name>, <no. of bytes per element>,
<max no. of elements>, <file pointer name>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Writing to a file

Writing a character to a file


fputc(<character>, <file pointer name>);
Writing a string/line to a file
fputs(<string>, <file pointer name>);
Writing to a file on a per byte basis (for binary files)
fwrite(<variable or array pointer name>, <no. of bytes per element>,
<max no. of elements>, <file pointer name>);
Writing to a file, generalized
fprintf(<file pointer name>, "%<data type dependent placeholder>",
<variable name>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Writing to a file: example

#include <stdio.h>

main() {
int x[2][3] = {{22, 23, 24}, {101, 102, 105}};
int i, j;

/* In write mode, if file is non-existent, fopen will create the file */


FILE *output = fopen("./matrix.out", "w");

for (i = 0; i < 2; i++) {


for(j = 0; j < 3; j++) {
fprintf(output, "%d", x[i][j]);
if (j < 1) {
fputc(’ ’, output);
}
}
fputc(’\n’, output);
}
fclose(output);
}

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Writing to a file: example

#include <stdio.h>

main() {
FILE *output = fopen("./poem.out", "w");
fputs("Roses are red,\n", output);
fputs("Violets are blue,\n", output);
fputs("Rhyming is overrated,\n", output);
fputs("Zebra.\n", output);
fclose(output);
}

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Topics

1 Streams

2 File Handling in C
File Pointer
File I/O
Some Notes

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Maximizing use of the main function

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Maximizing use of the main function

Main function arguments


/* Assume resulting executable name for this source code is "sample" */

main(int argc, char *argv[]) {


FILE *input = fopen(argv[1], "r");
...
}

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Maximizing use of the main function

Main function arguments


/* Assume resulting executable name for this source code is "sample" */

main(int argc, char *argv[]) {


FILE *input = fopen(argv[1], "r");
...
}
Executing the program
$ ./sample /home/user/practice.c

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Converting string to numeric data and vice versa

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Converting string to numeric data and vice versa

String to integer
atoi(<string variable name>);
atol(<string variable name>); // for long int

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Converting string to numeric data and vice versa

String to integer
atoi(<string variable name>);
atol(<string variable name>); // for long int
String to float
atof(<string variable name>);

JMC Yap CS 11 - File Input and Output


File Pointer
Streams
File I/O
File Handling in C
Some Notes

Converting string to numeric data and vice versa

String to integer
atoi(<string variable name>);
atol(<string variable name>); // for long int
String to float
atof(<string variable name>);
Numbers to string
sprintf(<string variable>, "%d", <int variable>);
sprintf(<string variable>, "%ld", <long int variable>);
sprintf(<string variable>, "%f", <float variable>);
sprintf(<string variable>, "%lf", <double variable>);

JMC Yap CS 11 - File Input and Output


Streams
File Handling in C

END OF LESSON 8

JMC Yap CS 11 - File Input and Output

Das könnte Ihnen auch gefallen