Hi,
I am trying to create a code that will find words and organize them in a text file in the following manner:
For the following input text file:
Code:
[PARAMETER: Left Pedal]
Mnemonic="Left Pedal";
Operational check=1;
Word=232;
[PARAMETER: Right Pedal]
Mnemonic="Right Pedal";
Operational check=1;
Word=232;
[PARAMETER: Character 1]
Mnemonic="Character 1";
Operational check=1;
Word=192;
[PARAMETER: Size]
Mnemonic="Size";
Decimal places=0;
Word=192;
I would like the output to be the following:
Code:
Character 1 192
Size 192
Left Pedal 232
Right Pedal 232
So the code extracted all Mnemonic names from the input file and their word number. It proceeded to organize them in order according to their word number.
Here is what I have so far:
Code:
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#define MAX_LEN_LINE 300 // ?
int main()
{
const char mnemonic[80] = "Mnemonic=";
char buffer[MAX_LEN_LINE+2];
char *buff_ptr, *find_ptr;
fpos_t position;
FILE *fp1, *fp2;
size_t find_len = strlen(mnemonic);
fp1 = fopen("originalfile.txt","r");
fp2 = fopen("filecreated.txt","w");
while(fgets(buffer,MAX_LEN_LINE+2,fp1))
{
buff_ptr = buffer;
while ((find_ptr = strstr(buff_ptr,mnemonic)))
{
fgetpos (fp1, &position);
char avoid[]=";";
int i;
i = strcspn (buff_ptr,avoid);
fsetpos (fp1, &position);
fgets ( buffer, (i-2), fp1 );
while(buff_ptr < find_ptr)
fputc((int)*buff_ptr++,fp2);
fputs("\n",fp2);
fputs(buffer,fp2);
buff_ptr += find_len;
}
}
fclose(fp2);
fclose(fp1);
return 0;
}
The code above was supposed to extract all parameter names from the input, but for some reason it prints the line proceeding the one where the Mnemonic is found and cuts words.
Here's my output:
Code:
Operational check=
Operational check=1
Operational check=1
Decimal plac
Can anyone please point me in the right direction?