*Flails*

This is a discussion on *Flails* within the C Programming forums, part of the General Programming Boards category; Yep, i've got my self stuck again. i've decided to write a program for my Dad, he gets text off ...

  1. #1
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420

    *Flails*

    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:


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXSIZE 1000

    int main()

    {
    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");
    exit(0);
    }
    outfile = fopen("datamod.txt", "w");


    for (a=1; a<10; a++)
    {
    fgets(arr, MAXSIZE, infile);

    x = strlen(arr);

    buffer[0] = arr[0];
    buffer[1] = '.';
    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]==' ')
    z++;

    else break;

    c = x-z;

    for (y=0; y<c; y++)
    temp[y] = buffer[y];
    temp[c] = buffer[c];
    temp[c+1]= '(';

    for (y=2; y<25; y++)
    temp[c+y] = buffer[c+y-1];

    printf("%s", temp); /**** for debugging *****/
    fputs(temp, outfile);
    }
    return 0;
    }


    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.

    Advice?

    Thanks in advance.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It seems to me that you aren't making any real changes to the file except changing newline characters to a comma.
    Code:
    /* Pseudocode */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      FILE *IN, *OUT;
      int f;
      if ( ( IN = fopen ( "input.txt", "r" ) ) != NULL && 
           ( OUT = fopen ( "output.txt", "w" ) ) != NULL )
      {
        while ( ( f = fgetc ( IN ) ) != EOF )
          if ( f == '\n' ) fputc ( ',', OUT );
          else fputc ( f, OUT );
        fclose ( IN );
        fclose ( OUT );
      }
      else
        fprintf ( stderr, "Error: File open failure\n" );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Thanks for showing me some "tidy" code

    But as well as removing the newline chars and replacing with commas, (which i did manage albeit in a rather more convoluted fashion than your code) I need to place brackets around the last word or words on each line that are in captials.

    I don't understand why my code doesn't work.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If all of the records in the file are formatted like the ones you used as an example, simply count spaces. When you count two, place an opening brace. When you reach a newline, place a closing brace and then handle the newline by replacing it with a comma.

    -Prelude
    My best code is written with the delete key.

  5. #5
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Thanks, a lot Prelude!

    *Goes to re-write his code*

  6. #6
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Well I rewrote my code to use your much neater nicer version Prelude, I also did what you suggested and counted the spaces inorder to put the brackets where i wanted them.

    So far so good

    Now, inorder to put the fullstop after the number at the beggining of each line i thought i would place a '.' after each digit and add a counter so that if the char got from the orginal file was the second digit on a given line it would place it over the character previously written (which would be a '.')

    IE. it would get a '1' print it to file, then print a '.' to file, the if it got another number, say a '0', it would place it over the '.' just written and then put another '.'

    So that i wouldn't get "1.0.", and instead have "10."


    Thing is I thought i could just subtract 1 from the file pointer....... didn't work.

    My code is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
      FILE *infile, *outfile;
      int x, f, c=0;
      if ( ( infile = fopen ( "results.txt", "r" ) ) != NULL && 
           ( outfile = fopen ( "modresults.txt", "w" ) ) != NULL )
      {
        while ( ( f = fgetc (infile) ) != EOF )
          switch(f)
          {
            case '\n': 
            {
               x=0; 
               c=0;
               fputc ( ')' , outfile ); 
               fputc ( ',' , outfile ); 
               fputc ( ' ' , outfile );
            }
            break;
            case ' ': 
            {
               fputc ( f , outfile );  
               if ( ++x == 3 ) fputc( '(', outfile );
            }
            break; 
            case '1':
    	case '2':
    	case '3':
    	case '4':
    	case '5':
    	case '6':
    	case '7':
    	case '8':
    	case '9':
    	case '0':
            {
               if (c++ == 1) 
               fputc ( f, outfile-1); /* problem line*/
               else fputc( f, outfile);
               fputc ( '.', outfile);
            }
            break;
            default: 
               fputc ( f, outfile );
           }
        fclose ( infile );
        fclose ( outfile );
      }
      else
        fprintf ( stderr, "Error: File open failure\n" );
      
      return 0;
    }
    Instead of getting "10." i get "1..", which remains the same if i change the pointer to outfile-2. IE. the second digit which should be printing over the first '.' isn't printing at all.


    EDIT: Ah, solved it, found the fseek() function. All working as planned now. Thanks for your help Prelude.
    Last edited by Clyde; 03-27-2002 at 06:06 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21