将文本插入特定行
Inserting a text into a specific line
我正在尝试创建一个根据用户输入在行与行之间插入文本的函数。用户必须指定行号和索引才能插入他的行。
目前,我已经设法在行前插入文本,但无法将其插入行"index"。
有谁知道如何根据索引号插入?
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");
}
}
这里使用ftell()
和fseek()
保存所选行开始的文件位置,读取行的长度和return到行的开始。
提示用户在小于行长度的行中输入索引。
我确实遇到了 *t 和 *f 的原始 malloc 的分段错误。我尝试了一些更长的输入,所以这为每个指针分配了 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");
}
}
我正在尝试创建一个根据用户输入在行与行之间插入文本的函数。用户必须指定行号和索引才能插入他的行。
目前,我已经设法在行前插入文本,但无法将其插入行"index"。 有谁知道如何根据索引号插入? 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");
}
}
这里使用ftell()
和fseek()
保存所选行开始的文件位置,读取行的长度和return到行的开始。
提示用户在小于行长度的行中输入索引。
我确实遇到了 *t 和 *f 的原始 malloc 的分段错误。我尝试了一些更长的输入,所以这为每个指针分配了 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");
}
}