Documente Academic
Documente Profesional
Documente Cultură
Syntax
CodeChallenge:Structure for aLINE
Structure fora LINE
A line has points, slope, midpoint, and distance between two
points.
Create a member variable of structure line.
Ask the x and y coordinates for eachpoint.
Quickrecap
Before we proceed,
I/O functions
• scanf() and printf()
• getchar() and putchar()
• gets() and puts()
Drawback
Binaryfiles
• mostly the .bin files in your computer
• Instead of storing data in plain text, they store it in the binary
form (0's and 1's)
Files in C
• #include <stdio.h>
• FILEobject contains file stream information
• Special files defined in stdio:
• stdin: Standard input
• stdout: Standard ouput
• stderr: Standard error
• EOF: end-of-file, a special negative integerconstant
FileOperations
• Opening an existing file
• Creating a new file
• Closing a file
• Reading and writing information to a file
Opening afile
• Before we perform any operations on a file, we need toopen it.
• We do this by using a file pointer.
defined inside
stdio.h FILE
allows us to define
file pointer
defined inside
stdio.h FILE
allows us to define
file pointer
fscanf()
• like scanf()
• Takes first argument as filepointer
Whole code
Example of fprintf()
Create a filepointer.
Open thefile
Declare thenecessary variables
Askinput from the user
Write contents into the file.
Close the file
Output
fscanf()example
Create filepointer
Open thefile
Declare necessaryvariables
Read file content
Print file content after scanning
Close the file
Output
Example
Suppose we have a file input.txt with the followingcontent:
4kml6a2ktv9hk511q5o73a
Output: math153
SampleOutput
Appended