一个控制台程序读入txt文档的问题

yinvictor 2010-05-09 05:43:01
今天用控制台程序读入一个txt文档内数字,程序出了问题。先给程序:
#include <iostream.h>
#include <fstream.h>
int main()
{
ifstream infile;
infile.open("d:\\元素.txt");
int i,j;
unsigned char m;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
infile>>m;
cout<<m;
}
return 0;
}
txt内容是:
1 2 3
4 5 6
这时还能正常输出;
当当我把txt内容改成:
123 4 5
678 9 0
这时只输出:
1
2
3
4
5
6
若想原样输出txt文档的内容该怎么办?跪求各位大侠。
...全文
196 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yinvictor 2010-05-09
  • 打赏
  • 举报
回复
恩,搞定了,多谢各位。
gudaoqianfu 2010-05-09
  • 打赏
  • 举报
回复
unsigned char m;

要换成int类型了
Fireway2008 2010-05-09
  • 打赏
  • 举报
回复
unsigned int n; 试试
yaozhiyong110 2010-05-09
  • 打赏
  • 举报
回复
int i,j;
int m;
unsigned char n;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
infile>>m;
cout<<m;
}

这样就是了
yaozhiyong110 2010-05-09
  • 打赏
  • 举报
回复
infile>>n; 你这里的n还不是unsigned char 的
那还是只读1字节的啊
yinvictor 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yaozhiyong110 的回复:]
你要读1个int 那就要unsigned int m; 啊
[/Quote]
我感觉应该是二进制输入方式打开文件函数的问题吧,对这个函数不了解呀。
yinvictor 2010-05-09
  • 打赏
  • 举报
回复
下面是修改后的程序:
#include <iostream.h>
#include <fstream.h>
int main()
{
ifstream infile;
infile.open("d:\\元素.txt");
int i,j;
int m;
unsigned char n;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
infile>>n;
m=n-'0';
cout<<m;
}
return 0;
}
结果还是一样,怎么办啊?
yinvictor 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yaozhiyong110 的回复:]
unsigned char m; 这当然只读1字节啦
[/Quote]
我将帖子改一改。谢谢你的回答,呵呵。
yaozhiyong110 2010-05-09
  • 打赏
  • 举报
回复
你要读1个int 那就要unsigned int m; 啊
yinvictor 2010-05-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fireway2008 的回复:]
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{

应该改成:
for(i=0;i<2;i++)
for(j=0;j<5;j++)
{
[/Quote]
我想的是把123当做一个整体输出,用上面的程序还是将1,2,3分别输出了。
yaozhiyong110 2010-05-09
  • 打赏
  • 举报
回复
unsigned char m; 这当然只读1字节啦
Fireway2008 2010-05-09
  • 打赏
  • 举报
回复
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{

应该改成:
for(i=0;i<2;i++)
for(j=0;j<5;j++)
{

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧