Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.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
C 错误:请求成员‘;txt’;在非结构或联盟中_C_Function_File_If Statement_While Loop - Fatal编程技术网

C 错误:请求成员‘;txt’;在非结构或联盟中

C 错误:请求成员‘;txt’;在非结构或联盟中,c,function,file,if-statement,while-loop,C,Function,File,If Statement,While Loop,对不起,我在编译代码时遇到了一个问题,我不知道要解决的错误是什么: “6:26:错误:RequestFormMember'txt'位于非结构或 工会“ 有人能帮我吗 我给你留下我的密码: #include <stdio.h> int calcular(char nombre[]){ FILE *fichero; int contar, maximo = 0; char caracter; fichero = fopen(fichero.txt,"r");

对不起,我在编译代码时遇到了一个问题,我不知道要解决的错误是什么:

“6:26:错误:RequestFormMember'txt'位于非结构或 工会“

有人能帮我吗

我给你留下我的密码:

#include <stdio.h>
int calcular(char nombre[]){
  FILE *fichero; int contar, maximo = 0;
  char caracter;
  fichero = fopen(fichero.txt,"r");
        if(fichero == NULL)
         printf("ERROR de Apertura");
        else{
             while(!feof(fichero)){
                 fscanf(fichero, "%c", & caracter);
                 if(caracter == '/n'){
                   if(contar > maximo)
                      maximo = contar;
                      contar = 0;
                      fclose(fichero);        
                      fclose(salida);
                 }
             }
             contar++;
        }
        return(linea);
}
#包括
整数计算(字符名称[]){
文件*fichero;int contar,maximo=0;
字符;
fichero=fopen(fichero.txt,“r”);
if(fichero==NULL)
printf(“开孔错误”);
否则{
而(!feof(fichero)){
fscanf(fichero、%c、&caracter);
如果(字符=='/n'){
如果(contar>maximo)
maximo=contar;
contar=0;
fclose(fichero);
fclose(salida);
}
}
contar++;
}
返回(linea);
}

您需要对
fichero.txt
使用双引号作为字符串
“fichero.txt”
。由于编译器将其视为您先前声明的
文件*
对象,因此它会抱怨,因为

  • 它是一个指针,所以应该使用
    ->
    操作符访问它的成员
  • 当然,它没有成员
    txt

  • 您将字符串文本fichero.txt传递给fopen(),而不使用双引号,这会诱使编译器认为fichero是一个结构,而txt是该结构的成员

    尝试:


    我怀疑,
    fichero.txt
    是一个文件名。因此,您必须将其编写为带有双引号的字符串文字与其他声明混合,这就是为什么在一行中声明多个变量是不好的。更糟糕的是,在同一行中声明不同类型的变量。好的,谢谢,我还忘了评论我遇到的另一个问题,我不知道你们是否注意到了,就是“linea”和“salida”变量没有声明,因为我不知道它们在代码的哪个部分真正取决于您打算在哪里使用它们。如果您只想在calcular()函数中访问这些值,那么将它们声明为该函数的本地值就足够了。您已经在该函数中进行了本地声明,因此进行后续的delcaration应该不太难理解。要在函数中声明它们,就像您所说的,我在终端:fichero.c:函数'calcular':fichero.c:8:7:错误:“fichero”int-fichero的类型冲突;fichero.c:4:9:注意:前面的“fichero”声明在这里文件*fichero;fichero.c:10:11:警告:赋值从指针生成整数,但不使用强制转换fichero=fopen(“fichero.txt”,“r”);正如iharob已经指出的,您有一个文件*已经命名为fichero…您不能有两个声明使用相同的名称来表示相同的函数。要么更改文件名*,要么为后续声明选择不同的名称。另外,我忘了注释我遇到的另一个问题,我不知道你们是否注意到了,就是“linea”和“salida”变量没有声明,因为我不知道它们在代码的哪一部分go@V_WraMphT3R我很忙。但是请随时给我发邮件iharob@gmail.com
    #include <stdio.h>
    int calcular(char nombre[]){
      FILE *fichero; int contar, maximo = 0;
      char caracter;
      fichero = fopen(fichero.txt,"r");
            if(fichero == NULL)
             printf("ERROR de Apertura");
            else{
                 while(!feof(fichero)){
                     fscanf(fichero, "%c", & caracter);
                     if(caracter == '/n'){
                       if(contar > maximo)
                          maximo = contar;
                          contar = 0;
                          fclose(fichero);        
                          fclose(salida);
                     }
                 }
                 contar++;
            }
            return(linea);
    }
    
    fopen("fichero.txt", "r");