fclose, fopen
// using 'fclose' function #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { FILE *fptr; char *str = "Test string"; char *file = "temp.txt"; char buff[255]; int ch; if ( ferror ( fptr = fopen(file,"w") ) ) { perror("Cann't open temp.txt for 'w'\n"); exit(EXIT_FAILURE); } fputs(str,fptr); freopen(file,"r",fptr); while ( ( ch = fgetc(fptr)) != EOF ) fputc(ch,stdout); fclose(fptr); remove(file); return 0; } OUTPUT : // Test string fcloseall
#include <stdio.h> #include <stdlib.h> #include <errno.h> int main (int argc, char *argv[]) { FILE *p[3]; char buff[256], phone[255], name[50]; char *file[] = {"name.dat","phone.dat","report.dat"}; char *mod[] = {"r","r","w"}; int i = 0; // open 3 files. 2 for reading, 1 for writing for ( ; i < 3; i++ ) { if ( (p[i] = fopen (file[i],mod[i])) == NULL ) { sprintf(buff,"Cann't open %s for \%s\. err: %d", file[i],mod[i],errno); fprintf(stderr,"%s\n",buff); if ( i > 0 ) // close all opened files fcloseall(); exit(EXIT_FAILURE); } } i = 1; while ( fscanf(p[0],"%s%s",buff,name) == 2 && fscanf(p[1],"%s",phone) == 1 ) { fprintf(p[2],"%d:%s %s:%s\n",i++, buff,name,phone); } // close all opened files fcloseall(); // open report.dat for reading if ( (p[0] = fopen (file[2],mod[0])) == NULL ) { sprintf(buff,"Cann't open %s for \%s\. err: %d", file[2],mod[0],errno); fprintf(stderr,"%s\n",buff); exit(EXIT_FAILURE); } while ( (fgets (buff,254,p[0])) != NULL ) { fprintf(stdout,"%s",buff); } fclose(p[0]); return 0; } OUTPUT : // 1:Linda Bain:123-3455 // 2:Robert Smith:345-9877 // 3:Ivan Mazepa:456-9870 feof
#include <stdio.h> int main (int argc, char *argv[]) { FILE *fp; int c; fp = fopen("test.txt","r"); /* print `EOF' character */ while ( !feof(fp) ) { c = fgetc(fp); fputc(c, stdout); // or fputc ( c=fgetc(fp), stdout ); } puts("\n"); // puts 2 new lines rewind(fp); /* mutch better */ while ( !feof(fp) ) { c = fgetc(fp); if ( feof(fp) ) break; fputc ( c, stdout ); } return 0; } OUTPUT : // First line // Second line // // // First line // Second line ferror, perror, clearerr
#include <stdio.h> #include <errno.h> #include <stdlib.h> int main (int argc, char *argv[]) { FILE *fp; int c; char *file_name = "nonexisting.txt"; /* full notation */ if ( ferror ( fp = fopen(file_name,"r")) ) { perror("Cann't open file for reading\n"); clearerr(fp); /* exit(EXIT_FAILURE); */ } /* shorter */ fp = fopen(file_name,"r"); if ( ferror(fp) ) { fprintf(stderr,"Cann't open file. Error: %d\n",errno); clearerr(fp); /* exit(EXIT_FAILURE); */ } fp = fopen(file_name,"w"); if ( !ferror(fp) ) { fprintf(stdout,"File is opened for writing!\n"); } /* read char from file wich id opend for writing */ fputc('A',fp); if ( ferror(fp) ) { perror("File is opend for writing, not reading!\n"); exit(EXIT_FAILURE); } /* `exit' function closes all opened files we don't need to use `fclose()' */ // not reached statement return 0; } OUTPUT : // I tested `ferror' in Mandrake, RedHat, SuSE, // and never had correct result. // Every time - "Segmentation fault". // In Unix this function works as needed. // // I think that it is the default reaction on SIGSEGV, // that calls `abort()' function. // // Some Message from gdb ( debuger ): // $ gdb -q a.out // (gdb) run // Starting program: /home/ana/C/Stdio/a.out // // Program received signal SIGSEGV, Segmentation fault. // 0x40099498 in ferror () from /lib/libc.so.6 // (gdb) where // #0 0x40099498 in ferror () from /lib/libc.so.6 // #1 0x080486a2 in main () // #2 0x4004a7ee in __libc_start_main () from /lib/libc.so.6 fflush
// functon 'fflush' #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { FILE *phone_p; char *name[] = { "Robert Smith", "Linda Bain","Ivan Mazepa", NULL }; char *phone[] = { "345-3455", "123-9876","987-3456", NULL }; int i = 0; char buff[255]; if ( ferror ( phone_p = fopen("phone.dat","w")) ) { perror("Cann't open for writing\n"); exit(EXIT_FAILURE); } for ( ; i < 3; i++ ) { sprintf(buff,"%d:%s:%s\n",i+1,name[i],phone[i]); fputs(buff,phone_p); fflush(phone_p); // fflush(NULL) - flushes all opened stream for output } // reopen for reading freopen ("phone.dat","r",phone_p); { while ( fgets(buff,254,phone_p) ) fprintf(stdout,"%s",buff); } fclose(phone_p); remove("phone.dat"); return 0; } OUTPUT : // 1:Robert Smith:345-3455 // 2:Linda Bain:123-9876 // 3:Ivan Mazepa:987-3456 fgetc, fputc
#include <stdio.h> int main (int argc, char *argv[]) { FILE *in; int ch; in = fopen ("report.dat","r"); while ( (ch = fgetc(in)) != EOF ) { fputc (ch,stdout); } fclose(in); return 0; } OUTPUT : // 1 Linda Bain 123-3455 // 2 Robert Smith 345-9877 // 3 Ivan Mazepa 456-9870 fgetpos
#include <stdio.h> int main (int argc, char *argv[]) { FILE * fp; fpos_t pos; int ch; /* open file for writing + reading */ fp = fopen ("test.txt","w+"); /* cursor position */ printf ("Position of cursor is %lu\n",ftell(fp)); /* save this position */ fgetpos (fp, &pos); /* writing message to the file */ fputs ("That is a test message",fp); printf ("Position of cursor is %lu\n",ftell(fp)); /* put cursor to the previosly saved position */ fsetpos (fp, &pos); printf ("Position of cursor is %lu\n",ftell(fp)); /* change contants of file ('That' for 'This') */ fputs ("This",fp); printf ("Position of cursor is %lu\n",ftell(fp)); /* put cursor to the position '0' */ rewind(fp); /* reading the file */ while ( (ch = fgetc(fp)) != EOF ) { fputc(ch,stdout); } fclose (fp); /* removing the file */ remove("test.txt"); return 0; } OUTPUT : // Position of cursor is 0 // Position of cursor is 22 // Position of cursor is 0 // Position of cursor is 4 // This is a test message fgets
#include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argc, char *argv[]) { char buff[255]; char *name; double gpa; printf ("Enter your name : "); fgets (buff,25,stdin); buff[strlen(buff)-1] = '\0'; name = (char*)malloc(strlen(buff)+1); strcpy(name,buff); printf ("\nEnter your GPA : "); fgets (buff,5,stdin); gpa = strtod(buff,(char **)NULL); printf ("\nStudent: %s; GPA: %.2lf\n", name, gpa); free(name); return 0; } OUTPUT : // Enter your name : // Enter your GPA : // Student: Robert Smith; GPA: 3.56 fileno
#include <stdio.h> int main (int argc, char *argv[]) { FILE *fp1, *fp2; int fd; printf("STDIN file descriptor is %d\n",fileno(stdin)); printf("STDOUT file descriptor is %d\n",fileno(stdout)); printf("STDERR file descriptor is %d\n",fileno(stderr)); fp1 = fopen("test.txt","w"); fd = fileno(fp1); printf("File opened for 'w', feledescriptor is %d\n",fd); fp2 = fopen("test.txt","r"); fd = fileno(fp2); printf("File opened for 'r', feledescriptor is %d\n",fd); fcloseall(); return 0; } OUTPUT : // STDIN file descriptor is 0 // STDOUT file descriptor is 1 // STDERR file descriptor is 2 // File opened for 'w', feledescriptor is 3 // File opened for 'r', feledescriptor is 5 fprintf
#include <stdio.h> int main (int argc, char *argv[]) { int i = 100; unsigned int u = 250; float f = 12.5; double d = 2.897653; char ch = 'A'; char ary[] = "Array string"; char *str = "justify"; char *p; p = ary; fprintf (stdout,"Integer i = %d\n", i); fprintf (stdout,"Unsigned u = %d\n", u); fprintf (stdout,"Float f = %f\n", f); fprintf (stdout,"Double d = %.2lf\n", d); fprintf (stdout,"Scientific notation d = %e\n", d); fprintf (stdout,"Char ch = %c\n", ch); fprintf (stdout,"Array string is : %s\n", ary); fprintf (stdout,"Address of ary is : %p\n", p); fprintf (stdout,"Left %s\n", str); fprintf (stdout,"Right %20s\n",str); fprintf (stdout,"%-*s %s\n",20,"%-*s %s","test string"); fprintf (stdout,"%*s %s\n",20,"%*s %s","test string"); return 0; } OUTPUT : // Integer i = 100 // Unsigned u = 250 // Float f = 12.500000 // Double d = 2.90 // Scientific notation d = 3e+00 // Char ch = A // Array string is : Array string // Address of ary is : 0xbffff2f0 // Left justify // Right justify // %-*s %s test string // %*s %s test string fputs, puts
#include <stdio.h> int main (int argc, char *argv[]) { char *str = "Test string for puts and fputs functions"; puts(str); fputs(str,stdout); return 0; } OUTPUT : // Test string for puts and fputs functions // Test string for puts and fputs functions fread, fwrite
#include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { FILE *ptr; int len; char buff[1024]; char *str = "Testing string for fread and fwrite functions"; if ( (ptr = fopen("test.txt","wb")) == NULL ) { fprintf (stderr,"Cann't open for writing\n"); exit(EXIT_FAILURE); } len = strlen(str); if ( fwrite(str, sizeof(char), len, ptr ) != len ) { fprintf(stderr,"Error writing to file\n"); exit(EXIT_FAILURE); } fclose(ptr); if ( (ptr = fopen("test.txt","rb")) == NULL ) { fprintf (stderr,"Cann't open for reading\n"); exit(EXIT_FAILURE); } if ( fread(buff, sizeof(char), len, ptr ) != len ) { fprintf(stderr,"Error reading file\n"); exit(EXIT_FAILURE); } fclose(ptr); fputs(buff,stdout); return 0; } OUTPUT : // Testing string for fread and fwrite functions freopen
#include <stdio.h> void reop(FILE *out); int main (int argc, char *argv[]) { FILE *out; char *message = "Test reopen function"; out = fopen("test.txt","w"); fprintf(out,"%s",message); reop(out); fclose(out); return 0; }// end of main void reop(FILE *out) { char ch = 0; freopen("test1.txt", "r", out); // not same file_name while ( (ch = fgetc(out)) != EOF ) { putc(ch,stdout); } freopen("test.txt", "r", out); // same file_name while ( (ch = fgetc(out)) != EOF ) { putc(ch,stdout); } } OUTPUT : // Text from test1.txt file // Test reopen function fscanf
#include <stdio.h> int main (int argc, char *argv[]) { FILE *in; char f_name[25], l_name[25], phone [25]; int num; system("head -1 report.dat"); puts(""); in = fopen("report.dat","r"); while ( (fscanf(in,"%d%s%s%s",&num, f_name,l_name,phone)) == 4 ) { fprintf (stdout,"%04d %-12s %-12s %s\n",num, f_name,l_name,phone); } fclose(in); return 0; } OUTPUT : // 1 Linda Bain 123-3455 // 0001 Linda Bain 123-3455 // 0002 Robert Smith 345-9877 // 0003 Ivan Mazepa 456-9870 fseek
#include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { FILE *fp; int ch; /* w+ - after writing you can read file */ if ( (fp = fopen("test.txt", "w+")) == NULL) { fprintf(stderr,"Cann't open for writing\n"); exit(EXIT_FAILURE); } /* Writes letters to the file */ for (ch = 65; ch <= 90; ch++) { putc(ch, fp); } /* Skips 8 letters */ fseek(fp, 8L, SEEK_SET); ch = getc(fp); /* Changes the I to an @ */ fseek(fp, -1L, SEEK_CUR); fputc('@', fp); printf("The first character is %c\n",ch); /* Skips 16 letters, points to Q */ fseek(fp, 16L, SEEK_SET); ch=getc(fp); printf("The second character is %c\n",ch ); /* Changes the I to an @ */ fseek(fp, -1L, SEEK_CUR); fputc('@', fp); fclose(fp); return; } // The first character is I // The second character is Q fsetpos
#include <stdio.h> int main (int argc, char *argv[]) { FILE *fp; fpos_t pos; int ch; /* open file in write-read mode */ fp = fopen("test.txt","w+"); /* write message to the file */ fputs("first line\n",fp); printf ("After writing - cursors's position is %lu\n",ftell(fp)); /* save current position */ fgetpos(fp,&pos); /* put cursor to the very beginning of file */ rewind(fp); printf ("After rewind - position is %lu\n",ftell(fp)); /* put cursor to seved position */ fsetpos (fp,&pos); printf ("After fsetpos - position is %lu\n",ftell(fp)); /* write message to the file */ fputs("second line\n",fp); printf ("After writing - position is %lu\n",ftell(fp)); /* save current position */ fgetpos(fp,&pos); /* put cursor to the very beginning of file */ rewind(fp); printf ("After rewind - position is %lu\n",ftell(fp)); /* put cursor to seved position */ fsetpos (fp,&pos); printf ("After fsetpos - position is %lu\n",ftell(fp)); /* write message to the file */ fputs("third line\n",fp); printf ("After writing - position is %lu\n",ftell(fp)); /* put cursor to the very beginning of file */ rewind(fp); printf("\nContents of file:\n"); /* read file */ while ( (ch = fgetc(fp)) != EOF ) fputc(ch, stdout); fclose(fp); remove("test.txt"); return 0; } OUTPUT : // After writing - cursors's position is 11 // After rewind - position is 0 // After fsetpos - position is 11 // After writing - position is 23 // After rewind - position is 0 // After fsetpos - position is 23 // After writing - position is 34 // // Contents of file: // first line // second line // third line ftell, rewind
#include <stdio.h> int main (int argc, char *argv[]) { FILE *fp; char *msg = "Testing ftell function"; long lon; char buff[1024]; fp = fopen("test.txt","w"); fputs(msg, fp); fclose(fp); fp = fopen("test.txt","r"); lon = ftell(fp); printf ("After opening file position = %lu\n",lon); /* read 'Testing ' */ fgets(buff,9,fp); fputs(buff,stdout); puts(""); lon = ftell(fp); printf ("After reading 8 char position = %lu\n",lon); /* read next 5 char */ fgets(buff,6,fp); fputs(buff,stdout); puts(""); lon = ftell(fp); printf ("Reading next 5 char, position = %lu\n",lon); /* puts pointer to the very beginning of the file */ rewind(fp); lon = ftell(fp); printf ("After rewind position = %lu\n",lon); /* reads hole message from file */ fgets (buff, strlen(msg)+1 ,fp); fputs (buff,stdout); puts(""); lon = ftell(fp); printf ("After reading hole file position = %lu\n",lon); fclose(fp); return 0; } OUTPUT : // After opening file position = 0 // Testing // After reading 8 char position = 8 // ftell // Reading next 5 char, position = 13 // After rewind position = 0 // Testing ftell function // After reading hole file position = 22 getc, putc
#include <stdio.h> int main (int argc, char *argv[]) { int ch; printf ("Enter testing string\n"); printf ("\tCtl+d to quit : "); while ( (ch = getc(stdin)) != EOF ) { putc(ch,stdout); } puts(""); return 0; } OUTPUT : // Enter testing string // Ctl+d to quit : testing // getc function // the end getchar, putchar
#include <stdio.h> int main (int argc, char *argv[]) { int ch; printf ("\nEnter any character for testing : "); ch = getchar(); printf ("\nYou entered : "); putchar(ch); return 0; } OUTPUT : // Enter any character for testing : A // You entered : A gets
#include <stdio.h> int main (int argc, char *argv[]) { char buff[50]; gets(buff); return 0; } OUTPUT : // /tmp/cc6HCkEq.o: In function `main': // /tmp/cc6HCkEq.o(.text+0xe): the `gets' // function is dangerous and should not be used. // use `fgets' instead popen, pclose
#include <stdio.h> int main (int argc, char **argv) { char result [256]; FILE * cmdp = popen ("ps -ef | grep ana", "r"); if (!cmdp) { perror ("popen"); return 1; } while (fgets (result, sizeof(result), cmdp)) fputs (result, stdout); pclose(cmdp); return 0; } OUTPUT : // ana 1336 1335 pts/3 opera // ana 1340 1 ? gvim // ana 1349 1348 pts/2 a.out // ana 1350 1349 pts/2 sh -c ps -ef | grep ana // ana 1351 1350 pts/2 ps -ef // ana 1352 1350 pts/2 grep ana printf
#include <stdio.h> int main (int argc, char *argv[]) { int i = 100; unsigned int u = 250; float f = 12.5; double d = 2.897653; char ch = 'A'; char ary[] = "Array string"; char *str = "justify"; char *p; p = ary; printf ("Integer i = %d\n", i); printf ("Unsigned u = %d\n", u); printf ("Float f = %f\n", f); printf ("Double d = %.2lf\n", d); printf ("Scientific notation d = %.e\n", d); printf ("Char ch = %c\n", ch); printf ("Array string is : %s\n", ary); printf ("Address of ary is : %p\n", p); printf ("Left %s\n", str); printf ("Right %20s\n",str); printf ("%-*s %s\n",20,"%-*s %s","test string"); printf ("%*s %s\n",20,"%*s %s","test string"); return 0; } OUTPUT : // Integer i = 100 // Unsigned u = 250 // Float f = 12.500000 // Double d = 2.90 // Scientific notation d = 3e+00 // Char ch = A // Array string is : Array string // Address of ary is : 0xbffff2f0 // Left justify // Right justify // %-*s %s test string // %*s %s test string remove
// removing file(s) #include <stdio.h> int main (int argc, char *argv[]) { int num[] = {1,2,3}; char ext[] = ".tmp"; // file extention char file_name[50]; int i; puts("List of files:"); /* list of file in current directory */ system("ls -m"); /* create 3 files (file1.tmp, file2...) */ system("touch file{1,2,3}.tmp"); puts("\nList of files:"); system("ls -m"); for ( i = 0; i < sizeof(num)/sizeof(int); i++ ) { /* generate file_name */ sprintf(file_name,"%s%d%s","file",num[i],ext); /* remove file */ remove(file_name); } puts("\nList of files:"); system("ls -m"); return 0; } OUTPUT : // a.out, remove.c // List of files: // // List of files: // a.out, file1.tmp, file2.tmp, file3.tmp, remove.c // // List of files: // a.out, remove.c rename
// renaming file(s) #include <stdio.h> #include <string.h> int main (int argc, char **argv) { char result [256]; FILE *cmdp1, *cmdp2; int dote = '.'; char *ext; char new_file_name[50]; // list of coma separated files system ("ls -m"); // create 3 files (f1.cpp f2.cpp f3.cpp) cmdp1 = popen ("touch f{1,2,3}.cpp", "w"); if (!cmdp1) { perror ("popen"); return 1; } pclose(cmdp1); system ("ls -m"); cmdp2 = popen ("ls -1", "r"); if (!cmdp2) { perror ("popen"); return 1; } while (fgets (result, sizeof(result), cmdp2)) { // function fgets puts '\n' result[strlen(result)-1] = 0; ext = (index(result,dote)); if ( !strcmp(ext,".cpp") ) { // generate new file name sprintf(new_file_name,"%s%s",result,".tmp"); rename(result,new_file_name); } } pclose(cmdp2); system("ls -m"); return 0; } OUTPUT : // a.out, rename.c // a.out, f1.cpp, f2.cpp, f3.cpp, rename.c // a.out, f1.cpp.tmp, f2.cpp.tmp, f3.cpp.tmp, rename.c scanf
/* you have number of monet - f.e. 15 * first costs $money - f.e. 5 * second $money each - f.e. 10 * Total cost is $money - $1.05 * How many */ #include <stdio.h> int main (int argc, char *argv[]) { int wiget, col_x, col_y; int flag = 0; float x, y, total,x_cost,y_cost; printf("Enter cost of first item : "); scanf ("%f", &x); printf("Enter cost of second item : "); scanf ("%f", &y); printf("Enter number of wigets : "); scanf ("%d", &wiget); printf("Enter total cost of wigets : "); scanf ("%f", &total); for ( col_x = 0; col_x <= wiget; col_x++ ) { x_cost = col_x * x; for ( col_y = 0; col_y <= wiget; col_y++ ) { y_cost = col_y * y; if ( (x_cost + y_cost) == total && (col_x + col_y) == wiget) { printf("\tNumber %f is : %d\n",x,col_x); printf("\tNumber %f is : %d",y,col_y); printf("\n\n"); flag = 1; break; } } if ( flag ) break; } if ( !flag ) puts ("There is no variant!"); return 0; } OUTPUT : // Enter cost of first item : 5 // Enter cost of second item : 10 // Enter number of wigets : 15 // Enter total cost of wigets : 105 // Number 5.000000 is : 9 // Number 10.000000 is : 6 setbuf
#include <stdio.h> int main (int argc, char *argv[]) { char buff[BUFSIZ]; FILE *p1, *p2; p1 = fopen ("test1.txt","w"); p2 = fopen ("test2.txt","w"); setbuf ( p1, buff ); fputs ("This is buffered stream. This message will write to a disc after flushing", p1); fflush(p1); setbuf ( p2, NULL ); fputs ("This is unbuffered stream. You don't need to flush it.", p2); fcloseall(); return 0; } OUTPUT : snprintf
#include <stdio.h> int main (int argc, char *argv[]) { int i = 0, n = 4; char *s[] = {"first string", "second string", "third string"}; char buff[255]; for ( ; i < 3; i++ ) { snprintf(buff, n, "%s", s[i]); fprintf (stdout,"%s\n", buff); n += n; } return 0; } OUTPUT : // fir // second // third string sprintf
#include <stdio.h> int main (int argc, char *argv[]) { int i = 100; double d = 2.5; char ch = 97; char *s = "Test"; char buff[255]; sprintf (buff,"\t\t\tinteger i = %d\n\ double d = %f\n\ char ch = %c\n\ string s = %s", i, d, ch, s); puts(buff); return 0; } OUTPUT : // integer i = 100 // double d = 2.500000 // char ch = a // string s = Test sscanf
#include <stdio.h> #include <stdlib.h> int main (void) { char a[20]="12/10/2000", b[]="300023"; char j[3],k[3],l[3]; char c[3],d[5]; char s,s1; int p, p1, p2; sscanf(a,"%2s%1s%2s%1s%4s",j,l,k,c,d); printf ("\n%d%s%d%s%d\n", atoi(j),l,atoi(k),c,atoi(d)); sscanf(b,"%4d%d",&p,&p1); printf("%d %d\n",p,p1); sscanf(a,"%d%c%d%c%d",&p,&s,&p1,&s1,&p2); printf("%d %c %d %c %d\n",p,s,p1,s1,p2); return 0; } OUTPUT : // 12/10/2000 // 3000 23 // 12 / 10 / 2000 tmpfile
// generating temporary file name #include <stdio.h> int main (int argc, char *argv[]) { char buff[50]; FILE *tmp; // tmpfile will automatically deleted tmp = tmpfile(); fputs("Anatoliy",tmp); // puts pointer to the beginning of file rewind(tmp); fgets(buff,sizeof("Anatoliy"),tmp); fputs(buff,stdout); fclose(tmp); return 0; } OUTPUT : // the use of 'tmpnam' is dangerous, // better use 'mkstemp' // // File name is: /tmp/filev9pLLj // Anatoliy tmpnam, tempname
// generating temporary file_name #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { char *pwd; char *tmp_nam; char *temp_nam; tmp_nam = tmpnam(NULL); fputs(tmp_nam,stdout); fputs("\n",stdout); // 2 argc: 1 - *dir, 2 - *filename temp_nam = tempnam(NULL,NULL); fputs(temp_nam,stdout); fputs("\n",stdout); pwd = getenv("PWD"); temp_nam = tempnam(pwd,NULL); fputs(temp_nam,stdout); fputs("\n",stdout); return 0; } OUTPUT : // gcc -Wall tmpnam.c &> err // cat err // /tmp/cc8s4zsp.o: In function `main': // /tmp/cc8s4zsp.o(.text+0xc): the use of `tmpnam' // is dangerous, better use `mkstemp' // /tmp/cc8s4zsp.o(.text+0x4b): the use of `tempnam' // is dangerous, better use `mkstemp' // // /tmp/file9PxPwR // /tmp/fileyVVxcB // /home/ana/C/Stdio/fileBdahUk ungetc
/* using ungetc function */ #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main (int argc, char *argv[]) { int c; double d, sum= 0; char buff[50]; FILE *p; puts("Contents of test.txt"); /* without `fflush' message from `system' will print first */ fflush(stdout); system ("cat test.txt"); puts("\n"); /* file has ':' field separator. Instead of usig `strtok', I use `ungetc' function */ p = fopen("test.txt","r"); while ( !feof (p) ) { c = fgetc(p); if ( feof(p) ) break; /* change ':' for ' ' */ if ( c == ':' ) c = ' '; if ( isdigit(c) ) { /* put back character */ ungetc(c,p); /* read hole number */ fgets(buff,sizeof(double),p); d = strtod(buff,(char**)NULL); printf(" %.2lf\n",d); sum += d; } else putc(c,stdout); } fclose(p); printf ("=========== total : $%.2lf\n",sum); return 0; } OUTPUT : // Contents of test.txt // Robert Smith:67500 // Linda Bain:79200 // Ivan Mazepa:42000 // // // Robert Smith 67500.00 // Linda Bain 79200.00 // Ivan Mazepa 42000.00 // =========== total : $188700.00