2015-05-23 99 views
0

我试图根据用户输入创建一个在行之间插入文本的函数。用户必须指定行号和索引才能插入他的行。将文本插入到特定行中

目前,我已经设法插入行前的文本,但我不能将其插入到行“索引”。 有谁知道如何根据索引号插入? PS。我仍然是C编程的入门者。我知道文件打开和关闭的次数太多了!

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

void inserttext(void); 

main() 
{ 
    inserttext(); 
}  

void inserttext(void) 
{ 
    FILE *file1,*file2; 
    char *f = malloc(sizeof(char)), *t = malloc(sizeof(char)); 
    int l,i,r,y,n,index,nl=0; 

    printf("Enter a text file name: "); 
    scanf("%s",f); 

    if (access(f,F_OK)!=-1)//if the text file exists 
    { 
     file1=fopen(f, "r+"); 
     file2=fopen("f2.txt", "w+"); 
     printf("\nThe file before editing:\n\n"); 
     while((n=fgetc(file1))!=EOF)// to show the contents of the file before the edit 
     { 
      putchar(n); 
     } 
     fclose(file1); 
     fclose(file2); 

     if(access(f,W_OK)!=-1)//if the file has the write permission 
     { 
      file1=fopen(f, "r+"); 
      file2=fopen("f2.txt", "w+"); 

      printf("\n\nPlease enter your text: \n"); 
      scanf(" %[^\n]s ",t); 

      printf("Specify the line number where you want to insert: "); 
      scanf("%d", &l); 

      printf("\nindex:\n"); 
      scanf("%d", &index); 

      while((r=fgetc(file1))!=EOF)//copying file1 contents into file2 contents 
      { 
       fputc(r,file2); 
       if(r == '\n' && ++nl == l){ 
        fprintf(file2, "%s ", t);//adding the inserted text 
       } 
      } 
      fclose(file1); 
      fclose(file2); 

      file1=fopen(f, "w+"); 
      file2=fopen("f2.txt", "r"); 
      while((y=fgetc(file2))!=EOF){ 
        fputc(y,file1); 
      } 
      fclose(file2); 
      fclose(file1); 
      remove("f2.txt"); 

      file1=fopen(f, "r"); 
      printf("\n"); 
      while((i=fgetc(file1))!=EOF)//showing the result after inserting 
      { 
       putchar(i); 
      } 
      fclose(file1); 
      free(f); 
      free(t); 
      } 
      else{ 
      printf("\n%s text file does not have the Write Permission!", f); 
      free(f); 
      free(t); 
      return; 
       } 
      }else{ 
       printf("file doesn't exits!\n"); 
      } 
} 
+2

'char * f = malloc(sizeof(char))'是否正确? – Subinoy

+1

您只能阅读索引,但不要检查也不要使用它。你的意思是什么?列号? – LeleDumbo

+3

@Subinoy:很好。他可能很幸运不会写入无效内存(但可能会覆盖一些有效的内存)。 – LeleDumbo

回答

1

这使用ftell()fseek()保存文件位置在所选择的行的开始,读出的线的长度,并返回到行的开始。
提示用户输入小于行长度的行中的索引。 我确实得到了原始malloc对* t和* f的分段错误。我尝试了一些更长的输入,所以这为每个指针分配了100个字符。

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

void inserttext(void); 

int main() 
{ 
    inserttext(); 
    return 0; 
} 

void inserttext(void) 
{ 
    FILE *file1,*file2; 
    char *f = malloc(100), *t = malloc(100); 
    int l,i,r,y,n,index,nl=0; 
    int linelength = 0;; 
    long offset = 0; 

    printf("Enter a text file name: "); 
    scanf("%99s",f); 

    if (access(f,F_OK)!=-1)//if the text file exists 
    { 
     file1=fopen(f, "r+"); 
     file2=fopen("f2.txt", "w+"); 
     printf("\nThe file before editing:\n\n"); 
     while((n=fgetc(file1))!=EOF)// to show the contents of the file before the edit 
     { 
      putchar(n); 
     } 
     fclose(file1); 
     fclose(file2); 

     if(access(f,W_OK)!=-1)//if the file has the write permission 
     { 
      file1=fopen(f, "r+"); 
      file2=fopen("f2.txt", "w+"); 

      printf("\n\nPlease enter your text: \n"); 
      scanf(" %99[^\n]",t); 

      printf("Specify the line number where you want to insert: "); 
      scanf("%d", &l); 

      while((r=fgetc(file1))!=EOF)//copying file1 contents into file2 contents 
      { 
       fputc(r,file2); 
       if(r == '\n' && ++nl == l){ 
        offset = ftell (file1);//save location in file 
        while ((r = fgetc (file1)) != '\n' && r != EOF) { 
         linelength++;//count characters in line 
        } 
        fseek (file1, offset, SEEK_SET);//seek back to start of line 
        //get index where to insert text 
        do { 
         printf("\nindex(less than %d):\n", linelength); 
         if ((scanf("%d", &index)) != 1) { 
          scanf ("%*[^\n]");//input not an integer. clear buffer 
          index = linelength; 
         } 
        } while (index >= linelength || index < 0); 

        while (index) { 
         r = fgetc (file1); 
         fputc(r,file2); 
         index--; 
        } 
        fprintf(file2, "%s ", t);//adding the inserted text 
       } 
      } 
      printf("\nDONE:\n"); 
      fclose(file1); 
      fclose(file2); 

      file1=fopen(f, "w+"); 
      file2=fopen("f2.txt", "r"); 
      while((y=fgetc(file2))!=EOF){ 
        fputc(y,file1); 
      } 
      fclose(file2); 
      fclose(file1); 
      remove("f2.txt"); 

      file1=fopen(f, "r"); 
      printf("\n"); 
      while((i=fgetc(file1))!=EOF)//showing the result after inserting 
      { 
       putchar(i); 
      } 
      fclose(file1); 
      free(f); 
      free(t); 
     } 
     else{ 
      printf("\n%s text file does not have the Write Permission!", f); 
      free(f); 
      free(t); 
      return; 
     } 
    }else{ 
     printf("file doesn't exits!\n"); 
    } 
} 
相关问题