Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/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
使用fopen打开文件,c\ubuntu_C_Eclipse_Ubuntu_Fopen - Fatal编程技术网

使用fopen打开文件,c\ubuntu

使用fopen打开文件,c\ubuntu,c,eclipse,ubuntu,fopen,C,Eclipse,Ubuntu,Fopen,我试图用fopen打开文件,读写“哈夫曼树”。 读完课文后,我试着在另一个文件中写一本“字典”,上面写着每个字母的代码。 我有一个eror,我没有找到类似的东西,除了原因是旧版本的eclipse,但这不是问题所在。 我在ubuntu中用eclipse用c编程 主要原因如下: int main(){ FILE *fsrc; node *root; if( (fsrc = fopen( "src.txt", "r" )) == NULL ){ // prin

我试图用fopen打开文件,读写“哈夫曼树”。 读完课文后,我试着在另一个文件中写一本“字典”,上面写着每个字母的代码。 我有一个eror,我没有找到类似的东西,除了原因是旧版本的eclipse,但这不是问题所在。 我在ubuntu中用eclipse用c编程

主要原因如下:

int main(){
    FILE *fsrc;
    node *root;
    if( (fsrc  = fopen( "src.txt", "r" )) == NULL ){ //
        printf( "The file 'src.txt' was not opened\n" );
        return 0;
    }
    else {
        printf( "The file 'src.txt' was opened\n" );
    }
    root = getBinTree(fsrc);
    printTree(&(*root));
    huffman(root); 
    return 0;
}
这是写入目标文本的功能

void printhtree(node *n,FILE *trg){
       char *str = calloc(1, sizeof(char));
       node *ptr=n;
       while (!(ptr->m_hls && ptr->m_hrs)){
           while(ptr->m_hls){
               n=ptr;
               ptr = ptr->m_hls;
               str = realloc(1,sizeof(char)*(strlength(str)+1));
               *(str+strlength(str)-1)='0';
           }//while
           if(!(ptr->m_hrs)){
               printf("%s-%c ",str,ptr->m_ch);
               n->m_hls = NULL;
               return;
           }//if
           while(ptr->m_hrs){
               n=ptr;
               ptr = ptr->m_hrs;
               str = realloc(1,sizeof(char)*(strlength(str)+1));
               *(str+strlength(str)-1)='1';
           }//while
       }//while
       if (!(ptr->m_ch)){
           fputc(ptr->m_ch,trg);
           fputc(' ',trg);
           fputs(str,trg);

           n->m_hls = NULL;
           return;
       }//if
   }//printhtree
这是激活| pringhtree“功能的功能:

  ‪void drawTree(node *n,FILE *trg‬‬){
        while(!(n))
           printhtree(n,trg);
   }
错误出现在绘图树的第一行: “无效绘图树(节点*n,文件*trg‬‬)" 上面写着: “此行有多个标记 遇到错误的字符序列 程序中的游离'\252',与编号254342200的同一行

我在程序开始时写下所有函数的名称时也出现了同样的错误


非常感谢

这是代码中的一个重要问题

str = realloc(1,sizeof(char)*(strlength(str)+1));
出于许多原因

  • 语法错误,因为您正在
    realloc()
    ing地址
    0x01
  • 您不应该立即覆盖指针,因为如果发生错误,您将无法恢复,
    realloc()
    的良好用法如下

    void *tmp = realloc(str, 1 + strlength(str));
    if (tmp != NULL)
        str = tmp;
    
  • 这并没有错,但它会让代码变得不必要的丑陋,
    sizeof(char)
    保证是
    1

  • 大概
    strlength()
    模拟了
    strlen()
    ,因此您不想计算两次长度,存储它并使用该值

  • 第一次使用
    malloc()
    分配空间时,由于在代码中误用了
    realloc()
    ,因此完全删除了该指针,而且除了在写入指向的数据之前,您实际上不需要分配空间,因此可以使用
    malloc()替换
    realloc()
    并删除
    calloc()
    ,通过不必要的方式将所有值初始化为
    0
    ,您不需要这样做

  • 您仅初始化设置为
    '\0'
    str
    的最后一个值,但其余数据仍未初始化,因为如果正确使用,实际分配内存的函数将是
    realloc()
    ,特别是因为
    calloc()
    分配的是单个字节


  • 你从某个编码为UTF8(可能是网页)的地方复制了代码,编译器抱怨这些字符在C代码中是不允许的(至少对于你正在使用的编译器和C版本)。代码不是从web复制的,我自己写的。它是“abbc”。我能做些什么来修复它?所以你是对的,问题是我从txt文件复制了我的代码,有很多看不见的迹象导致了很多问题。非常感谢你:)