- 论坛徽章:
- 0
|
我在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下运行? |
|