Pages

Sunday, February 27, 2011

File operation in C programming(text file method)

There are two type file operation. One is using text file method and another one using binary file method. The method is for text file method.

First step is to define


FILE *
example :
FILE *ifile;
FILE *book;

Then to open a text file in c programming

function : fopen()
format : fopen("filename",mode);
mode:
"r" = read only
"w" = write only(start from beginning file-replace all previous data)
"a" = write only(start at end file- if got data in the data, it will not be replace)
"r+" = open to update the data(read and write)
"w+"= open to update the data(create,read and write)

example
fopen("book.txt","w");
fopen("new/inputfile.text","r");

note: only one mode in one fopen statement. cannot use"rw". can open a file to write and to read but once open to read must close it first to reopen for write operation.

To write to the text data

function : fprintf()
format : fprintf(FILE *,"",);
example:
fprintf(FILE *ifile, "%d\n", int data);
fprintf(FILE *book,"%c\n", char books);

To read from text file

function: fscanf()
format: fscanf(FILE *,"",);
example:
fscanf(FILE *ifile, "%d\n", int data);
fscanf(FILE *book,"%c\n", char books);

To close the text file

function : fclose()
format: fclose(FILE *);
example:
fclose(ifile);
fclose(book);

Full sequence in a main function

void main()
{
FILE *ifile;
FILE *book;
char books[10];
int data;

fopen("/new/inputfile.text","r");
fopen("book.txt","w");

fscanf(ifile, "%d\n",data);

fprintf(book,"%c\n",books);

fclose(ifile);
fclose(book);

return;
}

Wednesday, February 9, 2011

Driver Writing Tutorial in Ubuntu 10.04 kernel 2.6.32-27-generic

Compiling the parlelport.c

1. at the parlelport.c directory

2. the command below is using the admin account

3. type “make –C /lib/modules/(current kernel)/build/ M=$(pwd) modules

a. sudo make –C /lib/modules/2.6.32-27-generic/build/ M=$(pwd) modules

b. then type the admin password

4. The compiling will do the process as shown in figure below.

Inserting the module/driver in the kernel system

1. Login as admin

2. To insert type “insmod

a. sudo insmod parlelport.ko

b. after the driver compiled, it will generate filetype *.ko in the same directory. This will be use when we want to insert the driver in the kernel.

3. To check the module is in the kernel or not type “lsmod

4. We can see that in figure below parlelport is in the kernel by size 1552 byte


Remove the module/driver

1. Login as admin

2. To remove the module type “rmmod

a. sudo rmmod parlelport.ko

3. To check the module again using “lsmod

4. We can see the parlelport module is not in the list below.]


Port usage in the kernel

1. In the parlelport.c we already using a command in the function once the driver connected to request a port to assign to the driver.

2. In the parlelport.c, we request using the command below

a. request_region(0x378,1,"parlelport");

b. this command will request the kernel to reserve and assign the requested port for the driver to use.

c. In this command, requested port is 0x378 using parlelport as it label.

3. We can see in figure below. The parlelport is using 0378-0378 as it port once the driver is insert in the kernel using insmod command.

4. Once the rmmod command is used to remove the driver, it also request the kernel to release the port assigned before using :

a. release_region(0x378,1);