FileOperation.cpp

zhangxm, 2011-04-22 00:10

Download (1.5 kB)

 
1
// to enable CCLOG()
2
#define COCOS2D_DEBUG 1
3

    
4
#include "cocos2d.h"
5
#include "FileOperation.h"
6
#include <stdio.h>
7

    
8
using namespace std;
9

    
10
void FileOperation::saveFile()
11
{
12
        string path = getFilePath();
13
        FILE *fp = fopen(path.c_str(), "w");
14

    
15
        if (! fp)
16
        {
17
                CCLOG("can not create file %s", path.c_str());
18
                return;
19
        }
20

    
21
        fputs("file example", fp);
22
        fclose(fp);
23
}
24

    
25
void FileOperation::readFile()
26
{
27
        string path = getFilePath();
28
        FILE *fp = fopen(path.c_str(), "r");
29
        char buf[50] = {0};
30

    
31
        if (! fp)
32
        {
33
                CCLOG("can not open file %s", path.c_str());
34
                return;
35
        }
36

    
37
        fgets(buf, 50, fp);
38
        CCLOG("read content %s", buf);
39

    
40
        fclose(fp);
41
}
42

    
43
string FileOperation::getFilePath()
44
{
45
        string path("");
46

    
47
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
48
        // In android, every programe has a director under /data/data.
49
        // The path is /data/data/ + start activity package name.
50
        // You can save application specific data here.
51
        path.append("/data/data/org.cocos2dx.application/tmpfile");
52
#endif
53

    
54
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
55
        // You can save file in anywhere if you have the permision.
56
        path.append("D:/tmpfile");
57
#endif
58

    
59
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WOPHONE)
60
        path = cocos2d::CCApplication::sharedApplication().getAppDataPath();
61

    
62
#ifdef _TRANZDA_VM_
63
        // If runs on WoPhone simulator, you should insert "D:/Work7" at the
64
        // begin. We will fix the bug in no far future.
65
        path = "D:/Work7" + path;
66
        path.append("tmpfile");
67
#endif
68

    
69
#endif
70

    
71
        return path;
72
}

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.