www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Writing an integer to a file

reply yochi <yochib gmail.com> writes:
Hello, I am trying to write an integer to a file, but when I open the
file it writes the char the number represents in ascii.

does anyone have a suggestion?
Mar 07 2011
parent reply "Regan Heath" <regan netmail.co.nz> writes:
On Mon, 07 Mar 2011 11:26:25 -0000, yochi <yochib gmail.com> wrote:
 Hello, I am trying to write an integer to a file, but when I open the
 file it writes the char the number represents in ascii.

 does anyone have a suggestion?

Post your code, people can then suggest changes. Also, some things to consider: - Are you opening the file in ascii or binary mode? - Are you calling a function which writes an integer? It sounds, from your description, that you're writing an integer 'as a char/byte' which probably means you've called the wrong function. -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Mar 07 2011
parent reply %u <yochib gmail.com> writes:
this is part of the code:

void WritePushPop(cmd command, string segment, int index)
		{
                        string temp = "TextFile.Asm";
			AsmFile = new File(temp, FileMode.OutNew );

		        string x = toString(index);

AsmFile.writeString(" ");

AsmFile.write(index);

AsmFile.writeLine("D=A");

AsmFile.writeLine(" 0");

AsmFile.writeLine("A=M");

AsmFile.writeLine("M=D");

AsmFile.writeLine("D=A+1");

AsmFile.writeLine(" 0");

AsmFile.writeLine("M=D");


}

Now it wouldn't even build, it doesn't like the line: string x =
toString(index);
but this line works when I'm working with the console.


Thanks!
Mar 07 2011
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
On 07/03/2011 12:11, %u wrote:
 this is part of the code:

Posting the whole code (or even better, a minimal, complete program that shows the problem) helps a lot.
 void WritePushPop(cmd command, string segment, int index)
 		{
                          string temp = "TextFile.Asm";
 			AsmFile = new File(temp, FileMode.OutNew );

 		        string x = toString(index);

 AsmFile.writeString(" ");

 AsmFile.write(index);

File.write writes a binary representation of its argument. To write a textual representation, use writef or writefln. <snip>
 Now it wouldn't even build, it doesn't like the line: string x =
 toString(index);
 but this line works when I'm working with the console.

What's the error message? Stewart.
Mar 07 2011