/* ヘッダ */
#include <stdlib.h>
#include <stdio.h>
/* 課題 */
void insert_str(const char *name, int row, int column, const char *str);
/* main() */
int main(void){
insert_str("data.txt", 4, 7, "test");
return 0;
}
/* 課題 */
void insert_str(const char *name, int row, int column, const char *str){
/* 変数 */
char *front;
char *back;
FILE *file_ptr;
int count = 1;
unsigned long int front_length, back_length;
/* エラー */
file_ptr
= fopen(name
, "r"); if(file_ptr == NULL){
}
/* 指定行まで移動 */
while(count < row){
if(fgetc(file_ptr
) == '\n'){ ++count;
}
}
/* 指定列まで移動 */
for(count = 0; count < column; ++count){
}
/* 文字列の長さを保存 */
front_length
= ftell(file_ptr
); fseek(file_ptr
, 0, SEEK_END
); back_length
= ftell(file_ptr
) - front_length
; /* 文字列を保存 */
front
= (char *)malloc(sizeof(char) * (front_length
+ 1)); back
= (char *)malloc(sizeof(char) * (back_length
+ 1)); fseek(file_ptr
, 0, SEEK_SET
); fread(front
, sizeof(char), front_length
, file_ptr
); fread(back
, sizeof(char), back_length
, file_ptr
); front[front_length] = '\0';
back[back_length] = '\0';
/* 書き込み */
file_ptr
= fopen(name
, "w"); if(file_ptr == NULL){
}
fprintf(file_ptr
, "%s%s%s", front
, str
, back
);
}
Lyog44OY44OD44OAICovCiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0ZGlvLmg+CgovKiDoqrLpoYwgKi8Kdm9pZCBpbnNlcnRfc3RyKGNvbnN0IGNoYXIgKm5hbWUsIGludCByb3csIGludCBjb2x1bW4sIGNvbnN0IGNoYXIgKnN0cik7CgovKiBtYWluKCkgKi8KaW50IG1haW4odm9pZCl7CglpbnNlcnRfc3RyKCJkYXRhLnR4dCIsIDQsIDcsICJ0ZXN0Iik7CgkKCXJldHVybiAwOwp9CgovKiDoqrLpoYwgKi8Kdm9pZCBpbnNlcnRfc3RyKGNvbnN0IGNoYXIgKm5hbWUsIGludCByb3csIGludCBjb2x1bW4sIGNvbnN0IGNoYXIgKnN0cil7CgkvKiDlpInm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