Line | |
---|
1 | // textfile.cpp
|
---|
2 | //
|
---|
3 | // simple reading and writing for text files
|
---|
4 | //
|
---|
5 | // www.lighthouse3d.com
|
---|
6 | //
|
---|
7 | // You may use these functions freely.
|
---|
8 | // they are provided as is, and no warranties, either implicit,
|
---|
9 | // or explicit are given
|
---|
10 | //////////////////////////////////////////////////////////////////////
|
---|
11 |
|
---|
12 |
|
---|
13 | #include <stdio.h>
|
---|
14 | #include <stdlib.h>
|
---|
15 | #include <string.h>
|
---|
16 |
|
---|
17 |
|
---|
18 | char *textFileRead(char *fn) {
|
---|
19 |
|
---|
20 |
|
---|
21 | FILE *fp;
|
---|
22 | char *content = NULL;
|
---|
23 |
|
---|
24 | int count=0;
|
---|
25 |
|
---|
26 | if (fn != NULL) {
|
---|
27 | fp = fopen(fn,"rt");
|
---|
28 |
|
---|
29 | if (fp != NULL) {
|
---|
30 |
|
---|
31 | fseek(fp, 0, SEEK_END);
|
---|
32 | count = ftell(fp);
|
---|
33 | rewind(fp);
|
---|
34 |
|
---|
35 | if (count > 0) {
|
---|
36 | content = (char *)malloc(sizeof(char) * (count+1));
|
---|
37 | count = fread(content,sizeof(char),count,fp);
|
---|
38 | content[count] = '\0';
|
---|
39 | }
|
---|
40 | fclose(fp);
|
---|
41 | }
|
---|
42 | }
|
---|
43 | return content;
|
---|
44 | }
|
---|
45 |
|
---|
46 | int textFileWrite(char *fn, char *s) {
|
---|
47 |
|
---|
48 | FILE *fp;
|
---|
49 | int status = 0;
|
---|
50 |
|
---|
51 | if (fn != NULL) {
|
---|
52 | fp = fopen(fn,"w");
|
---|
53 |
|
---|
54 | if (fp != NULL) {
|
---|
55 |
|
---|
56 | if (fwrite(s,sizeof(char),strlen(s),fp) == strlen(s))
|
---|
57 | status = 1;
|
---|
58 | fclose(fp);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | return(status);
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
Note: See
TracBrowser
for help on using the repository browser.