Having trouble File Writing

Hey guys, I like to think I pick up most parts of C++ easily, but when it comes to file writing for some reason, I just can't do it. I'm using an SDK, and it's forums and support are pretty inactive, and the file writing functions seem simple, so I was hoping somebody here would understand it pretty quickly. Basically all I need to do is write 3 integers to a file, and then load those back in later on.

Here's every function I'm given for file writing:
1
2
3
4
5
6
7
8
FileClose(int FileHandle);
int FileHandle = FileCreate("FileName");
FileDelete("FileName");
FileLength(int FileHandle); //returns the number of characters in the file
int FileHandle = FileOpen("FileName");
FileRead(int FileHandle, char *buffer, int length); //Buffer is the array that recieves the data, and length is the length of the buffer
FileSeek(int FileHandle, int position);
FileWrite(int FileHandle, char *buffer, int length);


My largest problem is just not understanding how buffer works, or why you need file seeking, and what indexes are; things like that. the syntax doesn't bother me. I can also provide more information on the SDK if I haven't provided enough to solve my problem. For anyone wondering I'm using DragonFireSDK.

Thanks for any help!!
- Marshall Demirjian
marshallrohin@gmail.com
Last edited on
what SDK do you have?

Basically all I need to do is write 3 integers to a file, and then load those back in later on.

This is a way you can do it with the standard c++ library...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream> // filestream library

int main()
{
    int a = 5, b=10, c=20;

    std::ofstream ofile; // output filestream
    ofile.open("test.txt"); // open file
    ofile<< a << " " << b << " " << c; // write 3 integers
    ofile.close(); // release file

    // ...

    std::ifstream ifile; // input filestream
    ifile.open("test.txt"); // open file
    ifile >> a >> b >> c; // get 3 integers
    ifile.close(); // release file


    std::cout << a << " " << b << " " << c;
}
Your code helped a lot! Based on what you did, I was able to do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    char *scores;//Instead of int a = 5, b=10, c=20;
    int ofile; //filehandle
    int length;

    ofile = FileOpen("test.txt"); // open file
    FileSeek(ofile, !?!?!?!??!?!!?); //Set the position in the file?
    length = //However you find the length of char *scores?
    FileWrite(ofile, scores, length); //Write data
    FileClose(ofile); // release file

    // ...

    ofile = FileOpen("test.txt"); // open file
    FileSeek(ofile, !?!?!?!??!?!!?); //Set the position in the file?
    length = FileLength(ofile); //Set length to the length of the file
    FileRead(ofile, scores, length); //Read data to scores
    FileClose(ofile); // release file

}


As you can see, my only problem is what to put for "Position" (The ?!?!?!?!?). I'm guessing I should maybe use 0 since I always want to start from the beginning?

PS: I'm using DragonFireSDK - Its for app development with c++
http://www.dragonfiresdk.net/help/DragonFireSDKHelp.html
The file Writing functions will be under the folder API and then CoreFunctions

Thanks again!
- Marshall Demirjian
Last edited on
Ah ok a short look in the API revealed everything for me.

ofile = FileOpen("test.txt"); // open file
be carefully here, if the file does not exists ofile will have the value 0 so you should check after opening.

You could just create it if it does not exist
1
2
3
ofile = FileOpen("test.txt"); // open file
if(ofile == 0)
    ofile = FileCreate("test.txt");


FileSeek(ofile, !?!?!?!??!?!!?); //Set the position in the file?
I guess you only need to use FileSeek if you know the contents of the file exactly, otherwise I assume it starts at 0, the beginning of the file.

1
2
3
char *scores;//Instead of int a = 5, b=10, c=20;
 // ...
length = //However you find the length of char *scores? 

wooooow.... you didn't even initialise scores here, it just points at a random location on the RAM ...

maybe you want something like this?
1
2
3
4
5
#include <cstring>

// main
char scores[] = {"5 10 20"};
length = strlen(scores);

anyway, it's up to you how you handle your strings...

the rest looks fine but I think you won't actually need FileSeek that often :)



May I ask why don't you use anything from the C++ standard library for your task?
why don't you use the c++ standard class std::string to store the scores and why don't you use the c++ standard streams to write to and read from files?

can't you use it?
what kind of app do you develop, what's your target system?
Last edited on
THANKYOU SO MUCH!!

I should have it working in a few minutes, but I've run into a problem:

How would I change this character array with an integer? (I'm not used to character arrays as I'm sure you've figured out...)

For example: I have
1
2
3
int score1;
int score2;
int score3;


How do I make
char scores[] = {score1 + score2 + score3}; ?
Also what do I do when I have one that's like 10 digits long? Doesn't that screw up my char array?

Also I'm using character arrays because that's the only input the functions provided by DragonFire allow, and I'm using DragonFire's file writing because I'm developing for IOS, and since the C++ libraries aren't really made for IOS, I assumed they wouldn't work (And DragonFire provides them, so I assumed "Why would they provide them if they weren't needed?").
Last edited on
Also I'm using character arrays because that's the only input the functions provided by DragonFire allow, and I'm using DragonFire's file writing because I'm developing for IOS, and since the C++ libraries aren't really made for IOS, I assumed they wouldn't work (And DragonFire provides them, so I assumed "Why would they provide them if they weren't needed?").
You should first try it...
As this post states: http://stackoverflow.com/questions/5774642/any-limitations-using-c-stl-for-ios-development
The main purpose of using standardized tools is to achieve portability. As long as you use the library as intended, without relying on behavior outside the standard, there should not be any issues moving it to any platform with a good C++ implementation.

The Standard Library implementation on iOS is the same as the one on Mac OS. There should be no difference.



How do I make
char scores[] = {score1 + score2 + score3}; ?

uhm, why is there a +?
do you want to add the scores together or do you want to save all 3 of them?
Save all three of them:
I put the plus for visual representation.
For example, if
1
2
3
score1 = 5;
score2 = 27;
socre3 = 10029;


I want
char scores[]
to be
"5 27 10029"

And vice versa? (From character array to integer)
Thanks! :D
Last edited on
with the c++ standard library I'd do this:

1
2
3
4
5
6
#include <sstream>

std::stringstream ss;
ss << score1 << " " << score2 << " " << score3;
std::string scores;
std::getline(ss, scores);


but I've allready forgotten how to do this in C, I'm sorry ^^

look at the library cstring, maybe this one helps you out
Well, I figured something out.. but you're going to make fun of me... But hey, it works.
So this is my working version(complete using the SDK):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "DragonFireSDK.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
#include <cstring>
#include <sstream>

using namespace std;

int info1, info2, info3;
void WriteFile(int info1, int info2, int info3);
void ReadFile();
int length;
int dataF1, dataF2, dataF3;
char data1[5];
char data2[5];
char data3[100];
stringstream ss;
string scoreS;

void WriteFile(int info1, int info2, int info3)
{
	ss.clear();
	ss << info1;
	getline(ss, scoreS);
	strcpy_s(data1, scoreS.c_str());
	dataF1 = FileOpen("data1.txt"); // open file
	if (dataF1 == 0){ dataF1 = FileCreate("data1.txt"); }
	dataF1 = FileOpen("data1.txt");
	length = strlen(data1);
	FileWrite(dataF1, data1, length); //WRITE data
	FileClose(dataF1); // release file

	ss.clear();
	ss << info2;
	getline(ss, scoreS);
	strcpy_s(data2, scoreS.c_str());
	dataF2 = FileOpen("data2.txt"); // open file
	if (dataF2 == 0){ dataF2 = FileCreate("data2.txt"); }
	dataF2 = FileOpen("data2.txt");
	length = strlen(data2);
	FileWrite(dataF2, data2, length); //WRITE data
	FileClose(dataF2); // release file

	ss.clear();
	ss << info3;
	getline(ss, scoreS);
	strcpy_s(data3, scoreS.c_str());
	dataF3 = FileOpen("data3.txt"); // open file
	if (dataF3 == 0){ dataF3 = FileCreate("data3.txt"); }
	dataF3 = FileOpen("data3.txt");
	length = strlen(data3);
	FileWrite(dataF3, data3, length); //WRITE data
	FileClose(dataF3); // release file

}

void ReadFile()
{

	dataF1 = FileOpen("data1.txt"); // open file
	if (dataF1 == 0){ dataF1 = FileCreate("data1.txt"); }
	dataF1 = FileOpen("data1.txt");
	length = strlen(data1);
	FileRead(dataF1, data1, length); //READ data
	FileClose(dataF1); // release file
	info1 = atoi(data1);

	dataF2 = FileOpen("data2.txt"); // open file
	if (dataF2 == 0){ dataF2 = FileCreate("data2.txt"); }
	dataF2 = FileOpen("data2.txt");
	length = strlen(data2);
	FileRead(dataF2, data2, length); //READ data
	FileClose(dataF2); // release file
	info2 = atoi(data2);

	dataF3 = FileOpen("data3.txt"); // open file
	if (dataF3 == 0){ dataF3 = FileCreate("data3.txt"); }
	dataF3 = FileOpen("data3.txt");
	length = strlen(data3);
	FileRead(dataF3, data3, length); //READ data
	FileClose(dataF3); // release file
	info3 = atoi(data3);
	
}



As you can see, I tapped into my inner laziness and just made 3 different data files... Made the conversion from strings to character to integers to etc to etc alot easier since they weren't storing all 3 values at once.

It may be ugly, but like I said, it works. Thanks again so much for bringing me this far!
Last edited on
I'm getting a weird problem now that I've had it processed to run on the phone, I'm getting this error:

1
2
/App.cpp:2831: error: 'strcpy_s' was not declared in this scope
/App.cpp:2831: error: 'strcpy_s' was not declared in this scope


Note that DragonFire gave me this, not my compiler. If your wondering, Line 2831 is:

strcpy_s(data1, scoreS.c_str());

This is also line 28 in my example above. I tried changing my header file <string> to <string.h> but that didn't change anything. Any ideas? Thanks!
How do I make
char scores[] = {score1 + score2 + score3}; ?


1
2
3
char scores[0]=score1;
char scores[1]=score2;
char scores[2]=score3;


Ah never mind I found the problem, strcpy_s is supported on IOS. I used strcpy instead. And SamuelAdams I didn't use that because sometimes my different scores vary in digits (I.E. score 3 could be anywhere from 1 to 1000000). Instead I just used the 3 separate character arrays and data files.

Thanks everyone for fixing my problem!
Topic archived. No new replies allowed.