Monday, April 09, 2012

Coding lessons: Working with Files, (reading) (Lessons16)

Anyone following these lessons as actual lessons might think finally something useful.

After all everything we dealt with so far has concerned putting data into a system, working with that data and spitting it out, nothing has been saved at all  when the program completes running that's it, it closes and memory space used for the program that was containing any data goes back to the system where it's overwritten.

You might say to make a really useful program that you have to be able to store data, And of course you need to be able to open that file again, and possibly work with that data and save it in a slightly changed format.

So lets get right into it.

Ways of opening files
Files can be opened in a variety of ways, first and foremost the most basic way of opening a file is to open a file for reading only.

The second way of opening a file would be to open a file for writing, there are two ways of opening a file for writing.
open for overwriting, (where data will be added at the start of the file and overwrite existing data)
or open file for appending, (where data is added to the end of a file.

You select this mode of file opening by using r, w or a.

Files are opened by default in string mode. but files may also be opened in binary mode.


What this means is if you have a file that contains the decimal number 53, if you open this in string mode the number 5 will come out.
Decimal number 97 will be read as 'a' in string mode. (because that is the ASCII representation of the character)

If you have a file that is a series of numerical data then you should know that you want to open the file in binary mode to get your numerical data out.

to select to open the file in string mode, do nothing, (that's the default).
If you want to open the file in binary mode, add a b after the r/w/a file opening mode
if you want to open the file for both reading and writing add a + to the end.

for example if I want to open a file, read data, and then overwrite it, I open with the following mode operator

w+

if it's binary data I use

wb+

remember r opens the file for reading, and sets the file stream at the beginning of the file

w sets the file length to zero (and so overwrites) and points the file stream at the start of the file.

a, points the file stream to the end of the file, so that updates occur at the end of the file.


Reading files
when we access a file, just like when we access a memory resource we use a pointer to do it.

#include<stdio.h>
int main()
{
char c;
FILE *filepointer;


then we point our pointer at the file resource that we wish to access.

filepointer=fopen("D:\\coding\\lesson16\\file.txt", "r");


This is a file that contains a series of characters,
The file is a few characters long and then stops, you and I do not see the stop, but the end of the file is marked with a special marker called End Of File

while (c!=EOF)
{


so we say, while c, (which is the place we'll put characters as they are read from the file) is not the end of file marker, then do this (read the file)

c = getc(filepointer);

this says, c (the place where we're putting the characters read from the file) equals the result of the function getc(filepointer), getc is a function that says get the character pointed to by this pointer, the pointer is filepointer.

printf("%c", c);
}
fclose(filepointer);
}


Then we print the character and close the file

#include<stdio.>
int main()
{
int i=0;
char c;
FILE *filepointer;
filepointer=fopen("D:\\coding\\lesson16\\file.txt", "r");

while (c!=EOF)
{
i++;
c = getc(filepointer);
printf("%c", c);
}
printf("while loop ran %d times", i);
fclose(filepointer);
}



D:\coding\lesson16>source.exe
this
is
a
file! while loop ran 16 times
D:\coding\lesson16>

(and that is the contents of the file at the location.)
the file contains 16 characters
this(4) return(1) is(2) return(1) a(1) return(1) file!(5) return(1)

There are other ways of reading files,
in the same way as we get data from the keyboard using scanf, we can scan files using fscanf.

however, this time to make sure that we stop searching when we reach the end of the file we need to use the function feof(filepointer) to search for the end of the file.

when we use scanf we say scanf("datatype", &variable-to-store), when using fscanf we also need to include an argument to tell it what file to read data from, (in this way we can work with multiple file streams.

#include<stdio.h>
int main()
{
int i=0;
char string[50];
FILE *filepointer;
filepointer=fopen("D:\\coding\\lesson16\\file.txt", "r");

while (feof(filepointer)==0)
{
i++;
fscanf(filepointer, "%s", &string);
printf("%s", string);
}
printf("while loop ran %d times", i);
fclose(filepointer);
}


the loop runs four time, carriage returns are ignored, and there are 4 distinct inputs, in the same way we investigated the scanf statement and found that spaces are ignored, (when introducing flush() spaces are also ignored. so "Hello World" will appear as two strings.

and finally we can use fgets, earlier we used getc to read the file a single character at a time. now we're going to read the file as a string.

fgets requires us to provide three arguments
the charector array (string) in which the returned data will be stored.
the size chunk of data to bring back (number of characters)
the pointer to the file

#include<stdio.h>
int main()
{
int i=0;
char string[50];
FILE *filepointer;
filepointer=fopen("D:\\coding\\lesson16\\file.txt", "r");

while (feof(filepointer)==0)
{
i++;
fgets(string, 50, filepointer);
printf("%s", string);
}
printf("while loop ran %d times", i);
fclose(filepointer);
}


The loop runs 4 times. (as there are four strings)

if we had specified.
fgets(string, 3, filepointer);
then the loop runs 9 times

1, thi
2, s
3, [return]
4, is
5, [return]
6, a
7, [return]
8, fil
9, e!


No comments: