我正在尝试将从文件中提取的字节数组发送到 C 代码并再次从 C 代码返回该字节数组。
我在文本视图 contents.text 中打印来自 C 的返回值的内容,但我只能看到字节数组对象“OggS”。我看不到字节数组的内容。谁能说我哪里出错了?...我已经发布了 C 代码和 AS 代码。
AS Code:
private function copyByteArray(content:String):void{
try{
byteData = new ByteArray();
//byteData.writeUTFBytes(contents);
var dec:Base64Decoder = new Base64Decoder();
dec.decode(content);
byteData = dec.toByteArray();
//Alert.show("byte Array " + byteData +" :: " +contents.length);
var file:File = File.desktopDirectory.resolvePath("Files/test.spx");
stream = new FileStream();
stream.open(file, FileMode.WRITE);
/* var loader:CLibInit = new CLibInit();
var lib:Object = loader.init();
loader.supplyFile("testFile.txt", byteData);
contents.text = lib.sqre(5);*/
var loader:CLibInit = new CLibInit();
var lib:Object = loader.init();
// var result:String = lib.doMagic(byteData);
var byteArr:ByteArray;
var byteStr:String;
//byteArr.writeBytes(byteData, 0 , byteData.length);
loader.supplyFile("test1.txt" , byteData);
byteStr = lib.readFile("test1.txt");
contents.text = byteStr;
//stream.writeBytes();
stream.close();
}
catch (ex: ErrorEvent){
Alert.show("error");
}
}
C代码:
static AS3_Val readFile(void* self, AS3_Val args)
{
char * fileName;
FILE * file;
long fileSize;
char * buffer;
AS3_ArrayValue(args, "StrType", &fileName);
file = fopen(fileName,"rb");
//Get file size
fseek (file, 0, SEEK_END);
fileSize = ftell(file);
rewind(file);
//Allocate buffer
buffer = (char*) malloc(sizeof(char)*fileSize);
//Read file into buffer
fread(buffer, 1, fileSize, file);
//close file and free allocated buffer
fclose (file);
free (buffer);
return AS3_String((char*)buffer);
}