为什么下面用strcpy没赋值?

ljm19891118 2009-12-02 06:34:36
文本文件shuju.txt里面的内容是

lijimeng nan
wang nv
mashangjian nan
xieyuexiao nan

下面执行了strcpy(woman[j],c);
strcpy(man[j],c);
在输出的时候还是null
不知道为什么没有复制上


#include<iostream.h>
#include<string.h>
#include <fstream.h>
#include<windows.h>
#include <stdio.h>
void main()
{
char man[100][100],woman[100][100];
int i,j,k,m;
// char a[256],b[256];
i=j=0;
k=1;

for( m=0;m <100;m++)
{
strcpy(man[m],"null");
strcpy(woman[m],"null");
}

ifstream fin("shuju.txt");
char c[128],d[128];
while(fin>>c)
{
fin>>d;
if(strcmp(d,"nv"))
{
strcpy(man[i],c);
i++;
cout<<"nan"<<c<<endl;
cout<<"nan"<<man[i]<<endl;
}
if(strcmp(d,"nan"))
{
strcpy(woman[j],c);
j++;
cout<<"nv"<<c<<endl;
cout<<"nv"<<woman[j]<<endl;
}
} fin.close();
}
...全文
146 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljm19891118 2009-12-02
  • 打赏
  • 举报
回复
嗯 解决了
换成了128

ifstream fin("shuju.txt");
char c[128],d[128];
while(fin>>c)
{
fin>>d;
if(strcmp(d,"nv"))
{
strcpy(man[i],c);

cout<<"nan"<<c<<endl;
cout<<"nan"<<man[i]<<endl;
i++;
}
if(strcmp(d,"nan"))
{
strcpy(woman[j],c);

cout<<"nv"<<c<<endl;
cout<<"nv"<<woman[j]<<endl;
j++;
}
} fin.close();


ljm19891118 2009-12-02
  • 打赏
  • 举报
回复
嗯 刚才加了点测试的代码没注意
BTzjzxxx 2009-12-02
  • 打赏
  • 举报
回复
我的天,调了半天还……首先是你的c和d都是128的,但是目的的man和woman都是100的,这样strcpy应该不成功的吧,最后,最重要的是:你应该把i++和j++移到cout后面去!
voidsky 2009-12-02
  • 打赏
  • 举报
回复
strcpy(woman[j],c)直接这样是不行得,你上面定义得是c[128]换成这样strcpy(woman[j],c[j])

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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