| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 402 人关注过本帖
标题:文件读取
只看楼主 加入收藏
fireice11
Rank: 1
等 级:新手上路
帖 子:11
专家分:4
注 册:2010-10-26
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:8 
文件读取
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "fstream.h"
#include "string.h"


int main(int argc, char* argv[])
{
    FILE *fp;
char str[5][100];
char strr[100];
FILE *frFile;
frFile=fopen("test.txt","r");
int LineNum=0;
if(frFile==NULL)
{
    printf("can't open test.txt to read!\n");
    exit(0);
}
int i;
for(i=0;i<5;i++)
{
fgets(strr,100,frFile);
strncpy((char*)str[i],strr,100);

}

fclose(frFile);
for (i=0;i<5;i++)
{
printf("%s\n",str[i]);
}

system("pause");
return 0;

   
   
  

}

我是想将test.txt文件中的数据按行读入字符串数组str[5][100]中,然后屏幕上显示出来,可出来的总是can't open test.txt to read!
请问这怎么回事,如何改正?
test文件数据如下:
sdhj125620ghij
edgh230155lk
teyu156231kl
fhgf658512650
56324565895666645120
搜索更多相关主题的帖子: 文件 
2010-10-26 12:29
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>
//#include <fstream.h>  // 是C++中的!除掉就好了
#include <string.h>


int main(int argc, char* argv[])
{
    FILE *fp;
    char str[5][100];
    char strr[100];
    FILE *frFile;
    frFile=fopen("test.txt","r");
    int LineNum=0;
    if(frFile==NULL)
    {
        printf("can't open test.txt to read!\n");
        exit(0);
    }
    int i;
    for(i=0;i<5;i++)
    {
        fgets(strr,100,frFile);
        strncpy((char*)str[i],strr,100);

    }

    fclose(frFile);
    for (i=0;i<5;i++)
    {
        printf("%s\n",str[i]);
    }

    system("pause");
    return 0;
}

haole

If You Want Something, Go Get It, Period.
2010-10-26 13:05
fireice11
Rank: 1
等 级:新手上路
帖 子:11
专家分:4
注 册:2010-10-26
收藏
得分:0 
还是不行啊,一样的问题
2010-10-26 13:47
gmac
Rank: 2
等 级:论坛游民
帖 子:174
专家分:85
注 册:2010-9-28
收藏
得分:0 
把text.txt放在当前目录下
2010-10-26 13:51
cx12588
Rank: 2
来 自:河南
等 级:论坛游民
帖 子:49
专家分:74
注 册:2010-10-15
收藏
得分:0 
不懂, 但也要支持下!
2010-10-26 13:55
ycc892009
Rank: 2
等 级:论坛游民
帖 子:34
专家分:90
注 册:2009-12-23
收藏
得分:0 
这个文件名字你没有写错吧test.txt,不是text.txt。

test。txt放在你工程目录下与。cpp、。h是同一个目录的。

除了这两个原因就没有别的原因了。

要不你直接写全路径名就可以了。

到达理想的界面是我的目标,成功却不是快捷方式!
2010-10-26 17:20
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:10 
怎么不行啊!郁闷啊!我这行啊!肯定是test.txt 没放在当前路径!
图片附件: 游客没有浏览图片的权限,请 登录注册

If You Want Something, Go Get It, Period.
2010-10-26 19:03
fireice11
Rank: 1
等 级:新手上路
帖 子:11
专家分:4
注 册:2010-10-26
收藏
得分:0 
我更郁闷啊,文件名称和路径都没错啊,可就是出不来结果啊
2010-10-26 19:13
fireice11
Rank: 1
等 级:新手上路
帖 子:11
专家分:4
注 册:2010-10-26
收藏
得分:0 
错误已找到,感谢关注。
2010-10-26 20:00
快速回复:文件读取
数据加载中...
 
   



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

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