在C fprintf中通用的说法 [英] Generic argument for fprintf in C

查看:123
本文介绍了在C fprintf中通用的说法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写将数据保存在C文件,利用fprintf函数的功能。然而,这需要一个占位符作为它的一个参数。
例如:%s表示字符串,%d表示整数...
我应该怎么做,如果数据类型是通用的,也就是说,使用此功能可以将我写的文件到任何他想要的变量的数据类型的程序员?
谢谢!

I am writing a function that saves data to a file in C, making use of the fprintf function. However, this requires a placeholder as one of its arguments. For example: %s for strings, %d for integers... What should I do if the data type is generic, that is, the programmer using this function can set the data type of the variable I am writing to the file to anything he wants? Thanks!

推荐答案

由于数据的类型在运行时只知道,你必须:

Since the type of the data is known only at run-time, you will have to:


  • 生成在运行时所用的函数fprintf格式字符串,

  • generate the format string used by the fprintf function at run-time, or

多哈日codeD格式字符串之间选择传递给fprintf中。

choose between multiple harcoded format strings to pass to fprintf.

这两个选项之间的选择可能取决于你有多少不同类型的处理,和/或你怎么知道在运行时的占位符参数的类型。

The choice between these two options could depend on how many different types you have to deal with, and/or how you know at run-time the type of your placeholder argument.

这篇关于在C fprintf中通用的说法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆