#include <stdio.h>#include <string.h>#include <stdlib.h>int main(){ FILE *input, *output; if ((input = fopen("读入文件路径", "r")) == NULL) { printf("Open file error\n"); exit(0); } if ((output = fopen("输出文件路径", "w")) == NULL) { printf("Create file error\n"); exit(0); } char s[110], t[110]; int p, q; fscanf(input, "%s%d%d", s, &p, &q); strncpy(t, s + p, q); strcat(s, t); fprintf(output, "%s", s); return 0;}
在main函数中创建一个文件,将键盘输入的字符串写入文件中。打开该文件,读出字符串。将字符串从指定下标p处开始的前q个字符(包括p处字符,q≤p+1)复制到字符串的结尾,形成的新串保存到另一个文件中。比如,abcdefg,从下标3处开始的前2个字符复制到字符串的结尾,得新串:abcdecd。要求:p和q均由键盘输入。
给strcat加个断点调试一下吧,代码贴出来啊,文件操作不熟悉,我眼睛看不出问题啊~
strncpy函数不会自动加'\0'