我有一个sscanf解决的问题(从字符串中提取东西).我不喜欢sscanf,因为它不是类型安全的,而且是古老而可怕的.我想要聪明并使用C++标准库的一些更现代的部分.我应该用什么呢?
我是编程菜鸟所以请耐心等待.
我正在尝试将文本文件中的数字读入数组.文本文件"somenumbers.txt"只包含16个数字,如此"5623125698541159".
#include <stdio.h>
main()
{
FILE *myFile;
myFile = fopen("somenumbers.txt", "r");
//read file into array
int numberArray[16];
int i;
for (i = 0; i < 16; i++)
{
fscanf(myFile, "%d", &numberArray[i]);
}
for (i = 0; i < 16; i++)
{
printf("Number is: %d\n\n", numberArray[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
该计划不起作用.它汇编但输出:
编号是:-104204697
数字是:0
编号是:4200704
编号是:2686672
编号是:2686728
编号是:2686916
编号是:2004716757
编号是:1321049414
数字是:-2
编号是:2004619618
编号是:2004966340
编号是:4200704
编号是:2686868
编号是:4200798
编号是:4200704
编号是:8727656
进程返回20(0x14)执行时间:0.118秒按任意键继续.
以下三个功能:
getc getchar&scanf
从stdin读取角色的最佳选择是什么?为什么?
这些功能中是否存在任何已知的缺点或限制,这使得一个功能优于另一个功能?
有一个很好的方法循环一个字符串sscanf吗?
假设我有一个如下所示的字符串:
char line[] = "100 185 400 11 1000";
Run Code Online (Sandbox Code Playgroud)
我想打印这笔款项.我真正想写的是:
int n, sum = 0;
while (1 == sscanf(line, " %d", &n)) {
sum += n;
line += <number of bytes consumed by sscanf>
}
Run Code Online (Sandbox Code Playgroud)
但是没有干净的方法来获取这些信息sscanf.如果它返回消耗的字节数,那将是有用的.在这样的情况下,人们可以使用strtok,但能够写出类似于你可以做的事情的东西是很好的stdin:
int n, sum = 0;
while (1 == scanf(" %d", &n)) {
sum += n;
// stdin is transparently advanced by scanf call
}
Run Code Online (Sandbox Code Playgroud)
有一个我忘记的简单解决方案吗?
所以我想问一下这两者有什么区别.如果有的话.在大学里我接受过教学,我使用的是scanf,但是在我的个人电脑上,视觉工作室一直在发送这个警告.
error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead.
Run Code Online (Sandbox Code Playgroud)
我必须将所有scanf更改为scanf_s,否则程序将无法构建.(我从2013年开始使用ms visual studio的最新版本)
当我需要扫描一堆字符串中的值时,我经常发现自己sscanf()严格地回到C语言,因为它简单易用.例如,我可以非常简洁地从字符串中拉出几个double值:
string str;
double val1, val2;
if (sscanf(str.c_str(), "(%lf,%lf)", &val1, &val2) == 2)
{
// got them!
}
Run Code Online (Sandbox Code Playgroud)
这显然不是很C++.我不一定认为这是一种憎恶,但我总是在寻找一种更好的方法来完成一项共同的任务.我理解读取字符串的"C++方式"是istringstream,但是处理上面的格式字符串中的括号和逗号所需的额外输入只会使得我想要使用它太麻烦.
是否有一种很好的方法可以以类似于上面的方式将内置设施弯曲到我的意愿,或者是否有一个好的C++库以更加类型安全的方式完成上述操作?看起来Boost.Format确实以一种很好的方式解决了输出问题,但我没有发现任何类似的输入简洁.
如果我尝试以下内容:
int anint;
char achar;
printf("\nEnter any integer:");
scanf("%d", &anint);
printf("\nEnter any character:");
scanf("%c", &achar);
printf("\nHello\n");
printf("\nThe integer entered is %d\n", anint);
printf("\nThe char entered is %c\n", achar);
Run Code Online (Sandbox Code Playgroud)
它允许输入一个整数,然后scanf完全跳过第二个,这真的很奇怪,因为当我交换两个(charscanf优先)时,它工作正常.究竟是什么错了?
似乎有很多方法可以在C中获得用户输入.
什么是最简单的方法,需要很少的代码?
基本上我需要显示这个:
Enter a file name: apple.text
Run Code Online (Sandbox Code Playgroud)
基本上我需要询问用户文件名.所以我需要的东西只能得到用户输入的那个词.
我正在尝试使用此代码读取0到255(unsigned char)之间的值.
#include<stdio.h>
int main(void)
{
unsigned char value;
/* To read the numbers between 0 to 255 */
printf("Please enter a number between 0 and 255 \n");
scanf("%u",&value);
printf("The value is %u \n",value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我按预期得到了以下编译器警告.
warning: format ‘%u’ expects type ‘unsigned int *’, but argument 2 has type ‘unsigned char *’
这是我对该计划的输出.
Please enter a number between 0 and 255 45 The value is 45 Segmentation fault
运行此代码时,我确实遇到了分段错误.
unsigned char使用读取值的最佳方法是什么scanf?