#include <stdio.h> // standard input output
#include <stdlib.h> // standard bibliothek
#include <string.h> // strings
#define MAX_LINE_LENGTH 300 // maximal length of a line of the input file
#define Max_LINE_LENGHT_APPENDED 1000 // maximal length of full appendet line
#define MAX_LINES 59 // maximal lines of the original input file
int main()
{
const char FILEIN[] = "/tmp/abc123";
FILE *fpp=fopen("/tmp/abc123", "w");
fputs(" \n"
" \n"
" \n"
" PERMNO date CUSIP NCUSIP COMNAM TICKER PERMCO SHRCD SHRCLS ISSUNO EXCHCD HEXCD SICCD HSICCD HSICMG HSICIG NAMEENDT TSYMBOL NAICS PRIMEXCH TRDSTAT SECSTAT\n"
" \n"
" 10000 06/01/1986 68391610 7952 . 10396 . 3 . 3990 39 399 . \n"
" 10000 07/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 03/12/1986 OMFGA Q A R \n"
" 10000 08/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 09/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 10/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 13/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 14/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 15/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 16/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 17/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 20/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 21/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 22/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 23/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 24/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" 10000 27/01/1986 68391610 68391610 OPTIMUM MANUFACTURING INC OMFGA 7952 10 A 10396 3 3 3990 3990 39 399 . OMFGA Q A R \n"
" \n"
" PRC VOL OPENPRC ASKHI BIDLO BID ASK NUMTRD RET RETX SHROUT SHRFLG SHRENDDT DLSTCD NWPERM NEXTDT DLAMT DLPRC DLPDT DLRET\n"
" \n"
" . . . . . . . . . . . . . . . . . . . . \n"
" -2.56250 1000 . 2.75000 2.37500 . . . C C 3680 2 30/01/1986 . . . . . . . \n"
" -2.50000 12800 . 2.62500 2.37500 . . . -0.024390 -0.024390 3680 . . . . . . . . . \n"
" -2.50000 1400 . 2.62500 2.37500 . . . 0.000000 0.000000 3680 . . . . . . . . . \n"
" -2.50000 8500 . 2.62500 2.37500 . . . 0.000000 0.000000 3680 2 . . . . . . . . \n"
" -2.62500 5450 . 2.75000 2.50000 . . . 0.050000 0.050000 3680 . . . . . . . . . \n"
" -2.75000 2075 . 2.87500 2.62500 . . . 0.047619 0.047619 3680 . . . . . . . . . \n"
" -2.87500 22490 . 3.00000 2.75000 . . . 0.045455 0.045455 3680 . . . . . . . . . \n"
" -3.00000 10900 . 3.12500 2.87500 . . . 0.043478 0.043478 3680 . . . . . . . . . \n"
" -3.00000 8470 . 3.12500 2.87500 . . . 0.000000 0.000000 3680 . . . . . . . . . \n"
" -3.00000 1000 . 3.12500 2.87500 . . . 0.000000 0.000000 3680 . . . . . . . . . \n"
" -3.00000 1000 . 3.12500 2.87500 . . . 0.000000 0.000000 3680 . . . . . . . . . \n"
" -3.00000 2700 . 3.12500 2.87500 . . . 0.000000 0.000000 3680 . . . . . . . . . \n"
" -3.75000 24000 . 3.87500 3.62500 . . . 0.250000 0.250000 3680 2 . . . . . . . . \n"
" -4.18750 11372 . 4.37500 4.00000 . . . 0.116667 0.116667 3680 2 . . . . . . . . \n"
" -4.43750 16570 . 4.50000 4.37500 . . . 0.059701 0.059701 3680 2 . . . . . . . . \n"
" \n"
" DLRETX DISTCD DIVAMT FACPR FACSHR CFACPR CFACSHR DCLRDT RCRDDT PAYDT ACPERM ACCOMP TRTSCD NMSIND MMCNT NSDINX vwretd vwretx ewretd ewretx sprtrn\n"
" \n"
" . . . . . . . . . . . . 4 1 0 1 -0.000138 -0.000525 0.001926 0.001783 -0.001091\n"
" . . . . . 1.00000 1.00 . . . . . 1 1 9 2 0.013809 0.013800 0.011061 0.011046 0.014954\n"
" . . . . . 1.00000 1.00 . . . . . . . . . -0.020744 -0.020750 -0.005117 -0.005135 -0.027268\n"
" . . . . . 1.00000 1.00 . . . . . . . . . -0.011219 -0.011315 -0.011588 -0.011659 -0.008944\n"
" . . . . . 1.00000 1.00 . . . . . 1 1 10 2 0.000083 0.000047 0.003651 0.003632 -0.000728\n"
" . . . . . 1.00000 1.00 . . . . . . . . . 0.002750 0.002680 0.002433 0.002369 0.003690\n"
" . . . . . 1.00000 1.00 . . . . . . . . . 0.000366 0.000314 0.004474 0.004439 -0.000387\n"
" . . . . . 1.00000 1.00 . . . . . . . . . 0.008206 0.008203 0.007693 0.007683 0.007840\n"
" . . . . . 1.00000 1.00 . . . . . . . . . 0.004702 0.004655 0.005670 0.005653 0.004370\n"
" . . . . . 1.00000 1.00 . . . . . . . . . -0.001741 -0.001924 0.003297 0.003222 -0.003538\n"
" . . . . . 1.00000 1.00 . . . . . . . . . -0.003735 -0.003735 -0.001355 -0.001356 -0.004318\n"
" . . . . . 1.00000 1.00 . . . . . . . . . -0.006992 -0.007012 -0.003472 -0.003494 -0.008384\n"
" . . . . . 1.00000 1.00 . . . . . . . . . -0.009593 -0.009605 -0.004588 -0.004599 -0.011176\n"
" . . . . . 1.00000 1.00 . . . . . 1 1 11 2 0.002665 0.002659 0.001397 0.001386 0.003735\n"
" . . . . . 1.00000 1.00 . . . . . 1 1 10 2 0.009683 0.009623 0.006771 0.006750 0.010673\n"
" . . . . . 1.00000 1.00 . . . . . 1 1 11 2 0.004343 0.004120 0.002140 0.001935 0.004650\n"
,fpp);
fclose(fpp);
// create new file for saving total appended line
FILE *outputFile = fopen("output.txt", "w");
if (outputFile == NULL) {
perror("Error creating output file");
return 1;
}
FILE *fp = fopen("/tmp/abc123", "r");
// Read the lines from the file, the lines are stored in a 2D array for manipulation.
char lines[MAX_LINES][MAX_LINE_LENGTH];
int lineCount = 0;
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), fp) != NULL) {
// Remove newline character from the end of the line
if (line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0';
strcpy(lines[lineCount], line);
lineCount++;
}
fclose(fp);
// Append line 41 to 22 and 9 to the end of line 3
char appendedLine[MAX_LINES][Max_LINE_LENGHT_APPENDED];
appendedLine[0][Max_LINE_LENGHT_APPENDED]=sprintf(appendedLine[0], "%s%s%s\n", lines[3], lines[22], lines[41]);
int i=0;
int j=0;
int k=0;
int m=0;
int count=0;
for(i=1,j=5,k=24,m=43; i<=20,j<=20,k<=39, m<=58; i++,j++,k++,m++)
{
appendedLine[i][Max_LINE_LENGHT_APPENDED]=sprintf(appendedLine[i], "%s%s%s\n", lines[j], lines[k], lines[m]);
count++;
}
// Print the result in a new file
for(int i=0;i<=count;i++)
{
fprintf(outputFile,"%s", appendedLine[i]);
}
return 0;
}