File I/O problem

This is a discussion on File I/O problem within the C++ Programming forums, part of the General Programming Boards category; Crashed. Even if it tries to read just one chromosome and fitness from that DEAD file it crashes. (Sigh)...

  1. #31
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    Crashed. Even if it tries to read just one chromosome and fitness from that DEAD file it crashes.

    (Sigh)

  2. #32
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Bench82
    You will find that the snippet of code I posted works fine once error checking is introduced.
    Error checking is unnecessary if you use the read statement as the while conditional, which is what the poster was doing.

  3. #33
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Extracting from an STL container is not the same as extracting from an istream. The Vector will work because the iterator position can be checked before looping, using vector::end().
    An istream cannot be checked in the same way because there is no iterator to denote "one past the end". I've reposted this to include the check - this one will not overrun.

    Code:
    while (file_to_read)
      std::string s;
      std::string::size_type start(0), index;
      std::getline(file_to_read, s);
    
      index = s.find('_');
      if (index == std::string::npos)
         break;
      std::string chromosome_read(s, start, index);
      std::string fitness_read(s, index);

  4. #34
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by 7stud
    Error checking is unnecessary if you use the read statement as the while conditional, which is what the poster was doing.
    That is true, but he was also using the while conditional to search for a character in the stream at the same time, and then assign the result to another string. This won't work because the final time the conditional is called, that character will not exist.

    it would be OK if he was simply using
    Code:
    while(getline(s, file_to_read) )
    Even then, before doing any kind of assignment (based on a search for a char) to other strings in the loop, there should still be some kind of check in order to protect against overrun.
    Last edited by Bench82; 02-20-2006 at 12:44 PM.

  5. #35
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    I've tried your loop, Bench. It still crashes, unfortunately.

  6. #36
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Onions
    I've tried your loop, Bench. It still crashes, unfortunately.
    Then I'm baffled It compiles and runs on my system.

    (Which is MSVC++ 2003 - although as far as I'm aware, I've not used any non-standard code)
    Last edited by Bench82; 02-20-2006 at 01:01 PM.

  7. #37
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    Quote Originally Posted by Bench82
    Then I'm baffled It compiles and runs on my system.

    (Which is MSVC++ 2003 - although as far as I'm aware, I've not used any non-standard code)
    Well, it compiles and runs, yes. But after it has made a few enemies the game crashes for random reasons again.

  8. #38
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It's possible that what you write to the file is not what you think, and therefore when you read the data into your vector, it's corrupted data. Personally, I don't like the way you are formating your data when you write to the file. Why don't you do this instead:

    22222222222222 3443

    Then you can use >> to read in the data, and there won't be any whitespace or newlines in the data you read. getline() grabs everything until the delimiter, which may be grabbing some whitespace after 3443, and maybe that whitespace is causing the crashes.
    Last edited by 7stud; 02-20-2006 at 01:18 PM.

  9. #39
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    So, if I then opened the DEAD file to look at what had happened would it look like

    Code:
    111111111111 223 222222222222 342 111111111111 112
    Or would it still be

    Code:
    111111111111 223
    222222222222 342
    111111111111 112
    It's not hugely important, I guess. I'm just trying to understand how you think I should do it.

    Also - how will there not be any whitespace? There is still a gap between the chromosome and the fitness, isn't there?

  10. #40
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    I've changed it to this:

    Code:
      while (file_to_read) {
    
        file_to_read >> chromosome_int_read;
        file_to_read >> fitness_string_read;
    
        chromosome_int_array.push_back(chromosome_int_read);
    
        fitness_string_array.push_back(fitness_string_read);
    
    }
    And the DEAD file starts off as just:

    Code:
    111111111111 50
    But it is now reading in that chromosome and that fitness twice. What should I use as the terminating condition for this new code?

    (Also, thanks for the continuing help)

  11. #41
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I've changed it to this:

    while (file_to_read)
    Wrong choice. If you do that, then you have to add some end of file error checking inside the while loop. Keep your read statement as the while conditional.

    Or would it still be

    111111111111 223
    222222222222 342
    111111111111 112
    That's what I was envisioning. However, when you use >> to read in data, it doesn't matter whether the data is on separate lines or not--the only thing you need is alternating strings. The >> operator skips all whitespace(spaces, tabs, and newlines).

    If you look at the file that your program wrote, you cannot tell what is at the end of the line. Carriage returns between lines are invisible. So if you see this in the file:

    111111111111 223
    222222222222 342

    can you tell if there are 10 spaces after the first line followed by a carriage return, or if there is only a carriage return at the end of the line? If there are 10 spaces, then getline will grab:
    Code:
    "223          "
    and stick it in your string variable. Will having those spaces in the string cause your program to crash?

    Try this: after your program writes the strings to the file, and after you finish reading the strings from the file and putting them in your vector, output the contents of the fitness_string_array vector. Additionally, after every string add "<----". For instance, do something like this:

    cout<<vector[i]<< "<----"<<endl;

    If there are no spaces at the end of the data, you will see:
    Code:
    223<----
    If there are spaces at the end of the data, you will see:
    Code:
    223    <----
    If everything looks ok after that test, then you have to consider the near certainty that the problem is not in that loop at all. Somewhere in your code, you may be trying to access elements that are out of bounds in your vector, which could cause your program to crash. Or, you may be trying to access index positions in the strings themselves that are of bounds.
    Last edited by 7stud; 02-20-2006 at 10:01 PM.

  12. #42
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    I finally fixed this a couple of days ago.

    I gave up on storing the information in a text file and used some vectors instead. Then I made a function which copies the vectors to an (otherwise unused) text file in a decent format so that I can view and analyse what happened with the genetic algorithm.

    Thanks to everyone who helped, particularly 7stud. I really appreciate you taking the time to lend a hand.

    Cheers.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  2. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 09:01 AM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 01:14 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21