scanf / field lengths : using a variable / macro, C/C++

后端 未结 5 1753
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 04:49

How can I use a variable to specify the field length when using scanf. For example:

char word[20+1];
scanf(file, \"%20s\", word);

Also, is

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-03 05:30

    http://www.manpagez.com/man/3/scanf/

    I don't see one. Therefore you're going to be creating your formatting string with sprintf or whatever (if you were coding in C++ you wouldn't be using either).

    Your macro version is possible. You could look in the boost preprocessor library for STRINGIFY and either use it or see how it works.

    Are you really writing in C++ here or in C? If in C then you're doing what you have to do. If you're actually writing C++ and not just considering the two the same language then you have many much better options for reading data from strings.

提交回复
热议问题