Duc Anh
Duc Anh

Reputation: 581

Edit content for textfile in C

I am studying C for the coming Test and I am struggling with this.

The work is edit text file with these rule :

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main()
{
int i=0,size;
char ch,source_file[100],*buffer;
FILE *source, *target;

printf("Enter name of file to copy\n");
gets(source_file);

source = fopen(source_file, "r");

if(!source)
{
    printf("File does not exist !");
    exit(EXIT_FAILURE);
}

target = fopen("test1.txt", "w");

/*
    Edit for rule number 2 and 3
 */
while( ( ch = fgetc(source) ) != EOF )  {
    if(i!=0)    {
        if(ch == ' ')   {
            if(i== 1)   {
                    fputc(ch,target);
                    i=2;
            }
            else if(i==2)   {
                    continue;
            }
        }
        else    {
            if(isalpha(ch)) {
                ch= toupper(ch);    
            }
            fputc(ch,target);
            i=0;
        }
    }
    else    {
        fputc(ch,target);
        if(ch==',' || ch=='.' || ch=='!' || ch==':' || ch==';' || ch=='?'){
            i=1;
        }
        else    {
            i=0;
        }
    }
}

 /**
 * Reverse and put text content into an array.
 * Then read array bottom up,put in source file
 * for requiment number 1 :
 */
fseek(target, 0, SEEK_END);
size = ftell(target);

buffer = (char *) malloc (size+1);
target= freopen("test1.txt","r",target);
for (i=(size-1); i>=0; i--)
{
    buffer[i] = fgetc(target);
}
buffer[size] = 0;

source =freopen(source_file,"w",source);
int state=0;
for(int k=(size-1);k>=0;k--)    {
    if(buffer[k]== EOF) {
        continue;
    }
    if(state== 1)   {
        if(buffer[k]== ' ') {
            continue;   
        }   
        else    {
            fputc(buffer[k],source);
            state= 0;
        }       
    }
    else    {
        if(buffer[k]== ',' || buffer[k]== '.' || buffer[k]== '!' || buffer[k]== ':' || buffer[k]== ';' || buffer[k]== '?')  {
            state= 1;   
        }
        fputc(buffer[k],source);
    }
}
printf("File copied successfully.\n");

  /**
   * Close all file
   */
fclose(source);
fclose(target);
if(remove("test1.txt")== 0) {
    printf("\nDelete successfully");
}
else {
    printf("\n Error");
}
return 0;
    }

I did solve the rule number 2 and 3.

But with 1st rule,the result is not as I expected on my code.After convert for rule 2 and 3,I reverse the content,then edit it,then revesre it again and put in the source_file.

I set a state,1 mean the previous character is , . ! : ; ?,0 is normal character The result is all space after , . ! : ; ? is delete,but space before , . ! : ; ? is still exist.

Where is the wrong in my code,pls help me out,sorry for long post :(

EDIT ( Worked ) :

 /* Rules:
  ** NO space before punctuation.
  ** only ONE space after punctuation
  ** after a .?! the next alpha character should be Capitalised
  */

  #include <stdio.h>
  #include <string.h>
  #include <ctype.h>
  #include <stdlib.h>

  #define STATE_INITIAL 0
  #define STATE_WORD 1
  #define STATE_KOMMA 2
  #define STATE_PERIOD 3
  #define STATE_EOF -1

  int main(void)
  {
unsigned spacecount;
int i=0,state;
char ch,source_file[100];
FILE *source, *target;

    spacecount= 0;

printf("Enter name of file to copy\n");
gets(source_file);

source = fopen(source_file, "r");

if(!source)
{
    printf("File does not exist !");
    exit(EXIT_FAILURE);
}

target = fopen("test1.txt", "w");

for (state=STATE_INITIAL; state != STATE_EOF; ) {
    ch = fgetc(source);
    switch(ch) {
    case ':' : case ',' : case ';' :
            spacecount=0;
            state = STATE_KOMMA;
            break;
    case '!' : case '.' : case '?' :
            spacecount=0;
            state = STATE_PERIOD;
            break;
    case ' ': spacecount++; continue;
    case EOF: state = STATE_EOF; continue;
    default:
            if (state == STATE_KOMMA || state == STATE_PERIOD) spacecount = 1;
            for( ;spacecount > 0; spacecount--) { fputc( ' ',target); }
            if (state == 3 && isalpha(ch))  ch = toupper(ch);
            state = STATE_WORD;
            break;
            }
    fputc(ch,target);
    }

  return 0;
  }

Upvotes: 1

Views: 133

Answers (1)

wildplasser
wildplasser

Reputation: 44220

Minimalistic state machine. Please note that there is no need for buffering previous characters, only remembering them (by means of state and spacecount)

/* Rules:
** NO space before punctuation.
** only ONE space after punctuation
** after a .?! the next alpha character should be Capitalised
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define STATE_INITIAL 0
#define STATE_WORD 1
#define STATE_KOMMA 2
#define STATE_PERIOD 3
#define STATE_EOF -1

int main(void)
{
unsigned spacecount;
int state,ch;

spacecount=0;
for (state=STATE_INITIAL; state > STATE_EOF; ) {
        ch = getc(stdin);
        switch(ch) {
        case ':' : case ',' : case ';' :
                spacecount=0;
                state = STATE_KOMMA;
                break;
        case '!' : case '.' : case '?' :
                spacecount=0;
                state = STATE_PERIOD;
                break;
        case ' ': spacecount++; continue;
        case EOF: state = STATE_EOF; continue;
        default:
                if (state == STATE_KOMMA || state == STATE_PERIOD) spacecount = 1;
                for( ;spacecount > 0; spacecount--) { putc( ' ', stdout); }
                if (state == 3 && isalpha(ch))  ch = toupper(ch);
                state = STATE_WORD;
                break;
                }
        putc(ch, stdout);
        }

return 0;
}

Upvotes: 3

Related Questions