C i/o tutorialde bugghy
/----------------\
| C i/o tutorial |
\----------------/
v1.0a
Written by: Bugghy
E-mail: bugghy@home.ro
URL: www.bugghy.home.ro
Comments/errors/bugs/jokes > bugghy@home.ro
CONTENTS
=====================================
1. Introduction
2. Basic understanding
3. Synopsis
4. Basic help
5. Link of the day
6. Last words
=====================================
1. Introduction
===============
You all wanna make nifty programs in C that open files, write to them,
do nasty things with them ... but you don't wanna use the system() function.
So here's how:
2. Basic understanding
======================
For accessing files in C you must use a FILE pointer to define that file
aka let the program now that you are accessing a file.
FILE *fp;
To start writting to a file you first have to open it:
fopen("/var/log/dmesg","w");
- fopen = function used to open a file:
- "/var/log/dmesg" = filename to open
- "w" = openned for writting
You can open a file in the following modes:
r - open for reading
w - open for writing (file need not exist)
a - open for appending (file need not exist)
r+ - open for reading and writing, start at beginning
w+ - open for reading and writing (overwrite file)
a+ - open for reading and writing (append if file exists)
Example:
FILE *fp;
if ((fp = fopen("/var/log/mesg", "r")) == NULL )
{
printf("Couldn't open file");
exit(-1);
}
// tries to open a file for reading
After you've done your job (aka you wrote to the file ...) it's time to close
it:
fclose(fp);
Now we can think of writting/reading to/from file: fprintf, fscanf
fprintf(fp,"it werks\n");
If we open a binary file then we use: fwrite, fread
Example:
FILE *fp;
if ((fp = fopen("/var/log/dmesg", "wb");
char ch[10]="funnywabit";
fwrite(ch, sizeof(ch[0]), sizeof(ch)/sizeof(ch[0]), fp);
3. SYNOPSIS
===========
#include
FILE *fopen (const char *path, const char *mode);
int fclose( FILE *stream);
int fprintf(FILE *stream, const char *format, ...);
int fscanf( FILE *stream, const char *format, ...);
size_t fwrite( const void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fread( void *ptr, size_t size, size_t nmemb, FILE *stream);
4. BASIC HELP
============
This is just a tut to real BASIC i/o programming.
Read all man pages for more info (man fprintf ...)
It's real simple after some practice.
5. Link of the day
==================
Great tuts: blacksun.box.sk neworder.box.sk black.box.sk ...
6. Last words
=============
If you wanna clarify some misunderstandings in the text, add something,
bugs/comments/... mail bugghy@home.ro
THE USUAL DISCLAIMER:
---------------------
This file is for [of course] informational purposes only. I
don't take responsibility for anything anyone does after reading this file.
Gr33tz:
vMatriCS.nET , anyone that helped me (they know who they are)
Toate drepturile rezervate © www.pc-dojo.ro Reproducerea permisa doar insotita de acordul autorului sau al site-ului
Arhiva | Adauga un articol | Despre e-zine |