FlowVis 1.0

FlowVis/Framework/textfile.cpp

00001 // textfile.cpp
00002 //
00003 // simple reading and writing for text files
00004 //
00005 // www.lighthouse3d.com
00006 //
00007 // You may use these functions freely.
00008 // they are provided as is, and no warranties, either implicit,
00009 // or explicit are given
00011 
00012 
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <string>
00016 
00017 
00018 char *textFileRead(char *fn) {
00019 
00020         FILE *fp;
00021         char *content = NULL;
00022 
00023         int count=0;
00024 
00025         if (fn != NULL) {
00026                 fp = fopen(fn,"rt");
00027 
00028                 if (fp != NULL) {
00029       
00030       fseek(fp, 0, SEEK_END);
00031       count = ftell(fp);
00032       rewind(fp);
00033 
00034                         if (count > 0) {
00035                                 content = (char *)malloc(sizeof(char) * (count+1));
00036                                 count = fread(content,sizeof(char),count,fp);
00037                                 content[count] = '\0';
00038                         }
00039                         fclose(fp);
00040                 }
00041         }
00042         return content;
00043 }
00044 
00045 int textFileWrite(char *fn, char *s) {
00046 
00047         FILE *fp;
00048         int status = 0;
00049 
00050         if (fn != NULL) {
00051                 fp = fopen(fn,"w");
00052 
00053                 if (fp != NULL) {
00054                         
00055                         if (fwrite(s,sizeof(char),strlen(s),fp) == strlen(s))
00056                                 status = 1;
00057                         fclose(fp);
00058                 }
00059         }
00060         return(status);
00061 }
00062 
00063 
00064 
00065 
00066 
00067 
00068 
 All Classes Functions Variables Friends