Thread: Ifstream Problem

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question Ifstream Problem

    Sorry, I don't know why in my code, if I declare: <ifstream f>, the answer will go very wrong !!! (in this code, I not use other variable f)
    I will give everybody here my code (too long !!!)
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<fstream>
    #include <vector>
    #include <sstream>
    #include <iostream>
    #include <cstdio>
    using namespace std ;
    #define fo "output.txt"
    
    class ThreeTeleports {
      public:
      struct point{
        long x;
        long y;
      };
      struct line{
        point a;
        point b;
      };
      struct line List[4];
      void Building(string a,int i){
          istringstream is(a);
          long tmp;
          is>>tmp;  List[i].a.x=tmp; //cout<<tmp<<endl;
          is>>tmp;  List[i].a.y=tmp; //cout<<tmp<<endl;
          is>>tmp;  List[i].b.x=tmp;  //cout<<tmp<<endl;
          is>>tmp;  List[i].b.y=tmp;  //cout<<tmp<<endl;
      }
      long long get(long x1,long y1,long x2,long y2){
        return(abs(x1-x2)+abs(y1-y2));
      }
    
      int shortestDistance(int xMe, int yMe, int xHome, int yHome, vector <string> teleports) {
          int i;
          ifstream g; //MY QUESTION HERE: IF DON'T HAVE THIS LINE, NO PROBLEM.
          for(i=0;i<=2;i++){
            Building(teleports[i],i+1);
          }
          long long min=1000000000;
          long long distance;
          int mask,div;
          long remainX,remainY;
          for(mask=0;mask<=7*7*7;mask++){
           // f<<"trang thai "<<mask<<endl;
            distance=0;
            remainX=xMe;    remainY=yMe;
            div=mask;
            for(i=1;i<=3;i++){
                int res;
                res=div%7;
                div=div/7;
                if(res!=0){
                    if(res<=3){
                        //f<<res<<endl;
                        distance=distance+get(remainX,remainY,List[res].a.x,List[res].a.y)+10;
                        remainX=List[res].b.x;  remainY=List[res].b.y;
                    }
                    else{
                        //f<<res<<endl;
                        res=res-3;
                        distance=distance+get(remainX,remainY,List[res].b.x,List[res].b.y)+10;
                        remainX=List[res].a.x;  remainY=List[res].a.y;
                    }
                }
            }
            distance=distance+get(remainX,remainY,xHome,yHome);
            //f<<"khoang cach "<<distance<<endl;
            if(distance<min) min=distance;
          }
         return(min);
     printf("************************************\n");
      }
    
    
    // BEGIN CUT HERE
    	public:
    	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
    	private:
    	template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
    	void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
    	void test_case_0() { int Arg0 = 3; int Arg1 = 3; int Arg2 = 4; int Arg3 = 5; string Arr4[] = {"1000 1001 1000 1002", "1000 1003 1000 1004", "1000 1005 1000 1006"}; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); int Arg5 = 3; verify_case(0, Arg5, shortestDistance(Arg0, Arg1, Arg2, Arg3, Arg4)); }
    	void test_case_1() { int Arg0 = 0; int Arg1 = 0; int Arg2 = 20; int Arg3 = 20; string Arr4[] = {"1 1 18 20", "1000 1003 1000 1004", "1000 1005 1000 1006"}; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); int Arg5 = 14; verify_case(1, Arg5, shortestDistance(Arg0, Arg1, Arg2, Arg3, Arg4)); }
    	void test_case_2() { int Arg0 = 0; int Arg1 = 0; int Arg2 = 20; int Arg3 = 20; string Arr4[] = {"1000 1003 1000 1004", "18 20 1 1", "1000 1005 1000 1006"}; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); int Arg5 = 14; verify_case(2, Arg5, shortestDistance(Arg0, Arg1, Arg2, Arg3, Arg4)); }
    	void test_case_3() { int Arg0 = 10; int Arg1 = 10; int Arg2 = 10000; int Arg3 = 20000; string Arr4[] = {"1000 1003 1000 1004", "3 3 10004 20002", "1000 1005 1000 1006"}; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); int Arg5 = 30; verify_case(3, Arg5, shortestDistance(Arg0, Arg1, Arg2, Arg3, Arg4)); }
    	void test_case_4() { int Arg0 = 3; int Arg1 = 7; int Arg2 = 10000; int Arg3 = 30000; string Arr4[] = {"3 10 5200 4900", "12212 8699 9999 30011", "12200 8701 5203 4845"}; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); int Arg5 = 117; verify_case(4, Arg5, shortestDistance(Arg0, Arg1, Arg2, Arg3, Arg4)); }
    	void test_case_5() { int Arg0 = 0; int Arg1 = 0; int Arg2 = 1000000000; int Arg3 = 1000000000; string Arr4[] = {"0 1 0 999999999", "1 1000000000 999999999 0", "1000000000 1 1000000000 999999999"}; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); int Arg5 = 36; verify_case(5, Arg5, shortestDistance(Arg0, Arg1, Arg2, Arg3, Arg4)); }
    
    // END CUT HERE
    
    };
    
     // BEGIN CUT HERE
        int main()
            {
            ThreeTeleports ___test;
            ___test.run_test(-1);
           int X ;
           scanf("%d",&X) ;
            }
     // END CUT HERE
    You can see line I note. You just run the program, and see why.

    please help me,please.
    thanks
    Last edited by hqt; 09-21-2011 at 11:22 AM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by hqt View Post
    Sorry, I don't know why in my code, if I declare: <ifstream f>, the answer will go very wrong !!! (in this code, I not use other variable f)
    I will give everybody here my code (too long !!!)
    What do you mean "go very wrong"? What is it that you want your code to do and what is it actually doing?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Ah, It's a complicate code. It's a solution of mine on TopCoder SRM 519.
    "very wrong" mean: you just run that program with/without line <istream f>. you will see fail message/successful message. and it change answer, just because a line, that I don't use in that code.
    Last edited by hqt; 09-21-2011 at 10:56 AM.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Ah, It's a complicate code. It's a solution of mine on TopCoder SRM 519.
    "very wrong" mean: you just run that program with/without line <istream f>. you will see fail message/successful message.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Code:
    #define fo "output.txt"
    #define FOREACH(it,c) for( __typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
    #define FOR(i,a,b) for( int i=(a),_b=(b);i<=_b;i++)
    #define DOW(i,b,a) for( int i=(b),_a=(a);i>=_a;i--)
    #define REP(i,n) FOR(i,0,(n)-1)
    #define DEP(i,n) DOW(i,(n)-1,0)
    #define all(a) (a).begin() , (a).end()
    #define push(a,b) (a).push_back((b))
    Why would you do that ? That's horrible.

    Also why would you include almost every header file possible ? Split your main file into many smaller files to improve readability.

    And finally, you're mixing C and C++. There's no reason why you would need scanf() in a C++ program.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    ha ha, It's just automatic code :"> You don't need to care them, I never use it

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by hqt View Post
    ha ha, It's just automatic code :"> You don't need to care them, I never use it
    Then remove it from your program if you never use it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hqt
    you just run that program with/without line <istream f>. you will see fail message/successful message.
    I compiled and ran your program either way and did not observe any problems.

    My guess is that you are telling the truth, and the reason for this discrepancy is there somewhere, there is undefined behaviour. The undefined behaviour exists whether or not you declare that ifstream variable, but when you do, on your system, it shows up.

    One possibility is that you are accessing the vector or some array out of bounds.

    Quote Originally Posted by hqt
    ha ha, It's just automatic code :"> You don't need to care them, I never use it
    What is "automatic code"? Furthermore, maybe parts of this code is a template that you are working with, but if you want people to read your code, clean it up first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    @laserlight: Oh, you say right I really don't know, how to express my idea.
    So, my problem don't meet on your computer? So, I don't know why It's happen with me
    I want to debug, by print data to file (because It's too long, so I cannot watch it on stdio). But, when I'm use ifstream, It's go wrong !!!

    @<automatic code> mean, when I'm create new project, I will generate those code. But, of course, I'm sorry for all, I will make it clearer.
    Last edited by hqt; 09-21-2011 at 11:24 AM.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That code is absolutely horrible.

    Although the error is unlikely to be due to the fstream, it is usually a good idea to open an ifstream (with a filename) before reading from it. The symptoms changing (when you define an ifstream versus not defining it) are most likely to be a side-effect of something else, as laserlight mentioned.

    One problem I see is in your ShortestDistance() function. The accesses to the array List[res] could quite easily be going out of bounds, as the computation to work out the value of res is a bit hairy.

    You also have identifiers with multiple leading underscores. That always means undefined behaviour.

    The various macros and unnecessarily convoluted coding style will just make the code harder to get right.

    Try simplifying your code (definitely eliminate those macros), and then format it (indenting, etc) so it is easier for a mere mortal to understand it. At least then you will have a fighting chance of being able to clearly understand what the code does, and improve your odds of finding the errors.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by hqt View Post
    Ah, It's a complicate code. It's a solution of mine on TopCoder SRM 519.
    "very wrong" mean: you just run that program with/without line <istream f>. you will see fail message/successful message.
    No, I will not run your program! Anyone who ever asks me to do that on here will at best get politely told where to shove that idea. I haven't needed to run anyone else'e program on here before to spot a bug and I'm not about to start. It would not help anyway because the bug most likely only manifests itself with a specific compiler and a specific set of optimisation settings.
    If you have a problem with the output then you post the exact output and state exactly what it should be.

    Many of us know the possible causes for the kind of bug you are experiencing and thus know what to look for to spot a bug of that kind by looking at the code. Your bug shows up with a change in size of the stack frame. If you're correct in what you are saying then this almost certainly means either an uninitialised variable or a buffer overrun of an array on the stack. There are no bugs of these kinds in that code. The bug has to be in other code that you have not posted.

    You are however completely unnecessarily using "long long" in several places in the code which will work identically if you just use "long".
    You are not passing a string and a vector by const-reference as would be recommended. And yuck, the generated code passes ints by const-reference.
    I also suggest that you stop trying to use arrays as though they are one-based by increasing the size by one and not using the first spot.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    I'm sure that List[res] will never out of bound. (because res=0->6, and if res=4->6, I will take: res=res-3 )
    Why do you say the computation to work out the value of res is a bit hairy? Can you help me, make it more clearer, please. (I want to learning everything that make better for me, not I don't trust you )
    And ifstream/fstream, I will read more document about this to understand information you(grumpy and laselight) give to me.

    @:iMalc: You post, I can write again, with little line. You say long, but I'm so sorry, It just give a little information. (your first paragraph, you just say: I will not test your code! that all.) and, I still don't know, why you say: the bug has to be in other code that you have not posted ? And:
    You are not passing a string and a vector by const-reference as would be recommended. And yuck, the generated code passes ints by const-reference.
    Simply, It's a must.

    thanks
    Last edited by hqt; 09-22-2011 at 03:23 AM.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by hqt View Post
    I'm sure that List[res] will never out of bound. (because res=0->6, and if res=4->6, I will take: res=res-3 )
    Why do you say the computation to work out the value of res is a bit hairy? Can you help me, make it more clearer, please.
    There are several relationships between variables in your code. Your computation of res depends on values of other variables in the outer loop, and the interactions of those variables (or their values) is not particularly obvious. On (admittedly) a quick look, I could not exclude the possibility that mask is a negative value and, since there is later a "div = mask;" and then "res = div %7;" I could not exclude the possibility of implementation-defined behaviour. Division involving negative integral values has such behaviour in some cases, and so does the modulo operator.

    I couldn't definitely say what is wrong with your code but, equally, because of it's structure I also couldn't be sure it is behaving correctly. Hence my comment.

    Generally, the more complicated your code is, the easier it is to get it wrong.

    Quote Originally Posted by hqt View Post
    Simply, It's a must.
    Why?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Yes, you say right, the relationship between variables very complicate. And if someone don't know the real purpose of this code, I'm sure that he/she cannot understand. (Like you read Some algorithm that you don't know what is it, you hard to understand it !!!)

    :-? I don't think mask will me negative, because It just change mask in loop. ( for(mask=0;mask<=7*7*7;mask++)). I don't change mask in this loop. And, div=mask, just mean I want to use mask variable, but I will change it in this loop,too. So I must use other variable. (if not, there will be infinity loop). And, "res=div%7" will always make res=0->6.

    Why?
    Ah, because, this is TopCoder Problem, and you solve it online. After you code it and give it to server, server will run your function. (that have already said to you before)
    Last edited by hqt; 09-22-2011 at 08:36 AM.

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by hqt View Post
    Ah, because, this is TopCoder Problem, and you solve it online. After you code it and give it to server, server will run your function. (that have already said to you before)
    Oh....are you doing one of those online programming competitions?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifstream problem
    By skiabox in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2010, 01:09 PM
  2. ifstream problem
    By vw1970 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2009, 12:10 PM
  3. ifstream problem
    By Yuri in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2005, 02:55 PM
  4. ifstream problem
    By Jaken Veina in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2005, 02:59 PM
  5. ifstream problem
    By Lurker in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2003, 06:10 PM