| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 401 人关注过本帖
标题:.txt文件的读取
只看楼主 加入收藏
y605302737
Rank: 1
等 级:新手上路
帖 子:32
专家分:9
注 册:2013-2-2
结帖率:81.82%
收藏
已结贴  问题点数:20 回复次数:6 
.txt文件的读取
大家好:我写了个读取.txt的程序,不知道错在哪,大家帮忙看下
文件是yyy.txt,内容
0 0 1 2 3 1 2 3
2 1 2 2 3 2 1 3
我想读到一个二维数组,再在终端显示,但运行完下面的程序提示:yyy.exe停止工作    出现了一个问题,导致程序停止正常工作。


#include <iostream>
#include <string >
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
   
    ifstream infile;
    infile.open("yyy.txt");
    if(!(infile.is_open()))
        cout<<"cannot open"<<endl;
    int suzu[1][7];
    int value;
    infile>>value;
    while(infile.good())
    {
        for(int i=0;i!=2;i++)
        {
            for(int m=0;m!=8;m++)
                suzu[i][m]=value;
        }
            infile>>value;
    }
    if(infile.eof())
        cout<<"end of file reach"<<endl;
    for(int i=0;i!=2;i++)
    {
        for(int m=0;m!=8;m++)
            cout<<"suzu[i][m] = "<<suzu[i][m]<<endl;
    }
    infile.close();
    return 0;
}
搜索更多相关主题的帖子: cannot include 
2013-02-02 22:06
zhuanjia0
Rank: 4
等 级:业余侠客
威 望:3
帖 子:86
专家分:232
注 册:2012-1-13
收藏
得分:10 
程序代码:
#include <iostream>
#include <string >
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
    
    ifstream infile;
    infile.open("yyy.txt");
    if(!(infile.is_open()))
        cout<<"cannot open"<<endl;
    int suzu[1][7];    //你的二维数组应该是2*7 所以应该改成 int suzu[2][7];
    int value;
    infile>>value;
    while(infile.good())
    {
        for(int i=0;i!=2;i++)
        {
            for(int m=0;m!=8;m++)
                suzu[i][m]=value;
        }
            infile>>value;
    }
    if(infile.eof())
        cout<<"end of file reach"<<endl;
    for(int i=0;i!=2;i++)
    {
        for(int m=0;m!=8;m++)
            cout<<"suzu[i][m] = "<<suzu[i][m]<<endl;
    }
    infile.close();
    return 0;
}


另外,纠正你的一个小错误,数组是shuzu不是suzu……
2013-02-03 00:01
y605302737
Rank: 1
等 级:新手上路
帖 子:32
专家分:9
注 册:2013-2-2
收藏
得分:0 
我将程序改了下,可以运行,程序如下,但输出不正确了
程序代码:
#include <iostream>
#include <string >
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
   
    ifstream infile;
    infile.open("yyy.txt");
    if(!(infile.is_open()))
        cout<<"cannot open"<<endl;
    int shuzu[2][8];
    int value;
   
    for (int i=0;i!=2;i++)
    {
        for(int m=0;m!=8;m++)
        {
            infile>>value;
            while(infile.good())
            {
                shuzu[i][m]=value;

                infile>>value;

            }
        }
       
    }
    if(infile.eof())
        cout<<"end of file reach"<<endl;
    for(int i=0;i!=2;i++)
    {
        for(int m=0;m!=8;m++)
            cout<<"shuzu["<<i<<"]"<<"["<<m<<"]"<<" = "<<shuzu[i][m]<<endl;
    }
    infile.close();
    return 0;
}

终端输出:
图片附件: 游客没有浏览图片的权限,请 登录注册


[ 本帖最后由 y605302737 于 2013-2-3 16:27 编辑 ]
2013-02-03 16:26
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:10 
程序代码:
for (int i=0;i!=2;i++)
    {
        for(int m=0;m!=8 && infile.good();m++)
        {
            infile>>value;
            shuzu[i][m]=value;    
        }
        
    }

迭代的是人,递归的是神。
2013-02-03 20:44
y605302737
Rank: 1
等 级:新手上路
帖 子:32
专家分:9
注 册:2013-2-2
收藏
得分:0 
回复 4楼 lintaoyn
谢谢,结果终于对了。
2013-02-03 22:26
SwanK
Rank: 1
等 级:新手上路
帖 子:68
专家分:3
注 册:2013-1-18
收藏
得分:0 
回答的好!!
2013-02-14 15:01
SwanK
Rank: 1
等 级:新手上路
帖 子:68
专家分:3
注 册:2013-1-18
收藏
得分:0 
系统时间不对,怎么才2月3
2013-02-14 15:01
快速回复:.txt文件的读取
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017402 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved