Howdy.
Just wondering if anyone could give me some sample source for File I/O in C ?
Cheers,
Jamie.
Printable View
Howdy.
Just wondering if anyone could give me some sample source for File I/O in C ?
Cheers,
Jamie.
Here's some ANSI-C documentation for you
http://www.hh.se/stud/d98rolb/ansi/main.html
Oh cool thanks!
basic i/o functions
fopen("filename","mode") where mode = w,r,r+,wb,rb,rb+....
Here are a bunch of code snippets
Code:listing 9-2
FILE *fp;
fp = fopen("test", "w");
listing 9-3
FILE *fp;
if ((fp = fopen("test","w"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
listing 9-4
do {
ch = getc(fp);
} while(ch!=EOF);
listing 9-5
/* KTOD: A key to disk program. */
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[])
{
FILE *fp;
char ch;
if(argc!=2) {
printf("You forgot to enter the filename.\n");
exit(1);
}
if((fp=fopen(argv[1], "w"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
do {
ch = getchar();
putc(ch, fp);
} while (ch!='
listing 9-6
/* DTOS: A program that reads files and displays them
on the screen. */
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[])
{
FILE *fp;
char ch;
if(argc!=2) {
printf("You forgot to enter the filename.\n");
exit(1);
}
if((fp=fopen(argv[1], "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
ch = getc(fp); /* read one character */
while (ch!=EOF) {
putchar(ch); /* print on screen */
ch = getc(fp);
}
fclose(fp);
}
listing 9-7
while(!feof(fp)) ch = getc(fp);
listing 9-8
/* Copy a file. */
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[])
{
FILE *in, *out;
char ch;
if(argc!=3) {
printf("You forgot to enter a filename.\n");
exit(1);
}
if((in=fopen(argv[1], "rb"))==NULL) {
printf("Cannot open source file.\n");
exit(1);
}
if((out=fopen(argv[2], "wb")) == NULL) {
printf("Cannot open destination file.\n");
exit(1);
}
/* This code acutally copies the file. */
while(!feof(in)) {
ch = getc(in);
if(!feof(in)) putc(ch, out);
}
fclose(in);
fclose(out);
}
listing 9-9
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void)
{
char str[80];
FILE *fp;
if((fp = fopen("TEST", "w"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
do {
printf("Enter a string (CR to quit):\n");
gets(str);
strcat(str, "\n"); /* add a newline */
fputs(str, fp);
} while(*str!='\n');
}
listing 9-10
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void)
{
char str[80];
FILE *fp;
if((fp = fopen("TEST", "w+"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
do {
printf("Enter a string (CR to quit):\n");
gets(str);
strcat(str, "\n"); /* add a newline */
fputs(str, fp);
} while(*str!='\n');
/* now, read and display the file */
rewind(fp); /* reset file position indicator to
start of the file. */
while(!feof(fp)) {
fgets(str, 79, fp);
printf(str);
}
}
listing 9-11
/* The program substitutes spaces for tabs
in a text file and supplies error checking. */
#include <stdio.h>
#include <stdlib.h>
#define TAB_SIZE 8
#define IN 0
#define OUT 1
void err(int e);
void main(int argc, char *argv[])
{
FILE *in, *out;
int tab, i;
char ch;
if(argc!=3) {
printf("usage: detab <in> <out>\n");
exit(1);
}
if((in = fopen(argv[1], "rb"))==NULL) {
printf("Cannot open %s.\n", argv[1]);
exit(1);
}
if((out = fopen(argv[2], "wb"))==NULL) {
printf("Cannot open %s.\n", argv[1]);
exit(1);
}
tab = 0;
do {
ch = getc(in);
if(ferror(in)) err(IN);
/* if tab found, output appropriate number of spaces */
if(ch=='\t') {
for(i=tab; i<8; i++) {
putc(' ', out);
if(ferror(out)) err(OUT);
}
tab = 0;
}
else {
putc(ch, out);
if(ferror(out)) err(OUT);
tab++;
if(tab==TAB_SIZE) tab = 0;
if(ch=='\n' || ch=='\r') tab = 0;
}
} while(!feof(in));
fclose(in);
fclose(out);
}
void err(int e)
{
if(e==IN) printf("Error on input.\n");
else printf("Error on output.\n");
exit(1);
}
listing 9-12
/* Double check before erasing. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
main(int argc, char *argv[])
{
char str[80];
if(argc!=2) {
printf("usage: xerase <filename>\n");
exit(1);
}
printf("Erase %s? (Y/N): ", argv[1]);
gets(str);
if(toupper(*str)=='Y')
if(remove(argv[1])) {
printf("Cannot erase file.\n");
exit(1);
}
return 0; /* return success to OS */
}
listing 9-13
/* Write some non-character data to a disk file
and read it back. */
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
FILE *fp;
double d = 12.23;
int i = 101;
long l = 123023L;
if((fp=fopen("test", "wb+"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
fwrite(&d, sizeof(double), 1, fp);
fwrite(&i, sizeof(int), 1, fp);
fwrite(&l, sizeof(long), 1, fp);
rewind(fp);
fread(&d, sizeof(double), 1, fp);
fread(&i, sizeof(int), 1, fp);
fread(&l, sizeof(long), 1, fp);
printf("%f %d %ld", d, i, l);
fclose(fp);
}
listing 9-14
struct struct_type {
float balance;
char name[80];
} cust;
listing 9-19
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[])
{
FILE *fp;
if(argc!=3) {
printf("Usage: SEEK filename byte\n");
exit(1);
}
if((fp = fopen(argv[1], "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
if(fseek(fp, atol(argv[2]), SEEK_SET)) {
printf("Seek error.\n");
exit(1);
}
printf("Byte at %ld is %c.\n", atol(argv[2]), getc(fp));
fclose(fp);
}
listing 9-20
fseek(fp, 9*sizeof(struct list_type), SEEK_SET);
listing 9-21
/* fscanf() - fprintf() example */
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
void main(void)
{
FILE *fp;
char s[80];
int t;
if((fp=fopen("test", "w")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
printf("Enter a string and a number: ");
fscanf(stdin, "%s%d", s, &t); /* read from
keyboard */
fprintf(fp, "%s %d", s, t); /* write to file */
fclose(fp);
if((fp=fopen("test","r")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
fscanf(fp, "%s%d", s, &t); /* read from file */
fprintf(stdout, "%s %d", s, t); /* print on
screen */
}
listing 9-24
#include <stdio.h>
void main(void)
{
char str[80];
printf("Enter a string: ");
gets(str);
printf(str);
}
listing 9-25
TEST > OUTPUT
listing 9-26
TEST < INPUT > OUTPUT
listing 9-27
#include <stdio.h>
void main(void)
{
char str[80];
freopen("OUTPUT", "w", stdout);
printf("Enter a string: ");
gets(str);
printf(str);
}
listing 9-28
int fd;
if((fd=open(filename, mode)) == -1) {
printf("Cannot open file.\n");
exit(1);
}
listing 9-30
/* Read and write using unbuffered I/O */
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#define BUF_SIZE 128
void input(char *buf, int fd1);
void display(char *buf, int fd2);
void main(void)
{
char buf[BUF_SIZE];
int fd1, fd2;
if((fd1=open("test", O_WRONLY))==-1){ /* open for write */
printf("Cannot open file.\n");
exit(1);
}
input(buf, fd1);
/* now close file and read back */
close(fd1);
if((fd2=open("test", O_RDONLY))==-1){ /* open for read */
printf("Cannot open file.\n");
exit(1);
}
display(buf, fd2);
close(fd2);
}
/* Input text. */
void input(char *buf, int fd1)
{
register int t;
do {
for(t=0; t<BUF_SIZE; t++) buf[t] = '\0';
gets(buf); /* input chars from keyboard */
if(write(fd1, buf, BUF_SIZE)!=BUF_SIZE) {
printf("Error on write.\n");
exit(1);
}
} while (strcmp(buf, "quit"));
}
/* Display file. */
void display(char *buf, int fd2)
{
for(;;) {
if(read(fd2, buf, BUF_SIZE)==0) return;
printf("%s\n", buf);
}
}
listing 9-31
/* Demonstrate lseek(). */
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <fcntl.h>
#define BUF_SIZE 128
void main(int argc, char *argv[])
{
char buf[BUF_SIZE+1], s[10];
int fd, sector;
if(argc!=2) {
printf("usage: dump <sector>\n");
exit(1);
}
buf[BUF_SIZE] = '\0'; /* null terminate buffer */
if((fd=open(argv[1], O_RDONLY))==-1) {
printf("Cannot open file.\n");
exit(1);
}
do {
printf("\nBuffer: ");
gets(s);
sector = atoi(s); /* get the sector to read */
if(lseek(fd, (long)sector*BUF_SIZE, 0)==-1L)
printf("Seek Error\n");
if(read(fd, buf, BUF_SIZE)==0) {
printf("Sector Out Of Range\n");
}
else
printf(buf);
} while(sector>=0);
close(fd);
}
Cheers for that too :)
Frank Herbert strikes again... see that you read Dune, so did I, about 45 years ago.
Yeah on the 3rd book of the dune series at the mo, and lovin' it !
Cool, Jamie, can i have the titles for the three first ones (I might get some time to read them :)
Dune
Dune Messiah
Children of Dune
God Emperor of Dune
Heretics of Dune
Chapterhouse: Dune
I can lend you any of the first three if you don't have them... don't have the rest myself though.
That would be cool, but how do you mean lend them?
Well you give me your address, I stick them in the post to you, you read them, and then send them back :confused:
Alrite :confused: That sounds pretty simple :D
see you @ icq?
I've read the first 4... then got bored =). So I went and read the wheel of time series again. Now it's Lord of the rings for the first time (stupid programming... I havent had to renew a book from the library in like, ten years!)
Z.