所以我有一个看起来像这样的结构:
typedef struct {
char *gpsId;
char *type;
char *name;
double latitude;
double longitude;
int elevationFeet;
char *city;
char *countryAbbrv;
} Airport;
Run Code Online (Sandbox Code Playgroud)
我还有一个函数,该函数的原型如下所示:
char * airportToString(const Airport *a);
Run Code Online (Sandbox Code Playgroud)
我需要按照函数的名称进行操作,我需要将传递的 Airport 结构转换为字符串,然后使用驱动程序打印返回的字符数组。我知道 sprintf 和所有这些方法,但我不想从这个函数打印我需要从主函数打印。我有一些代码,它只是一系列 strcat ,但似乎这是错误的做事方式,而且当它到达纬度时它会失败,因为您不能使用 strcat 将双精度值放入字符串中。另一个规定是我必须动态分配字符串,所以我有一行 malloc 看起来像:
char * array = (char * ) malloc((sizeof(Airport) * 1) + 8);
Run Code Online (Sandbox Code Playgroud)
但我认为这也会带来更多的错误,+ 8 仅用于格式化空格和最后的空终止符,但如果将双打或 int 转换为字符串并且它们很大,它会超出数组边界并超出范围是否正确?完成这项任务的最佳方法是什么,我需要做的是:
构造一个表示给定机场的新字符串。格式化的细节可以是任何东西,但它应该是可读的,并提供关于机场结构的合理数量的细节。此外,返回的字符串应该是动态分配的。
如上所述,确定所需空间量的一种有效方法是snprintf使用str&size指定为NULL和进行初始调用0,分别强制snprintf返回在str&size为写入提供足够空间时本应写入的字符数。然后,您可以使用 返回的字符数snprintf + 1动态分配足以容纳转换为字符串的结构内容的缓冲区。出于示例的目的,输出格式只是一个逗号分隔的airport结构值字符串(通常在 C 中避免变量/结构名称的前导大写,不是无效的,只是传统风格的问题)。
以下是解决此问题的一种方法。struct2str如果成功,该函数返回一个动态分配的字符串,其中包含机场结构的内容,否则返回NULL。如果要转换airport条目数组,则可以轻松传递数组中的元素数并修改函数以返回指向字符串的指针数组。如果您有任何疑问,请告诉我:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *gpsId;
char *type;
char *name;
double latitude;
double longitude;
int elevationFeet;
char *city;
char *countryAbbrv;
} airport;
char *struct2str (airport ap);
int main (void) {
/* declare structure and initialize values */
airport a = { "GPS100151", "GPS/ILS/RVR/AWOS", "A.L. Mangham Regional", 31.58, 94.72, 354, "Nacogdoches", "US" };
/* convert struct a to string (no name conflict with struct) */
char *airport = struct2str (a);
printf ("\n airport as a string:\n\n '%s'\n\n", airport);
/* free dynamically allocated memory */
if (airport) free (airport);
return 0;
}
/* convert contents of airport structure to a comma
separated string of values. Returns pointer to
dynamically allocated string containing contents
of airport structure on success, otherwise NULL.
*/
char *struct2str (airport ap)
{
/* get lenght of string required to hold struct values */
size_t len = 0;
len = snprintf (NULL, len, "%s,%s,%s,%lf,%lf,%d,%s,%s", ap.gpsId, ap.type, ap.name, ap.latitude,
ap.longitude, ap.elevationFeet, ap.city, ap.countryAbbrv);
/* allocate/validate string to hold all values (+1 to null-terminate) */
char *apstr = calloc (1, sizeof *apstr * len + 1);
if (!apstr) {
fprintf (stderr, "%s() error: virtual memory allocation failed.\n", __func__);
}
/* write/validate struct values to apstr */
if (snprintf (apstr, len + 1, "%s,%s,%s,%lf,%lf,%d,%s,%s", ap.gpsId, ap.type, ap.name, ap.latitude,
ap.longitude, ap.elevationFeet, ap.city, ap.countryAbbrv) > len + 1)
{
fprintf (stderr, "%s() error: snprintf returned truncated result.\n", __func__);
return NULL;
}
return apstr;
}
Run Code Online (Sandbox Code Playgroud)
输出
$ ./bin/struct_to_str
airport as a string:
'GPS100151,GPS/ILS/RVR/AWOS,A.L. Mangham Regional,31.580000,94.720000,354,Nacogdoches,US'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10396 次 |
| 最近记录: |