为什么fscanf在读取字符时失败
Why is fscanf failing while reading characters?
我有以下标题为"data.txt"的示例数据文件:
a<tab>b<tab>c<newline>
a<tab>b<tab>c<newline>
和以下代码:
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
char a,b,c;
FILE* fin = fopen("data.txt","r");
fscanf(fin,"%c %c %c",&a,&b,&c);
cout<<a<<" "<<b<<" "<<c<<endl;
fscanf(fin,"%c %c %c",&a,&b,&c);
cout<<a<<" "<<b<<" "<<c<<endl;
fclose(fin);
return 0;
}
编译并运行时,其输出如下:
a b c
a b
我期望看到:
a b c
a b c
怎么了?
您没有跳过读取的第三个和第四个字符之间的空白-您需要在其中一个格式字符串中使用另一个空格。
(为什么在C++中使用fscanf
?)
相关文章:
- 如果没有malloc,链表实现将失败
- C++字符*缓冲区的大小
- 模板参数替换失败,并且未完成隐式转换
- HEX值到wchar_t字符(UTF-8)的转换
- 在 Windows 上,stat 和 GetFileAttributes 对于包含奇怪字符的路径失败
- GetFileAttributeW对于非ASCII字符失败
- 查找下一个文件失败,出现空格字符
- 从UTF-8到ANSI wcstomb的转换在一个特定字符处失败
- 失败的输入帮助,国际和字符,C++
- 在添加额外的字符时,字符串流的putback失败
- 为什么当我删除此字符 * 时,调试断言失败block_type_is_valid
- 字符数组复制失败
- 当我传递从字符串流构建的字符指针时,流打开(..)失败
- 由于隐藏/控制字符,在C++中读取文本文件的行失败
- 当QFILE用于mediaSource时,QT声子播放失败,当字符串通过时,QT声波播放正常
- 对于非静态字符串,字符串到字符* 转换失败
- 字符编码-当读取超过127个ASCII值时,C++cin失败
- 为什么fscanf在读取字符时失败
- 调试断言失败错误,字符数组终止为null
- std::string擦除最后一个字符失败