如何从文件中读取“”字符
How to read from a file the ' ' character
我的代码的问题在于它永远不会进入if。换句话说,它从不读取""字符。""存在于我要读取的文件中,这很重要读取它以便以正确的方式保存矩阵。
#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
using namespace std;
int main(int argc, char **argv){
int N, M;
int n, m;
int i, j;
char a;
ifstream myfile;
string txt;
char mat[1000][1000];
txt = argv[1];
myfile.open(txt);
n = 0;
m = 0;
while(myfile >> a){
if(a == 'n'){
n++;
m = 0;
continue;
}
mat[n][m] = a;
m++;
}
myfile.close();
cout << n << " " << m;
for(i=0; i<=n; i++){
cout << endl;
for(j=0; j<m; j++){
cout << mat[i][j] << " ";
}
}
}
输入字符有两个等效的函数 — getc ()
和 fgetc ()
。支持两个相同的功能
getc()
函数用于从打开的流中读取字符 fopen()
.getc()
原型如下:
int getc(FILE * fp);
其中 fp 是指向 fopen()
返回的文件类型 FILE * 的指针。传统上,getc()
返回一个整数,但高字节设置为 0。
getc()
函数在到达文件末尾时返回 EOF。若要将文本文件读到最后,请使用以下代码:
ch = getc(fp);
while (ch! = EOF)
{
enter code here`ch = getc (fp);
}
或者你可以试试
if(a == '')
相关文章:
- 尝试一次将单个字符读取到大小不确定的数组中
- C++将 txt 数据和字符读取到 2D 数组中
- C++ UTF-8 瑞典语字符读取为 ASCII
- C++逐个字符读取二进制文件字符
- 逐个字符读取文件并使用<cctype>
- C 如何通过特定n字符读取TXT文件的值
- 如何使用4个字符读取C 的文本文件
- 使用C++中的字符读取具有文件 I/O 的文件
- 是否可以从C 中的文件中的线中的特定字符读取
- 将字符读取到数组
- 如何在 c++ 中逐个字符读取文件并通过 array_name[index] 访问它
- 如何从文本文件中读取,C++中逐个字符读取
- 如何将多个字符读取合并为std::向量
- 在 c++ 中逐个从文件字符读取
- C++ 中有多少个方法逐个字符读取字符串,以对每个字符执行所需的操作
- scanf 只能将 3 个字符读取到 5 位字符数组中
- 将行的前 X 个字符读取为字符串
- 逐个字符读取以查看它是否是数字
- 如何从字符**读取字符串
- 如何逐个字符读取文件的全部内容