免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1886 | 回复: 5
打印 上一主题 下一主题

[C] 一个SUN Unix环境下的C语言编译问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-16 15:15 |只看该作者 |倒序浏览
我在windows下编写的C代码,被要求在SUN Unix环境下运行。源代码如下:
//#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream.h>

int isdigit(char t)
{
return( t>='0'&&t<='9');
}

int main(int argc, char *argv[]){
if(argc!=2){
printf("Usage: ee608project1 #filename\n");
return 0;
}
char* fn=argv[1];
int N_num, L_num;
int c[3];
char str[50];
char temp[3];
int nLine=0;
ifstream infile(fn);
if (!infile){
printf("Sorry, we are unable to open the file!\n");
exit(0);
}
infile.getline(temp, 5);
N_num=atoi(temp); //number of nodes
infile.getline(temp, 5);
L_num=atoi(temp); //number of links
int* a;
a=(int* )malloc(N_num*sizeof(int*));
int* b;
b=(int* )malloc(N_num*sizeof(int*));
for (int k=0; k<N_num; k++)
{
a[k]=b[k]=0;
}
printf("---------------------------\nNode Links Costs\n");
while(!infile.eof())
{
infile.getline(str,200); /*read one line*/
int flag=0;
int q=0;
int len=strlen(str);
for (int i=0; i<len; i++){
if (isdigit(str[i]))
{
if (flag) c[q-1]=c[q-1]*10+str[i]-'0';
else
c[q++]=str[i]-'0';
flag=1;
}
else
flag=0;
}
//printf("%d %d %d\n", c[0],c[1],c[2]);
a[c[0]]+=c[2];
b[c[0]]+=1;
a[c[1]]+=c[2];
b[c[1]]+=1;
}
for (int i=0; i<N_num; i++){
printf("%d %d %d\n", i, b[i], a[i]);
}
infile.close();
}
当我执行g++ test.cpp -g -o run命令时,提示错误信息如下:
test.cpp:6: parse error before `+'
test.cpp: In function `int main(int, char **)':
test.cpp:13: implicit declaration of function `int printf(...)'
请问我该如何修改源代码,以使能在SUN UNIX下运行?

论坛徽章:
0
2 [报告]
发表于 2007-03-16 15:55 |只看该作者
应该没问题的,楼主段一段的调试吧,我现在就移植了一个windows下的com程序到soloaris下面了,调试全通过了,不过现在还是忙,不忙倒可以帮你看一下

论坛徽章:
0
3 [报告]
发表于 2007-03-16 16:33 |只看该作者
可能没有加头文件:
#inclued <stdio.h>

论坛徽章:
0
4 [报告]
发表于 2007-03-16 17:11 |只看该作者
-bash-3.00# CC -o abc abc.cpp
-bash-3.00# ./abc
Usage: ee608project1 #filename
-bash-3.00#            

没有任何问题
你是不是用错编译器了?
你用cc?

论坛徽章:
0
5 [报告]
发表于 2007-03-17 15:09 |只看该作者

回复 1楼 blijiang 的帖子

2 #include <iostream>
  3 #include <fstream.h>
  4 #include <stdlib.h>
  5 #include <stdio.h>

这样引用就OK了

论坛徽章:
0
6 [报告]
发表于 2007-03-17 15:21 |只看该作者

回复 5楼 webstroller 的帖子

.

fstream.h 只是用来进行向后兼容的。请用 fstream

另外,推荐在 C++ 中只用 C++ 本身的输入输出设施,而不要用 C 库函数。

.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP