Yep, i've got my self stuck again.
i've decided to write a program for my Dad, he gets text off the web in tabular form and has to convert it manually to linear text, so i thought i'd write a small program for him.... well i'm so close! But alas i'm stuck.
The text comes in like this:
1 HORTON Robert SOLIHULL
2 MARTIN Andy THE HALL
3 SADIQ Aladdin SALLE PAUL
3 WATKISS Lindsay NOTTS CAVILIERS
5 LAW Ryan CANTERBURY
6 KENNEA Paul NOTTS CAVILIERS
7 THOMAS David WARWICK UNI
8 PETTIT William MORECAMBE
9 BARNES-WEBB Richard SWASH & BUCKLE
and has to end up like this:
1. HORTON Robert (SOLIHULL), 2. MARTIN Andy (THE HALL), 3= SADIQ Aladdin (SALLE
PAUL), 3= WATKISS Lindsay (NOTTS CAVILIERS), 5. LAW Ryan (CANTERBURY), 6.
KENNEA Paul (NOTTS CAVILIERS), 7. THOMAS David (WARWICK UNI), 8. PETTIT William
(MORECAMBE), 9. WARNES-WEBB Richard (SWASH & BUCKLE),
now, i'm ok except for the first bracket on each line, which i'm having difficulty on.
The code i have so far is:
#define MAXSIZE 1000
FILE *infile, *outfile;
char temp[MAXSIZE], buffer[MAXSIZE], arr[MAXSIZE];
int a, y, x, c, z = 0;
if ((infile = fopen("data.txt", "r")) == NULL)
printf("Error: Unable to open data.txt");
outfile = fopen("datamod.txt", "w");
for (a=1; a<10; a++)
fgets(arr, MAXSIZE, infile);
x = strlen(arr);
buffer = arr;
buffer = '.';
for (y=2; y<50; y++)
buffer[y] = arr[y-1];
buffer[x] = ')';
buffer[x+1] = ',';
buffer[x+2] = ' ';
buffer[x+3] = arr[x];
for (y=1; y<50; y++)
if (buffer[x-y]=='A'||buffer[x-y]=='B'||buffer[x-y]=='C'||buffer[x-y]=='D'||buffer[x-y]=='E'||buffer[x-y]=='F'||buffer[x-y]=='G'||buffer[x-y]=='H'||buffer[x-y]=='I'||buffer[x-y]=='J'||buffer[x-y]=='K'||buffer[x-y]=='L'||buffer[x-y]=='M'||buffer[x-y]=='N'||buffer[x-y]=='O'||buffer[x-y]=='P'||buffer[x-y]=='Q'||buffer[x-y]=='R'||buffer[x-y]=='S'||buffer[x-y]=='T'||buffer[x-y]=='U'||buffer[x-y]=='V'||buffer[x-y]=='W'||buffer[x-y]=='X'||buffer[x-y]=='Y'||buffer[x-y]=='Z'||buffer[x-y]==' ')
c = x-z;
for (y=0; y<c; y++)
temp[y] = buffer[y];
temp[c] = buffer[c];
for (y=2; y<25; y++)
temp[c+y] = buffer[c+y-1];
printf("%s", temp); /**** for debugging *****/
Now i realise to all you programming Gods out there it probably looks like a hideous mishmash, but hey this only my 4th day of learning C! (though I will graciously accept any advice )
Basically the program reads in a line of text then copies it to an array called "buffer", it also adds in a "." after the number, and at the end of each line places a ")" and a ",".
Then (and heres where my problem is) I decided that to get the first bracket in, I would count how many capitals there were from the end of the each line then, copy buffer to a second array called "temp" along with a "(" at the appropriate place.
The "buffer" array works (atleast it appears to when i either print it to the screen, or alter the fputs() function to place buffer instead of array into the file i'm writing to. Unfortuneately the last part doesn't work.
temp ends up containing:
1. HORTON Robert (SOLIHULL), 2. MAR(TIN Andy THE HALL),
I'm lost, i thought that if it worked for the first line (which it seems to) it would work for the rest.
Thanks in advance.