X-Git-Url: https://pd.if.org/git/?p=pd_readline;a=blobdiff_plain;f=readfile.c;fp=readfile.c;h=c20033428571190f1e798462a02d045ef32571e8;hp=0000000000000000000000000000000000000000;hb=4bb27266f935c9aafad6870ffc8847fc65c8120f;hpb=3f771e17236364ded86e96ee64f99344337991f8 diff --git a/readfile.c b/readfile.c new file mode 100644 index 0000000..c200334 --- /dev/null +++ b/readfile.c @@ -0,0 +1,98 @@ + + + +/* readfile.c */ + +/* Check that a file exists. */ +/* If it does, read it into an array and print it out. */ + + +#include +#include + + + +/* Helper function, to let us see if a file */ +/* exists in the current directory. */ +int fexists(char *fname) +{ + FILE *fptr; + fptr = fopen(fname, "r") ; + if ( !fptr ) return -1 ; /* File does not exist in dir. */ + fclose(fptr); + return 0; /* File DOES exist in dir. */ +} + + +/* Helper function to chop newlines off the lines read in. */ +/* Without this being done, an extra newline is inserted */ +/* (which is usually not what is wanted). */ +void chop(char *s) +{ + s[strcspn(s,"\n")] = '\0'; +} + + +/* An array to store the file in. */ +/* Only 20 lines are read. */ +char myarray[20][80]; + + +/* Read the file into the array of strings. */ +void readfile(char *fname) +{ + int retval = fexists(fname); + + int i; + if (retval == 0) { + /* File exists, so open it. */ + /* We open it in read-write mode so we can */ + /* append new commands to it. */ + FILE *fptr; + fptr = fopen(fname, "rw"); + + for(i=0; i<20; i++) + { + chop(fgets(myarray[i], 80, fptr) ); + } + + } /* retval == 0 */ + + else puts("Error! File does not exist. \n"); + +} + + +void printfile(void) +{ + int j; + + for(j=0; j<10; j++) + { + puts(myarray[j]); + } + +} + + + +int main(void) +{ + +readfile("test.txt"); + +printfile(); + + +return 0; + +} + + + + + + + + +