在C中编辑文本文件的内容

时间:2013-04-03 15:47:32

标签: c

我正在为即将到来的考试学习C,我正在努力解决这个问题。

这项工作是使用以下规则编辑文本文件:

  • 没有空间,。 ! :; ?
  • 之后只有一个空格,。 ! :; ?
  • 之后的第一封信。 ! ?缪斯是大写的

的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;
    }

我确实解决了规则2和3。

但是使用第一条规则,结果并不像我在我的代码中预期的那样。转换为规则2和3后,我反转内容,然后编辑它,然后再次重新获取它并放入source_file。

我设置了一个状态,1表示前一个字符是,。 ! :; ?,0是正常字符 结果是之后的所有空间,。 ! :; ?删除,但空格,。 ! :; ?仍然存在。

我的代码中的错误在哪里,请帮助我,抱歉很长的帖子:(

编辑(工作):

 /* 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;
  }

1 个答案:

答案 0 :(得分:3)

简约状态机。请注意,无需缓冲以前的字符,只需记住它们(通过statespacecount

/* 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;
}
相关问题