从fwrite参数获取数据数组的内容 - 用objective-c打印出数据

时间:2022-09-06 20:51:54

I have some code that reads this:

我有一些代码可以读取:

int SaveTemplateToFile(char* Name, FTR_DATA Template )
{
    //NSLog(@"trying to save template to file");

    FILE *fp;
    fp = fopen( Name, "w+b");
    if( fp == NULL ) return FALSE;

    int Result = fwrite( Template.pData, 1, Template.dwSize, fp ) == Template.dwSize ? TRUE : FALSE;
    fclose(fp);

    return Result;
}

I understand that this will write out the data retrieved from Template.pData into a file named whatever is stored in the Name variable. This is what the .tmpl's contents reads:

据我所知,这会将从Template.pData中检索到的数据写入一个名为Name变量中存储的文件中。这就是.tmpl的内容:

从fwrite参数获取数据数组的内容 - 用objective-c打印出数据

Task/Question:

I am simply trying to store this data into a variable so that I can send this data to my webserver database and store it in a blob file for retrieval at a later time. This will also allow me to get rid of the fwrite function which I wont need since im storing everything onto the sebserver instead of storing it locally.

我只是想将这些数据存储到一个变量中,以便我可以将这些数据发送到我的web服务器数据库并将其存储在blob文件中以便以后检索。这也将允许我摆脱我不需要的fwrite函数,因为我将所有内容存储到sebserver而不是在本地存储它。

I am currently finding trouble reading this data. I am getting a crash when trying to output this data array, I also present what the datatype structure looks like:

我目前在查找这些数据时遇到了麻烦。我在尝试输出这个数据数组时遇到了崩溃,我还提出了数据类型结构的样子:

从fwrite参数获取数据数组的内容 - 用objective-c打印出数据

Where DGTVOID is of typedef void DGTVOID.

其中DGTVOID是typedef void DGTVOID。

How can I correctly read the contents of template? I was thinking if I understood what datatype it is, then I would be able to retrieve the data correcty.

我怎样才能正确阅读模板的内容?我在想如果我理解它是什么数据类型,那么我将能够正确地检索数据。

Update 1

Thanks to Paulw11 I am able access a very small portion of the data using %s instead of %@ which originally lead to a crash. Here is what is being printed now, a few funky upside down question marks:

感谢Paulw11,我可以使用%s代替%@来访问一小部分数据,这最初会导致崩溃。以下是现在正在印刷的内容,一些时髦的颠倒问号:

从fwrite参数获取数据数组的内容 - 用objective-c打印出数据

Is there a way to output the contents of this datastream from Template.pData without having to save the data onto the direction first as a file?

有没有办法从Template.pData输出此数据流的内容,而不必首先将数据保存到文件的方向?

1 个解决方案

#1


1  

I think the first thing you should do is convert your buffer to an NSData instance -

我认为您应该做的第一件事是将缓冲区转换为NSData实例 -

NSData template = [NSData dataWithBytes:Template.pData length:Template.dwSize];

Once you have that then you can Base64 encode the data for transmission over a web request -

一旦你有了,那么你可以通过Base64对数据进行编码,以便通过网络请求进行传输 -

NSString *templateStr = [template base64EncodedStringWithOptions:0];

If you are targeting a version earlier than iOS7 then you can use the deprecated method

如果您的目标是早于iOS7的版本,则可以使用已弃用的方法

NSString *templateStr = [template base64Encoding];

#1


1  

I think the first thing you should do is convert your buffer to an NSData instance -

我认为您应该做的第一件事是将缓冲区转换为NSData实例 -

NSData template = [NSData dataWithBytes:Template.pData length:Template.dwSize];

Once you have that then you can Base64 encode the data for transmission over a web request -

一旦你有了,那么你可以通过Base64对数据进行编码,以便通过网络请求进行传输 -

NSString *templateStr = [template base64EncodedStringWithOptions:0];

If you are targeting a version earlier than iOS7 then you can use the deprecated method

如果您的目标是早于iOS7的版本,则可以使用已弃用的方法

NSString *templateStr = [template base64Encoding];