我在C编程中组合两个字符串有困难,我希望能够从命令行参数中获取输入文件名,并将.out
添加到文件名作为输出文件new名称。例如Test1.txt
- > Test1.txt.out
以下代码因未知原因产生分段错误。
int main(int argc, char** argv)
{
char fileName_Out[200];
Consortium *con1;
int i;
for(i=0; i<argc; i++)
{
strcpy(fileName_Out, argv[i]);
strcat(fileName_Out, ".out");
con1 = readConsortium (argv[i]);
writeNetWorth (fileName_Out, con1);
}
free(con1->core);
free(con1->associate);
free(con1);
con1->core = NULL;
con1->associate = NULL;
con1 = NULL;
return 0;
}
使用完整代码进行更新:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char code[4];
float sharePrice;
int shares;
float assetValue;
float debts;
} Company;
typedef struct {
int numCore;
int numAss;
Company* core;
Company* associate;
} Consortium;
Consortium *readConsortium (char* fileName) {
Consortium *con1 = (Consortium*)malloc(sizeof(Consortium));
int i;
FILE *source_f = fopen(fileName, "r");
if(source_f == NULL)
{
con1 = NULL;
} else {
fscanf(source_f, "%d %d", &(con1->numCore), &(con1->numAss));
con1->core = (Company*)malloc(sizeof(Company)*(con1->numCore));
con1->associate = (Company*)malloc(sizeof(Company)*(con1->numAss));
for(i = 0; i < con1->numCore; i++)
{
fscanf(source_f, "%s %f %d %f %f", con1->core[i].code, &con1->core[i].sharePrice, &con1->core[i].shares, &con1->core[i].assetValue, &con1->core[i].debts);
}
for(i = 0; i < con1->numAss; i++)
{
fscanf(source_f, "%s %f %d %f %f", con1->associate[i].code, &con1->associate[i].sharePrice, &con1->associate[i].shares, &con1->associate[i].assetValue, &con1->associate[i].debts);
}
}
fclose(source_f);
return con1;
}
void writeNetWorth (char* fileName_Out, Consortium *con)
{
int i;
float netWorth;
FILE* target_f = fopen(fileName_Out, "w");
for(i = 0; i < con->numCore; i++)
{
netWorth = (con->core[i].sharePrice * con->core[i].shares) + con->core[i].assetValue - con->core[i].debts;
fprintf(target_f, "%s:%12.2f\n", con->core[i].code, netWorth);
}
for(i = 0; i < con->numAss; i++)
{
netWorth = (con->associate[i].sharePrice * con->associate[i].shares) + con->associate[i].assetValue - con->associate[i].debts;
fprintf(target_f, "%s:%12.2f\n", con->associate[i].code, netWorth);
}
fclose(target_f);
}
/* int main(void)
{
char fileName[200];
char fileName_Out[200];
Consortium *con2;
scanf("%s %s", fileName, fileName_Out);
con2 = readConsortium (fileName);
writeNetWorth (fileName_Out, con2);
free(con2->core);
free(con2->associate);
free(con2);
con2->core = NULL;
con2->associate = NULL;
con2 = NULL;
return 0;
}*/
int main(int argc, char** argv)
{
char fileName_Out[200];
Consortium *con1;
int i;
for(i=1; i<(argc+1); i++)
{
strcpy(fileName_Out, argv[i]);
strcat(fileName_Out, ".out");
con1 = readConsortium (argv[i]);
writeNetWorth (fileName_Out, con1);
}
free(con1->core);
free(con1->associate);
free(con1);
con1 = NULL;
return 0;
}
答案 0 :(得分:1)
两件事。
您确定使用fileName_Out[200]
时内存不足吗?来自strcat()
的{{3}}
dest字符串必须有足够的空间用于结果。如果dest不够大,程序行为是不可预测的;
您似乎从未在已显示的代码中为 con1
指针分配内存。 [考虑到你没有向我们展示readConsortium()
定义]。
我认为您应该避免 free
- con1-> ..
main()
,因为con
没有for
的分配。
编辑:
您的问题出现在代码中的其他位置。使用for(i=1; i<(argc+1); i++)
循环指定
for(i=1; i<argc; i++).
你已经跑出了界限。将该条件更改为
n
Remenber,数组中的n-1
元素的索引始终为{{1}}。
答案 1 :(得分:0)
对我来说,似乎con1变为NULL但你试图在writeNetWorth中使用该指针而不检查NULL。
for(i = 0; i < con->numCore; i++)
当con为NULL时会导致段错误。
con1变为NULL的原因很可能是你无法打开你从“argv [0] .out”获得的yourprogram.exe.out。
作为旁注,似乎你的程序有内存泄漏,在循环中动态分配内存而不会在同一循环中释放内存。