I'm having difficulty combining two string in C Programming, I want to be able to take an input files name from the command-line parameters and add .out to the files name as the output files new name. e.g. Test1.txt -> Test1.txt.out
The code below produces a segmentation fault for an unknown reason.
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;
}
Update with whole code:
#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;
}
Two things.
Are you sure, you're not running out of memory while using
fileName_Out[200]? From the man page ofstrcat()You have never seem to allocate memory tocon1pointer in the code you've shown. [Considering you didn't show usreadConsortium()definition].I think you should refrain fromfree-ingcon1-> ..as in yourmain()there is no allocation forcon.Edit:
Your problem was somewhere else in the code. with the
forloop specifyingyou're running out of bounds. Change that condition to
Remenber, the
nth element in array will always have an index ofn-1.