none
ifstream, failed to read the file that in the same directory as the exe file.

    Question

  • // huffmanCode.cpp :

    #include "stdafx.h"
    #include <fstream>
    #include <iostream>
    #include <stdlib.h>
    #define MAXLEAFNUM 128
    using namespace std;
    typedef struct node
    {
     int weight;
     char ch;
     int parent;
     int lchild, rchild;
    }Hnode;

    typedef char **HuffmanCode;
    HuffmanCode Hc;
    Hnode Ht[2 * MAXLEAFNUM];

    void Select(Hnode Ht[], int n, int &s1, int &s2){
        int x1 = 32767, x2 = 32767, k;
     s1 = s2 = 1;
     for(k=1; k <= n; k++){
      if((0 == Ht[k].parent) && (Ht[k].weight < x1)){
       x2 = x1;
       s2 = s1;
       x1 = Ht[k].weight;
       s1 = k;
      }else if((0==Ht[k].parent) && (Ht[k].weight<x2)){
       x2 = Ht[k].weight;
       s2 = k;
      }
     }
    }

    void CreateHtree(int n, int count[], int &m){
     int i, k, s1, s2;

     for(i = 1; i <= (2*n-1); i++){
      Ht[i].parent = 0;
      Ht[i].lchild = 0;
      Ht[i].rchild = 0;
     }

     for(i=1, k=0; k < n; k++){
      if(count[k] != 0){
       Ht[i].weight = count[k];
       Ht[i].ch = (char)k;
       ++i;
      }
     }

     m = i - 1;

     for(k = m+1; k <= (2*m-1); k++){
      Select(Ht, k-1, s1, s2);
      Ht[s1].parent = k;
      Ht[s2].parent = k;
      Ht[k].lchild = s1;
      Ht[k].rchild = s2;
      Ht[k].weight = Ht[s1].weight + Ht[s2].weight;
     }
    }

    void LeafCode(int n){
     int i, p = 2*n-1, cdlen = 0;
     char code[30];
     Hc = (HuffmanCode)malloc((n+1)*sizeof(char*));
     for(i = 1; i <= p; i++){
      Ht[i].weight = 0;
     }
     while(p){
      if(0 == Ht[p].weight){
       Ht[p].weight = 1;
       if(Ht[p].lchild != 0){
        p = Ht[p].lchild;
        code[cdlen++] = '0';
       }
       else if(1 == Ht[p].weight){
        Ht[p].weight = 2;
        if(Ht[p].rchild != 0){
         p = Ht[p].rchild;
         code[cdlen++] = '1';
        }
       }else{
        Ht[p].weight = 0;
        p = Ht[p].parent;
        cdlen--;
       }
      }
      cout<<endl;

      for(i = 1; i <= n; i++){
       if(10 == (int)Ht[i].ch){
        printf("换行符的哈夫曼编码为 : %s",Hc[i]);
        cout<<endl;
       }else if(32 == (int)Ht[i].ch){
        printf("\n空格符的哈夫曼编码 : %s",Hc[i]);
        cout<<endl;
       }else{
        printf("\n字符 %c 的哈夫曼编码为: %s",Ht[i].ch, Hc[i]);
        cout<<endl;
       }
      }
     }
    }

    void ReadFile(int count[]){                
     char ch;                                                //I have created a txt file in the same directory.
     ifstream infile("input.txt", ios::in);   //-----there is the error.    infile points the incorrect address.
                                                                  // What is the  wrong?

      cout<<"打开文件input.txt失败!"<<endl;
      cin>>ch;
     }

     while(infile.get(ch)){
      count[(int)ch]++;
     }

     infile.close();

     int i;

     for(i = 0; i < 128; i++){
      if(count[i] != 0){
          if(10 == i){
        cout<<"\n该文件中换行符的总个数为: "<<count[i]<<" (个)!"<<endl;
       }else if(32 == i){
        cout<<"\n该文件中空格符的总个数为: "<<count[i]<<" (个)!"<<endl;
       }else{
        cout<<"\n该文件中字符 "<<(char)i<<" 的总个数为: "<<count[i]<<" (个)!"<<endl;
       }
      }
     }
    }

    void WriteFile(int n, int count[]){
     int i;
     ofstream outfile("output.txt", ios::out);
     if(!outfile){
      cout<<"打开文件时失败!"<<endl;
      exit(0);
     }
     for(i = 1; i <= n; i++){
      if(10 == (int)Ht[i].ch){
       outfile<<"换行符的哈夫曼编码为 : "<<Hc[i]<<endl;
       cout<<endl;
      }else if(32 == (int)Ht[i].ch){
       outfile<<"\n空格符的哈夫曼编码为 : "<<Hc[i]<<endl;
       cout<<endl;
      }else{
       outfile<<"\n字符 "<<Ht[i].ch<<" 的哈夫曼编码为: "<<Hc[i]<<endl;
       cout<<endl;
      }
     }

     for(i = 0; i < 128; i++){
      if(count[i] != 0){
       if(10 == i){
        outfile<<"\n该文件中 换行符 的总个数为: "<<count[i]<<" (个)!"<<endl;
       }else if(32 == i){
        outfile<<"\n该文件中 空格符 的总个数为: "<<count[i]<<" (个)!"<<endl;
       }else{
        outfile<<"\n该文件中字符 "<<(char)i<<" 的总个数为: "<<count[i]<<" (个)!"<<endl;
       }
      }
     }
     outfile.close();
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
     int m = 128;
     int count[128] = {0};
     ReadFile(count);
     CreateHtree(128, count, m);
     LeafCode(m);
     WriteFile(m, count);
     return 0;
    }

    Friday, January 03, 2014 8:18 AM

Answers

  • When you run your program inside Visual Studio, the working directory is the directory that contains the project file (.vcproj or .vcxproj). To determine the directory of the executable, use GetModuleFileName(NULL) and PathRemoveFileSpec(). Then either

    (a) use this to change the working directory using SetCurrentDirectory()

    or

    (b) use this to construct the full path of your file "input.txt".


    David Wilkinson | Visual C++ MVP

    • Marked as answer by cutedevil Friday, January 03, 2014 1:20 PM
    Friday, January 03, 2014 11:06 AM

All replies

  • When you run your program inside Visual Studio, the working directory is the directory that contains the project file (.vcproj or .vcxproj). To determine the directory of the executable, use GetModuleFileName(NULL) and PathRemoveFileSpec(). Then either

    (a) use this to change the working directory using SetCurrentDirectory()

    or

    (b) use this to construct the full path of your file "input.txt".


    David Wilkinson | Visual C++ MVP

    • Marked as answer by cutedevil Friday, January 03, 2014 1:20 PM
    Friday, January 03, 2014 11:06 AM
  • void ReadFile(int count[]){                

     char ch;                                                //I have created a txt file in the same directory.
     ifstream infile("input.txt", ios::in);   //-----there is the error.    infile points the incorrect address.
                                                                  // What is the  wrong?

      cout<<"打开文件input.txt失败!"<<endl;
      cin>>ch;
     }

     while(infile.get(ch)){
      count[(int)ch]++;
     }


    In addition to what Dave said about running the program from the IDE, note that
    if you run it from the command line prompt then the input file will have to be in
    the current directory there - the one shown in the prompt. If the current directory
    at the prompt is the one where the program's .exe is located then that is where the
    input file must be located.

    In short, if you don't provide a full path to the file when opening it then it must
    be in whichever directory is the current one at the time.
                                                                  
    Your posted code looks messed up a little. Always use the "Insert Code Block" feature
    of the forum editor to post code. That will maintain the formatting.

    You should be testing for failure of the input file open in the same way as you are
    doing for the output file:

    if(!infile)

    or

    if(infile.fail())

    or

    if(!infile.is_open())

    If the file open failed then you should be terminating the program at that point,
    or at least bypass all attempts to read/get from that file after an unsuccessful
    open attempt. It's not enough to just issue a message and then proceed as if the
    file opened by trying to read from it.

    - Wayne

    Friday, January 03, 2014 1:19 PM