windows下,vc++,文件打开操作
来源:百度知道 编辑:UC知道 时间:2024/05/25 00:25:34
windowins下,vc++。
在F盘有文件simple.txt,文件内容如下(2行10列):
dim: 2 10
s....x...g
..........
现在的目的是:打开simple.txt文件,得到行数和列数(即2和10)。如何用程序实现?
本人试着编了一段程序,但编译报错:char buf[1024]这一行有错,错误信息为:未知字符“0xa1”。麻烦高人指点。谢谢!!
程序如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char buf[1024];
char *fn="F:\\simple.txt"; /*F盘下的文件simple.txt*/
FILE *in=fopen(fn,"r");
if(in==NULL)
{
printf("%s,open error!!\n",fn);
fclose(in);
return 1;
}
if(fgets(buf,sizeof(buf),in)==NULL)
return 0;
else
{
static int rows=0,cols=0;
sscanf(buf,"dim: %d %d",&rows,&cols);
if((rows<=0)||(cols<=0))
return 0;
cout<<"PARPs: reading"<&l
在F盘有文件simple.txt,文件内容如下(2行10列):
dim: 2 10
s....x...g
..........
现在的目的是:打开simple.txt文件,得到行数和列数(即2和10)。如何用程序实现?
本人试着编了一段程序,但编译报错:char buf[1024]这一行有错,错误信息为:未知字符“0xa1”。麻烦高人指点。谢谢!!
程序如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char buf[1024];
char *fn="F:\\simple.txt"; /*F盘下的文件simple.txt*/
FILE *in=fopen(fn,"r");
if(in==NULL)
{
printf("%s,open error!!\n",fn);
fclose(in);
return 1;
}
if(fgets(buf,sizeof(buf),in)==NULL)
return 0;
else
{
static int rows=0,cols=0;
sscanf(buf,"dim: %d %d",&rows,&cols);
if((rows<=0)||(cols<=0))
return 0;
cout<<"PARPs: reading"<&l
char buf[1024] 这行有中文字符或者全角字符 删掉重写一遍
//没错了!
#include <iostream>
#include <string>
using namespace std;
int main()
{
char buf[1024];
char *fn="D:\\simple.txt"; /*F盘下的文件simple.txt*/
FILE *in=fopen(fn,"r");
if(in==NULL)
{
printf("%s,open error!!\n",fn);
fclose(in);
return 1;
}
if(fgets(buf,sizeof(buf),in)==NULL)
return 0;
else
{
static int rows=0,cols=0;
sscanf(buf,"dim: %d %d",&rows,&cols);
if((rows<=0)||(cols<=0))
return 0;
cout<<"PARPs: reading"<<rows<<"rows and "<<cols<<"cols ";
cout.flush();
}
cin.get();
return 0;
}