Chinaunix

标题: 一个SUN Unix环境下的C语言编译问题 [打印本页]

作者: blijiang    时间: 2007-03-16 15:15
标题: 一个SUN Unix环境下的C语言编译问题
我在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下运行?
作者: arenxl    时间: 2007-03-16 15:55
应该没问题的,楼主段一段的调试吧,我现在就移植了一个windows下的com程序到soloaris下面了,调试全通过了,不过现在还是忙,不忙倒可以帮你看一下
作者: week008    时间: 2007-03-16 16:33
可能没有加头文件:
#inclued <stdio.h>
作者: wuqing    时间: 2007-03-16 17:11
-bash-3.00# CC -o abc abc.cpp
-bash-3.00# ./abc
Usage: ee608project1 #filename
-bash-3.00#            

没有任何问题
你是不是用错编译器了?
你用cc?
作者: webstroller    时间: 2007-03-17 15:09
标题: 回复 1楼 blijiang 的帖子
2 #include <iostream>
  3 #include <fstream.h>
  4 #include <stdlib.h>
  5 #include <stdio.h>

这样引用就OK了
作者: bGFuZ3Vl    时间: 2007-03-17 15:21
标题: 回复 5楼 webstroller 的帖子
.

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

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

.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2