打开文件时,简单的程序会导致seg错误

时间:2022-09-06 15:19:27

I've got a text file that contains a bunch of strings, nothing important in terms of my problem.

我有一个包含一堆字符串的文本文件,就我的问题而言并不重要。

The code here compiles/runs, and If I type in the correct text file, the first if statement runs. However, if I don't the else statement doesn't execute, but instead I get a seg fault, would Mallocing the pointer be of any help here? Any help would be much appreciated.

这里的代码编译/运行,如果我键入正确的文本文件,则运行第一个if语句。但是,如果我没有执行else语句,而是我得到一个seg错误,那么指向Mallocing指针是否有任何帮助?任何帮助将非常感激。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main (int argc, char * argv[])
{

    FILE * ptr;

    if(strcmp(argv[1],"test.txt") == 0)
    {
        printf("Right text file was inputted");
    }
   //but if I wan't the alternative (if the user didn't enter the right thing

    else
    {
     // this never executes, but instead the program just seg faults if the first if statement is not true
     printf("You didn't enter the right textfile, or none at all");
     exit(1);
    }
}

2 个解决方案

#1


3  

You should be using argc (the count of the number of arguments given) to determine if a value is entered or not. As it stands, accessing argv[1] when argc is 0, will cause a segmentation fault as you're accessing passed the end of the array when strcmp dereferences the terminating NULL pointer.

您应该使用argc(给定参数数量的计数)来确定是否输入了值。就目前而言,当argc为0时访问argv [1]会导致分段错误,因为当strcmp取消引用终止NULL指针时,您正在访问传递数组的末尾。

Your first if statement should be:

你的第一个if语句应该是:

if(argc > 1 && strcmp(argv[1],"test.txt") == 0) {
...

#2


0  

when you pass arguments to main(), they are passes in the form of strings to main(). argc is a count of the arguments passed to the main() and argv is argument vector which is always NULL ended. so if you dont provide any arguments you have to first check with argc count and then proceed. other thing is you cannot check if wrong file name is passed or file name is not passed at all in just one condition

当你将参数传递给main()时,它们会以字符串的形式传递给main()。 argc是传递给main()的参数计数,而argv是参数向量,它总是以NULL结尾。因此,如果您不提供任何参数,您必须首先检查argc计数然后继续。另一件事是你无法检查是否传递了错误的文件名,或者只是在一个条件下没有传递文件名

it should be like,

应该是这样的,

int main (int argc, char * argv[])
{
    FILE * ptr;
    if(argc>1)
    {
        if(strcmp(argv[1],"test.txt") == 0)
        {
            printf("Right text file was inputted");
        }
        else
        {
            printf("You didn't enter the right textfile");
            exit(1);
        }
    }
    else
        printf("you havn't entered any file name");
}

#1


3  

You should be using argc (the count of the number of arguments given) to determine if a value is entered or not. As it stands, accessing argv[1] when argc is 0, will cause a segmentation fault as you're accessing passed the end of the array when strcmp dereferences the terminating NULL pointer.

您应该使用argc(给定参数数量的计数)来确定是否输入了值。就目前而言,当argc为0时访问argv [1]会导致分段错误,因为当strcmp取消引用终止NULL指针时,您正在访问传递数组的末尾。

Your first if statement should be:

你的第一个if语句应该是:

if(argc > 1 && strcmp(argv[1],"test.txt") == 0) {
...

#2


0  

when you pass arguments to main(), they are passes in the form of strings to main(). argc is a count of the arguments passed to the main() and argv is argument vector which is always NULL ended. so if you dont provide any arguments you have to first check with argc count and then proceed. other thing is you cannot check if wrong file name is passed or file name is not passed at all in just one condition

当你将参数传递给main()时,它们会以字符串的形式传递给main()。 argc是传递给main()的参数计数,而argv是参数向量,它总是以NULL结尾。因此,如果您不提供任何参数,您必须首先检查argc计数然后继续。另一件事是你无法检查是否传递了错误的文件名,或者只是在一个条件下没有传递文件名

it should be like,

应该是这样的,

int main (int argc, char * argv[])
{
    FILE * ptr;
    if(argc>1)
    {
        if(strcmp(argv[1],"test.txt") == 0)
        {
            printf("Right text file was inputted");
        }
        else
        {
            printf("You didn't enter the right textfile");
            exit(1);
        }
    }
    else
        printf("you havn't entered any file name");
}