; if(scanf(“%79s”,源文件)!=1){ 返回1; } printf(“目标文件->”; 如果(扫描(“%79s”,目标文件)!=1){ 返回1; } printf(“开始行和结束行:”); if(scanf(“%d%d”、&start\u行和end\u行)!=2){ 返回1; } cp(源文件、目标文件、开始行、结束行); 返回0; }
您的程序中存在一些问题:; if(scanf(“%79s”,源文件)!=1){ 返回1; } printf(“目标文件->”; 如果(扫描(“%79s”,目标文件)!=1){ 返回1; } printf(“开始行和结束行:”); if(scanf(“%d%d”、&start\u行和end\u行)!=2){ 返回1; } cp(源文件、目标文件、开始行、结束行); 返回0; },c,getline,C,Getline,您的程序中存在一些问题: 不要使用gets(),这可能会导致缓冲区溢出 始终使用typeint存储fgetc()的返回值,以便将EOF与常规字节值区分开来 将额外的参数“.txt”传递给printf()。它将被忽略,但仍应删除 要将一系列行从源复制到目标,只需通过以下方式修改函数: #include <stdio.h> #include <string.h> #include <errno.h> void cp(char source_file[], c
- 不要使用
,这可能会导致缓冲区溢出gets()
- 始终使用type
存储int
的返回值,以便将fgetc()
与常规字节值区分开来EOF
- 将额外的参数
传递给“.txt”
。它将被忽略,但仍应删除printf()
#include <stdio.h>
#include <string.h>
#include <errno.h>
void cp(char source_file[], char destination_file[], int start_line, int end_line) {
int ch;
int line = 1, lines_copied;
FILE *source, *destination;
source = fopen(source_file, "r");
if (source == NULL) {
printf("Cannot open input file %s: %s\n",
source_file, strerror(errno));
exit(EXIT_FAILURE);
}
destination = fopen(destination_file, "w");
if (destination == NULL) {
printf("Cannot open output file %s: %s\n",
destination_file, strerror(errno));
fclose(source);
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source)) != EOF) {
if (line >= start_line && line <= end_line) {
fputc(ch, destination);
}
if (ch == '\n') {
line++;
}
}
lines_copied = 0;
if (line > start_line) {
if (line >= end_line) {
lines_copied = end_line - start_line + 1;
} else {
lines_copied = line - start_line + 1;
}
}
printf("Copied lines %d from %s to %s\n",
lines_copy, source_file, destination_file);
fclose(source);
fclose(destination);
}
int main() {
char source_file[80];
char destination_file[80];
int start_line, end_line;
printf("-Enter the name of the source file ending in .txt\n"
"-Enter the name of the destination file ending in .txt\n"
"-Enter the start and end line\n\n");
printf(">subcopy.o ");
if (scanf("%79s", source_file) != 1) {
return 1;
}
printf("destination file-> ");
if (scanf("%79s", destination_file) != 1) {
return 1;
}
printf("Start and end lines: ");
if (scanf("%d %d", &start_line, &end_line) != 2) {
return 1;
}
cp(source_file, destination_file, start_line, end_line);
return 0;
}
#包括
#包括
#包括
无效cp(字符源文件[],字符目标文件[],整数开始行,整数结束行){
int-ch;
int line=1,复制的行数;
文件*源,*目的地;
source=fopen(source_文件,“r”);
if(source==NULL){
printf(“无法打开输入文件%s:%s\n”,
源文件,strerror(errno));
退出(退出失败);
}
destination=fopen(destination_文件,“w”);
如果(目标==NULL){
printf(“无法打开输出文件%s:%s\n”,
目标文件,strerror(errno));
fclose(来源);
退出(退出失败);
}
而((ch=fgetc(源))!=EOF){
如果(行>=开始行和行开始行){
如果(行>=结束行){
复制的行=结束行-开始行+1;
}否则{
行复制=行-开始行+1;
}
}
printf(“将行%d从%s复制到%s\n”,
行(复制、源文件、目标文件);
fclose(来源);
fclose(目的地);
}
int main(){
字符源_文件[80];
char destination_文件[80];
int起始线、结束线;
printf(“-输入以.txt结尾的源文件名\n”
“-输入以.txt结尾的目标文件的名称\n”
“-输入起始行和结束行\n\n”);
printf(“>子副本o”);
if(scanf(“%79s”,源文件)!=1){
返回1;
}
printf(“目标文件->”;
如果(扫描(“%79s”,目标文件)!=1){
返回1;
}
printf(“开始行和结束行:”);
if(scanf(“%d%d”、&start\u行和end\u行)!=2){
返回1;
}
cp(源文件、目标文件、开始行、结束行);
返回0;
}
您能描述一下您遇到的问题吗?我正在尝试评估Linux cp命令。该程序接受3个输入:1)我们将从中读取的文件2)我们将写入的文件(源文件中的内容)3)我们要从源文件复制到目标文件的行,以及您对显示的代码有什么问题?是否有生成错误?是否有运行时错误或崩溃?意外结果?您的问题是什么?请。好的。该程序工作正常,它从一个文件读取并创建整个内容。我要做的就是由用户接收行,例如3-5。这意味着它将从源文件向目标文件写入第3-5行。`char ch;…while((ch=fgetc(source))!=EOF)`不起作用。fgetc()
返回一个int
,而不是char
,因为EOF
不能表示为char
,要从文本文件中读取行,请参见您能描述一下您遇到的问题吗?我正在尝试评估Linux cp命令。该程序接受3个输入:1)我们将从中读取的文件2)我们将编写的文件(源文件中的内容)3)我们要从源文件复制到目标文件的行,以及您对显示的代码有什么问题?是否有生成错误?是否有运行时错误或崩溃?意外结果?您的问题是什么?请。好的。程序工作正常,它从一个文件读取并创建整个内容。我要做的就是访问用户输入行,例如3-5。这意味着它将把源文件中的第3-5行写入目标文件。`char ch;…而((ch=fgetc(source))!=EOF)`将不起作用。fgetc()
返回一个int
,而不是char
,因为EOF
不能表示为char
,要从文本文件中读取行,请看你能解释我怎么做吗?你能解释我怎么做吗?我建议MAX\u LINE\u SIZE
太短(使用4096或类似的东西)。除此之外,输出的间距是双倍的(因为fgets()
保留了换行符,所以输出格式不需要再添加第二行。不过这两种格式都是琐事。@JonathanLeffler噢,是的,我不记得fgets保留了换行符,修复:)至于100,我只是选择了一个随机数作为占位符,但我同意它非常小,我需要插入行作为输入。还有一个参数行拷贝将用于此purpose@Eternal要从文件而不是stdin中读取,只需将变量名放入fgets中efgets(line,MAX\u line\u SIZE,source)
。如果这不是你要问的问题,我真的不理解你的问题,对不起。另外,line\u copy的用途是什么?line\u copy是我试图解决问题的方法。我的问题在我的帖子中以黄色背景解释。我想从文本文件中复制特定的行(a) 并制作另一个带有特定行的文本文件(a的副本),如黄色背景中的示例中的第2-3行。我建议MAX\u LINE\u SIZE
太短(使用4096或类似的东西)。除此之外,输出的间距是双倍的(因为fgets()
保留了换行符,所以输出格式不需要使用空格)
2
3
#include <stdio.h>
#include <stdlib.h>
void cp(char source_file[], char destination_file[], int lines_copy) {
char ch;
FILE *source, *destination;
source = fopen(source_file, "r");
if (source == NULL) {
printf("File name not found, make sure the source file exists and is ending at .txt\n");
exit(EXIT_FAILURE);
}
destination = fopen(destination_file, "w");
if (destination == NULL) {
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source)) != EOF)
fputc(ch, destination);
printf("Copied lines %d from %s to %s \n",
lines_copy, source_file, destination_file, ".txt");
fclose(source);
fclose(destination);
}
int main() {
char s[20];
char d[20];
int lines;
printf("-Enter the name of the source file ending in .txt\n"
"-Enter the name of the destination file ending in .txt\n"
"-Enter the number of lines you want to copy\n\n");
printf(">subcopy.o ");
gets(s);
printf("destination file-> ");
gets(d);
printf("Lines: ");
scanf("%d", &lines);
cp(s, d, lines);
return 0;
}
sed -n -e "3p;7,12p" < input.txt > output.txt
#ifndef MAX_LINE_SIZE
#define MAX_LINE_SIZE 100
#endif
char line[MAX_LINE_SIZE];
int count = 0;
while (fgets(line, MAX_LINE_SIZE, source)){
count++;
if (3 <= count && count <= 5){
fputs(line, destination);
}
}
#include <stdio.h>
#include <string.h>
#include <errno.h>
void cp(char source_file[], char destination_file[], int start_line, int end_line) {
int ch;
int line = 1, lines_copied;
FILE *source, *destination;
source = fopen(source_file, "r");
if (source == NULL) {
printf("Cannot open input file %s: %s\n",
source_file, strerror(errno));
exit(EXIT_FAILURE);
}
destination = fopen(destination_file, "w");
if (destination == NULL) {
printf("Cannot open output file %s: %s\n",
destination_file, strerror(errno));
fclose(source);
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source)) != EOF) {
if (line >= start_line && line <= end_line) {
fputc(ch, destination);
}
if (ch == '\n') {
line++;
}
}
lines_copied = 0;
if (line > start_line) {
if (line >= end_line) {
lines_copied = end_line - start_line + 1;
} else {
lines_copied = line - start_line + 1;
}
}
printf("Copied lines %d from %s to %s\n",
lines_copy, source_file, destination_file);
fclose(source);
fclose(destination);
}
int main() {
char source_file[80];
char destination_file[80];
int start_line, end_line;
printf("-Enter the name of the source file ending in .txt\n"
"-Enter the name of the destination file ending in .txt\n"
"-Enter the start and end line\n\n");
printf(">subcopy.o ");
if (scanf("%79s", source_file) != 1) {
return 1;
}
printf("destination file-> ");
if (scanf("%79s", destination_file) != 1) {
return 1;
}
printf("Start and end lines: ");
if (scanf("%d %d", &start_line, &end_line) != 2) {
return 1;
}
cp(source_file, destination_file, start_line, end_line);
return 0;
}