"); strcat(cm-6ren">
gpt4 book ai didi

c - 如何将 "cut"命令的输出附加为循环中文件的下一列?

转载 作者:行者123 更新时间:2023-11-30 17:28:44 27 4
gpt4 key购买 nike

我需要从文件中剪切一列并将其附加到另一个文件作为其下一列。它必须在循环中完成,直到满足某些条件。我正在尝试以下代码

while(limit<inFeatures){
memset(outFile,'\0',499);
strcpy(outFile,outputPath);
strcat(outFile,"Top_");
sprintf(buffer,"%d",limit);
strcat(outFile,buffer);
strcat(outFile,"_features-FVT.csv");
int pos;
for(i=0;i<limit;i++){
pos=boolian[i]+1;
memset(cmd,'\0',999);
strcpy(cmd,"cut -d ',' -f");
sprintf(buffer,"%d",pos);
strcat(cmd,buffer);
strcat(cmd," ");
strcat(cmd,inputFVT);
strcat(cmd," >> ");
strcat(cmd,outFile);
system(cmd);
}
}

当我执行此操作时,结果将附加在输出文件的底部。我如何将结果附加为输出文件的下一列...?

最佳答案

感谢那些试图帮助我的人。我成功地完成了该代码。

inFile=fopen(inputFVT,"r");
//printf("\n%s",inputFVT);//For debugging purpose
if(inFile==NULL){
printf("1.Cannot open the input File");exit(0);
}

count = 0;
//int retVal;

while(count<totalFeatures){
tot_features[count]=(char *)calloc(50,sizeof(char));
memset(tot_features[count],'\0',49);
int retVal = fscanf(inFile,"%[^,]",tot_features[count]);
fflush(inFile);
ch = fgetc(inFile);
fflush(inFile);
if(retVal == -1) break;
//printf("1. %s\n", tot_features[count]); //For debugging purpose
count = count + 1;

}
count = 0;
inputFeatures=fopen(argv[2],"r");
//printf("\n%s",argv[2]);//For debugging purpose
if(inputFeatures==NULL){
printf("2.Cannot open the input File");exit(0);
}
count = 0;
//int retVal;

while(count<inFeatures){
in_features[count]=(char *)calloc(50,sizeof(char));
memset(in_features[count],'\0',49);
int retVal=fscanf(inputFeatures,"%[^\n]",in_features[count]);
fflush(inputFeatures);
if(retVal == -1) break;
//printf("2. %s\n", in_features[count]); //For debugging purpose
ch = fgetc(inputFeatures);
fflush(inputFeatures);
count = count + 1;
}
count = 0;
for(i=0;i<inFeatures;i++){
for(j=0;j<totalFeatures;j++){
if((strcmp(in_features[i],tot_features[j]))==0){
//printf("3. \n%s %s",in_features[i],tot_features[j]);//For debugging purpose
boolian[i]=j+1;
break;
}else{
boolian[i]=-1;
}
count++;
}

}

int limit=100;
char *buffer,temp[5];
char outFile[500];
//char cmd[1000];
buffer=(char*)calloc((500+count+count),sizeof(char));
while(limit<inFeatures){
memset(outFile,'\0',499);
strcpy(outFile,outputPath);
strcat(outFile,"Top_");
sprintf(temp,"%d",limit);
strcat(outFile,temp);
strcat(outFile,"_features-FVT.csv");
strcpy(buffer,"cut -d ',' -f ");
for(i=0;i<limit;i++){
sort[i]=boolian[i];
}
quicksort(sort,0,limit-1);
int pos;
for(i=0;i<limit;i++){
pos=sort[i];
sprintf(temp,"%d,",pos);
strcat(buffer,temp);
}
sprintf(temp,"%d",totalFeatures+1);
strcat(buffer,temp);

strcat(buffer," ");
strcat(buffer,inputFVT);
strcat(buffer," > ");
strcat(buffer,outFile);
//printf("\n%s",buffer);//Debuging code
system(buffer);
limit=limit+100;
}
strcpy(outFile,outputPath);
strcat(outFile,"Top_");
sprintf(temp,"%d",inFeatures);
strcat(outFile,temp);
strcat(outFile,"_features-FVT.csv");
strcpy(buffer,"cut -d ',' -f");
for(i=0;i<inFeatures;i++){
sort[i]=boolian[i];
}
quicksort(sort,0,inFeatures-1);
int pos;
for(i=0;i<inFeatures;i++){
pos=sort[i];
sprintf(temp,"%d,",pos);
strcat(buffer,temp);
}
sprintf(temp,"%d",totalFeatures+1);
strcat(buffer,temp);
strcat(buffer," ");
strcat(buffer,inputFVT);
strcat(buffer," > ");
strcat(buffer,outFile);
system(buffer);

}

关于c - 如何将 "cut"命令的输出附加为循环中文件的下一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25965948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com