View
229
Download
0
Category
Preview:
Citation preview
8/8/2019 Textfiles C
1/18
Programming in C:
Structures, Textfiles
Clyde Meli 2007-2008
8/8/2019 Textfiles C
2/18
Overview
Structures
Files and Streams
Text File Example File Modes
Type File Example
Append File Example
Reading File Example
8/8/2019 Textfiles C
3/18
Structures
Structures, unlike arrays, may contain variables of
many different data types
They are commonly used to define records to be
stored in binary files (not text files)
They can be used with pointers to build morecomplex data structures such as linked lists,
queues, stacks and trees
Example definition:
struct person {int id_no;
int alive; //0=dead,1=alive
int age;
}
8/8/2019 Textfiles C
4/18
Structures (2)
struct employee {
char name[25];char surname[30];
int age;
char gender;// m or f
double monthlysalary;};
Structs cannot be compared using == and !=
You can initialise them as we did with arrays:
struct employee e1={Luke,Smith,20,m,950};
We can use the dot operator to access a struct
member eg. e1.name returns Luke.
printf(%s,e1.name)
8/8/2019 Textfiles C
5/18
Structures (3)
Structures may be passed to functions by passing
individual struct members, by passing an entirestruct or by passing a pointer to a struct.
When passing a struct or individual member, it is
passed by value
The keyword typedefcan create a synonym or
alias for a previously defined data type eg
typedef struct employee Employee;
defines the new type name Employee as a
synonym for type struct employee. It could be
combined as follows
typedef struct {
.... // members
} Employee;
8/8/2019 Textfiles C
6/18
Files and Streams
In C, every file is a sequential stream of bytes
Every file ends either with an EOF marker or at a
specific position in a system-maintained file
structure
When a file is opened, a stream is associated withthe file
Three files and their associated streams are opened
when program execution starts: Standard Input
(stdin), Standard Output (stdout) and StandardError (stderr)
Using function fgetc, like getchar, we can read one
character from a file. Fputc writes one character to
a file, like putchar.
8/8/2019 Textfiles C
7/18
Files and Streams (2)
fputc(a,stdout) writes a to stdout, same as
putchar(a);
fgetc(stdin) gets a char from stdin, same as
getchar();
fgets and fputs can be used to read and write to afile respectively (counterparts to gets & puts)
fscanf and fprintf are the file counterparts to scanf
and printf (you can print anything to a file)
fopen is used to open a file (it has to be opened soyou can use it) and fclose is used to close it
8/8/2019 Textfiles C
8/18
Files and Streams (3) - C++
ofstream is used to output to a text file
Then open(filename), close() to open & close resp.
ifstream, fstream to read, read&write resp.
Example:
#include #include using namespace std;
int main () {
ofstream myfile;myfile.open ("example.txt");myfile
8/8/2019 Textfiles C
9/18
Text File Example
1
2
3
45
6
..10
// create a text file with 1..10 on every line#include
const int MAX=10;
int main(void){FILE *f;int i;f=fopen(numbers.txt,w);if (!f) return 1;//same as if (f==0)
for (i=1;i
8/8/2019 Textfiles C
10/18
File Modes
fopen takes 2 arguments: a filename and a file mode.
The file mode w specifies that the file is to be written
to (and any existing file is overwritten). r means
reading, a means appending (continue writing at the
current end of the file)
8/8/2019 Textfiles C
11/18
Type File Example
This example imitates the classic type command
(Win) or cat command (Unix)
#include
int main(void){FILE *f;char s[1000];
f=fopen(infile.txt,r);if (!f) return 1;while (fgets(s,1000,f)!=NULL)printf(%s,s);
fclose(f);
return 0;}
8/8/2019 Textfiles C
12/18
Type File Example (C++)
This example imitates the classic type command
(Win) or cat command (Unix)
#include #include #include using namespace std;
int main(void){ifstream file;string s;file.open("infile.txt");while (!file.eof()) {
file >> s;cout
8/8/2019 Textfiles C
13/18
Append File Example
11
12
13
1415
16
..20
// appends 11..20 to existing text file#include
const int MAX=20;
int main(void){FILE *f;int i;f=fopen(numbers.txt,a);if (!f) return 1;//same as if (f==0)
for (i=11;i
8/8/2019 Textfiles C
14/18
Append File Example (C++)
11
12
13
1415
16
..20
#include #include #include using namespace std;
// appends 11..20 to existing text file
const int MAX=20;
int main(void){ofstream file;file.open("numbers.txt",ios::app);if (file.fail()) return 1;//file does not exist, error!//note, file will ALWAYS EXIST when
appending!!!
for (int i=11;i
8/8/2019 Textfiles C
15/18
Reading File Example
11
12
13
1415
16
..20
// reading integers from an existing text file#include
int main(void){
FILE *f;char s[1000];int i;f=fopen(numbers.txt,r);if (!f) return 1;//same as if (f==0)
while (fgets(s,1000,f)!=NULL) {i=atoi(s);printf(%d,i);
}fclose(f);return 0;
}
8/8/2019 Textfiles C
16/18
Reading File Example (2)
atoi() does not detect errors (or arithmetic
overflows) so there is no validation on the filedata..
Something like 643abc will be converted to 643 by
atoi(), xyzabc will be coverted to 0
Using strtol to parse a number:
(char *endptr; has to be defined first)
i=strtol(s,&endptr,10);
it returns the converted value (0 if no conversion
was done, LONG_MAX or LONG_MIN if there wasan overflow). Leading whitespace in the string is
ignored
8/8/2019 Textfiles C
17/18
Reading File Example - C++
11
12
13
1415
16
..20
strtol/atoi are not needed. Still should validate
inputs!
#include #include #include using namespace std;
int main(void){ifstream file;int i;file.open("numbers.txt");if (file.fail()) return 1;//file does not exist, error!while (!file.eof()) {
file >> i;cout
8/8/2019 Textfiles C
18/18
The End?
C++ to C#http://msdn.microsoft.com/en-us/magazine/cc301520.aspx
Java for C Programmers
http://www.d.umn.edu/~gshute/java/c2java.html
Java interface for your C program
ww.math.ucla.edu/~anderson/JAVAclass/JavaInterface/JavaInterface.html
C++ Language tutorial
http://www.cplusplus.com/doc/tutorial/
http://msdn.microsoft.com/en-us/magazine/cc301520.aspxhttp://www.math.ucla.edu/~anderson/JAVAclass/JavaInterface/JavaInterface.htmlhttp://www.cplusplus.com/doc/tutorial/http://www.cplusplus.com/doc/tutorial/http://www.math.ucla.edu/~anderson/JAVAclass/JavaInterface/JavaInterface.htmlhttp://www.d.umn.edu/~gshute/java/c2java.htmlhttp://msdn.microsoft.com/en-us/magazine/cc301520.aspxRecommended