Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';I’我试图从文件中只读取前3个字符-c_C_String_File - Fatal编程技术网

I';I’我试图从文件中只读取前3个字符-c

I';I’我试图从文件中只读取前3个字符-c,c,string,file,C,String,File,嗨,我正在构建一个应用程序,它将检查文件中的数字是否都已修复,如果没有,请修复它们(取决于特定的国家代码)。 为了检查号码是否正确,我首先需要查看国家代码,为此我需要读取前3个字符(来自文件中的电话号码),以查看号码是否有国家代码。 当我运行下面的代码时,我得到 变量“country_code”周围的堆栈已损坏” 我认为我从字符串中读取特定的3个字符是造成问题的原因,但我找不到具体的原因 void fix_file_il_country(char *filename) { FILE *f

嗨,我正在构建一个应用程序,它将检查文件中的数字是否都已修复,如果没有,请修复它们(取决于特定的国家代码)。 为了检查号码是否正确,我首先需要查看国家代码,为此我需要读取前3个字符(来自文件中的电话号码),以查看号码是否有国家代码。 当我运行下面的代码时,我得到

变量“country_code”周围的堆栈已损坏”

我认为我从字符串中读取特定的3个字符是造成问题的原因,但我找不到具体的原因

void fix_file_il_country(char *filename)
{
    FILE *f = fopen("1.txt", "r");                  //The file to check from
    if (f == NULL)
    {
        printf("EROR\n");
        return;
    }
    FILE *fp = fopen(filename, "w");                //The new file with fixed numbers
    if (f == NULL)
    {
        printf("EROR\n");
        return;
    }
    char num[20];
    char country_code[3];
    fscanf(f, "%3s %s", &country_code, &num);
    while (!feof(f))
    {
        if (strlen(num) == 12)
            if (country_code == "972")
                fprintf(fp, "%s\n", num);
        fscanf(f, "%3s %s", &country_code, &num);
    }
    fclose(f);
    fclose(fp);
}
像:9725XXXXXXXX这样的数字应该写入新文件
不应写入诸如:123xxxxxxxx或字符数大于或小于12的数字。

您没有为空终止符提供空间

char country_code[3];
应该是

char country_code[4];

另外,请参见您没有为空终止符提供空间

char country_code[3];
应该是

char country_code[4];

另外,请参见

我可以看到两个潜在问题:

  • char
    数组还需要存储空终止符,因此应将其定义为

    char country_code[4]
    
    否则,无法定义数组的结尾

  • scanf
    中,您正在传递指针的指针。您应该更改

    fscanf(f, "%3s %s", &country_code, &num)
    

    没有


  • 我可以看到两个潜在问题:

  • char
    数组还需要存储空终止符,因此应将其定义为

    char country_code[4]
    
    否则,无法定义数组的结尾

  • scanf
    中,您正在传递指针的指针。您应该更改

    fscanf(f, "%3s %s", &country_code, &num)
    

    没有


  • 请显示一些示例输入和所需输出对。将其与实际获得的输出进行对比。请将代码引号升级为a。此外,数组会衰减为指向第一个元素的指针,因此
    fscanf(f,“%3s%s”,国家代码,num);
    请显示一些示例输入和所需输出对。将其与实际获得的输出进行对比。请将代码引号升级为a。此外,数组会衰减为指向第一个元素的指针,因此
    fscanf(f,“%3s%s”,国家代码,num);
    正如上面两条注释所说,我需要在字符串处添加另一个空格以使其工作。正如上面两条注释所说,我需要在字符串处添加另一个空格以使其工作。