尝试在 C 中打印文件的每一行
Trying to print every second line of a file in C
我试图将文本文件的每一行打印到我作为命令行参数传入的标准输出。代码给我一个分段错误:11 每次我在命令行中将“-e”作为参数传递时,但其余参数(例如 'n'、'h' 和 'V' 有效正是他们应该如何。我只是想知道是否有人可以提供任何帮助。
int main(int argc, char **argv){
int option;
int x;
char line[1000];
char string[1000];
char evenString[1000];
char *fileName;
int y = 0;
int lineCounter = 10;
if(strcmp(argv[1], "head") == 0){
fileName = argv[2];
FILE *fptr = fopen(fileName, "r");
for(x = 0; x < 10; x++){
fgets(line, sizeof(line), fptr);
strcat(string, line);
}
printf("%s", string);
fclose(fptr);
}
else{
fileName = argv[3];
FILE *fptr = fopen(fileName, "r");
while ((option = getopt(argc, argv,"n:hVe")) != -1) {
switch(option){
case 'e':
lineCounter = 22;
for(y = 1; y < lineCounter; y++){
fgets(line, sizeof(line), fptr);
if(y % 2 == 0){
strcat(evenString, line);
}
}
fclose(fptr);
printf("%s", evenString);
break;
case 'n':
lineCounter = atoi(optarg);
for(x = 0; x < lineCounter; x++){
fgets(line, sizeof(line), fptr);
strcat(string, line);
}
printf("%s", string);
fclose(fptr);
break;
case 'V':
printf("Name: Patrick Hessionn\nEmail: patrick.hession@ucdconnect.ie\nStudent Number: 16347891\nVariant: Head, Even\n ");
break;
case 'h':
printf("-h: gives you the help for the head command\n-nK: prints the first K lines of the text file\n-V: Outputs the version information\n-e: prints the first 10 even lines of the text file\nhead: will print the first 10 lines of the input file.");
printf("\nMake sure to format your input as so: 'Program name:', 'argument name:', 'file name:'\n");
break;
default ://'?'
printf("Error!\nYou should format your input as so:\n'Program name:', 'argument name:', 'file name:'\n");
break;
}
}
}
return 0;
}
不要在未初始化的 char
数组上使用 strcat()
。
char string[1000];
char evenString[1000];
...
strcat(string, line);
...
strcat(evenString, line);
因为它期望有效的 "string" 作为参数。未初始化的 char
数组不是字符串。在 C 中,一个字符串需要至少有一个 char
被设置为 '[=21=]'
来标记它的结束。这也称为字符串的 0
-终止符,或 NUL
-终止符(注意仅 one ),或 null-terminator(注意 较低 个案例和两个 个案例)。
正确初始化 char
-array 使其成为一个空字符串。
char string[1000] = "";
char evenString[1000] = "";
或者你可以做更晦涩的事情
string[0] = '[=12=]';
或
strcpy(string, "");
还有你真的想测试是否fopen()
失败了:
FILE *fptr = fopen(fileName, "r");
if (NULL == fptr)
{
perror("fopen() failed");
exit(EXIT_FAILURE); /* include stdlib.h for EXIT_xxx macros. */
}
这里还有一个微妙的陷阱,以防阅读更多内容然后可以存储:
for(y = 1; y < lineCounter; y++){
fgets(line, sizeof(line), fptr);
if(y % 2 == 0){
strcat(evenString, line);
}
}
这可以通过多种方式解决。一个直接的方法是:
for(y = 1; y < lineCounter; y++){
fgets(line, sizeof(line), fptr);
if(y % 2 == 0){
if (sizeof evenString -1 < strlen(evenString) + strlen(line)
/* -1 to take into account the space needed
for string's '0'-terminator. */
{
errno = ERANGE; /* include errno.h for errno and Exxx macros. */
perror("evenString to short for another line");
exit(EXIT_FAILURE);
}
strcat(evenString, line);
}
}
与 case 'n'
相同。
我试图将文本文件的每一行打印到我作为命令行参数传入的标准输出。代码给我一个分段错误:11 每次我在命令行中将“-e”作为参数传递时,但其余参数(例如 'n'、'h' 和 'V' 有效正是他们应该如何。我只是想知道是否有人可以提供任何帮助。
int main(int argc, char **argv){
int option;
int x;
char line[1000];
char string[1000];
char evenString[1000];
char *fileName;
int y = 0;
int lineCounter = 10;
if(strcmp(argv[1], "head") == 0){
fileName = argv[2];
FILE *fptr = fopen(fileName, "r");
for(x = 0; x < 10; x++){
fgets(line, sizeof(line), fptr);
strcat(string, line);
}
printf("%s", string);
fclose(fptr);
}
else{
fileName = argv[3];
FILE *fptr = fopen(fileName, "r");
while ((option = getopt(argc, argv,"n:hVe")) != -1) {
switch(option){
case 'e':
lineCounter = 22;
for(y = 1; y < lineCounter; y++){
fgets(line, sizeof(line), fptr);
if(y % 2 == 0){
strcat(evenString, line);
}
}
fclose(fptr);
printf("%s", evenString);
break;
case 'n':
lineCounter = atoi(optarg);
for(x = 0; x < lineCounter; x++){
fgets(line, sizeof(line), fptr);
strcat(string, line);
}
printf("%s", string);
fclose(fptr);
break;
case 'V':
printf("Name: Patrick Hessionn\nEmail: patrick.hession@ucdconnect.ie\nStudent Number: 16347891\nVariant: Head, Even\n ");
break;
case 'h':
printf("-h: gives you the help for the head command\n-nK: prints the first K lines of the text file\n-V: Outputs the version information\n-e: prints the first 10 even lines of the text file\nhead: will print the first 10 lines of the input file.");
printf("\nMake sure to format your input as so: 'Program name:', 'argument name:', 'file name:'\n");
break;
default ://'?'
printf("Error!\nYou should format your input as so:\n'Program name:', 'argument name:', 'file name:'\n");
break;
}
}
}
return 0;
}
不要在未初始化的 char
数组上使用 strcat()
。
char string[1000];
char evenString[1000];
...
strcat(string, line);
...
strcat(evenString, line);
因为它期望有效的 "string" 作为参数。未初始化的 char
数组不是字符串。在 C 中,一个字符串需要至少有一个 char
被设置为 '[=21=]'
来标记它的结束。这也称为字符串的 0
-终止符,或 NUL
-终止符(注意仅 one ),或 null-terminator(注意 较低 个案例和两个 个案例)。
正确初始化 char
-array 使其成为一个空字符串。
char string[1000] = "";
char evenString[1000] = "";
或者你可以做更晦涩的事情
string[0] = '[=12=]';
或
strcpy(string, "");
还有你真的想测试是否fopen()
失败了:
FILE *fptr = fopen(fileName, "r");
if (NULL == fptr)
{
perror("fopen() failed");
exit(EXIT_FAILURE); /* include stdlib.h for EXIT_xxx macros. */
}
这里还有一个微妙的陷阱,以防阅读更多内容然后可以存储:
for(y = 1; y < lineCounter; y++){
fgets(line, sizeof(line), fptr);
if(y % 2 == 0){
strcat(evenString, line);
}
}
这可以通过多种方式解决。一个直接的方法是:
for(y = 1; y < lineCounter; y++){
fgets(line, sizeof(line), fptr);
if(y % 2 == 0){
if (sizeof evenString -1 < strlen(evenString) + strlen(line)
/* -1 to take into account the space needed
for string's '0'-terminator. */
{
errno = ERANGE; /* include errno.h for errno and Exxx macros. */
perror("evenString to short for another line");
exit(EXIT_FAILURE);
}
strcat(evenString, line);
}
}
与 case 'n'
相同。