C++ File read access violation help!!!

Discussion in 'OT Technology' started by ranthal, Dec 4, 2007.

  1. ranthal

    ranthal i did what?

    Joined:
    Feb 8, 2007
    Messages:
    772
    Likes Received:
    0
    Location:
    Los Angeles
    ok so i've included the code where i'm getting 0xC0000005: Access violation reading location 0xccccccd0. when it compiles at the while(!file.eof()) when the counter htis 359. Problem is this is a partner's code he developed so i can test a completely different function, so I can't figure out what he did wrong. Can anyone help?

    int main() {
    //6682-6686
    //6706-6710
    ifstream file;
    char file_1[200]="\\\\labsamba2.seas.ucla.edu\\ee180d\\CanePress_1_1194536682.txt" ;
    char file_2[200]="\\\\labsamba2.seas.ucla.edu\\jiahwa\\Desktop\\CanePress_1_1194536684.txt" ;
    char file_3[200]="\\\\labsamba2.seas.ucla.edu\\jiahwa\\Desktop\\CanePress_1_1194526686.txt" ;
    float temparray[200], newarray[201], oldarray[201];
    for(int a=0; a<3; a++){
    if(a==0)file.open(file_1);
    elseif(a==1)file.open(file_2);
    elseif(a==2)file.open(file_3);
    char line[2000];
    int counter=0;
    oldarray[0]=-10000;
    float one, two, three, pressure, five, six;

    while(!file.eof()){
    file.getline(line, 2000);
    if(line[2]=='/'){}
    else{
    sscanf(line," %f %f %f %f %f %f", &one, &two, &three, &pressure, &five, &six);
    temparray[counter]=pressure;
    counter++;
    }
    }
    if(oldarray[0]==-10000){
    for(int x=0; x<200; x++){
    newarray[x]=temparray[x];
    oldarray[x]=temparray[0];
    }
    }
    else{
    for(int x=0; x<200; x++){
    oldarray[x]=newarray[x];
    newarray[x]=temparray[x];
    }
    }
    }
    int data_success;
    data_success = data_analysis(newarray, oldarray);
    return data_success;
    }
     
  2. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    could you post the input files
     
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    You're kinda lucky to get up to 359 when temparray's defined size is 200.
     
  4. ranthal

    ranthal i did what?

    Joined:
    Feb 8, 2007
    Messages:
    772
    Likes Received:
    0
    Location:
    Los Angeles
    i caught one thing which was that i had only updated the 1st file, not the second and third, but it shouldn't matter because when i debugged it only hit the for loop once meaning it only treated the 1st file case. anyways, here is what one of them looks like:

    11/08/07 15:44:42
    0 2.323303 2.502670 1.070175 0.775452 8602
    1 2.188568 2.500000 0.970230 0.784836 8603
    2 2.179260 2.426758 0.756912 0.742569 8604
    3 2.246628 2.369385 0.575027 0.852661 8605
    .
    .
    .
    198 2.779541 1.676636 0.581284 0.539017 8800
    199 2.741013 1.683426 0.452881 0.546188 8801
     
  5. ranthal

    ranthal i did what?

    Joined:
    Feb 8, 2007
    Messages:
    772
    Likes Received:
    0
    Location:
    Los Angeles
    yeah i know :ugh:
     
  6. ranthal

    ranthal i did what?

    Joined:
    Feb 8, 2007
    Messages:
    772
    Likes Received:
    0
    Location:
    Los Angeles
    it seems to not be accurately retrieving the values from the .txt file... is it cuz of the pass by reference in sscanf?
     
  7. Dnepr

    Dnepr Guest

    "when it compiles at the while(!file.eof())"

    Does this error occur runtime or compile time?
     
  8. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    You said the counter reached 359 before it crashed, correct?
    Did you not get the clue that modifying the 359th element of a 200 element is probably bad?
     
  9. Bruticus

    Bruticus half dead OT Supporter

    Joined:
    Apr 10, 2004
    Messages:
    4,608
    Likes Received:
    0
    Location:
    Melbourne
    . :rofl:
     
  10. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Why aren't you using strings?
     
  11. CodeX

    CodeX Guest

    Strings are gay, even if the rest of my source is C++ I still use C-style character arrays, usually as pointers

    void open(char* filename){...}

    open("example.txt");
     
  12. Dnepr

    Dnepr Guest

    Why are strings gay in C++?
     
  13. ranthal

    ranthal i did what?

    Joined:
    Feb 8, 2007
    Messages:
    772
    Likes Received:
    0
    Location:
    Los Angeles
    a few responses...

    the error is a runtime error.

    yeah i know it's bad that it's trying to read the 359th element, that's why i included that.

    another bad thing is that even before it reaches what should be the end of file it doesn't even assign values to temparray, still just the default value: -infinite something.

    this code is only for another function's debugging purposes, and ultimately that function will be compiled in C by GCC (Linux) so i think my partner was programming with that in mind...

    cliffs: i think the problem is somewhere between opening the file and getline
     
  14. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Put some sort of safeguard against overstepping your array boundary...
    e.g. while (counter < 200 && !file.eof())

    And, check that a line was successfully read before attempting to parse the data...

    if (!file.getline(line,2000).fail())
    {
    parse data here
    }

    You will most likely get a failed getline before the eof bit is set.
     
  15. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    because dude REAL programmers never make array bound errors duh!
     
  16. ranthal

    ranthal i did what?

    Joined:
    Feb 8, 2007
    Messages:
    772
    Likes Received:
    0
    Location:
    Los Angeles
    figured it out and it was a problem with reading off a network drive. thanks for the help
     
  17. ranthal

    ranthal i did what?

    Joined:
    Feb 8, 2007
    Messages:
    772
    Likes Received:
    0
    Location:
    Los Angeles
    hahaha and i just compiled the 2 test cases and already got my code working perfect.

    time spent debugging partner's code = 3 hrs
    time spent debugging my code = 10 mins
    white engineer owning asian = priceless
     
  18. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    If only you knew how to use char arrays properly, or maybe (gasp) dynamically allocate your own memory and write your own string class, then your bullshit would be valid. Alas, you do not.

    Cliffs: I think the problem is somewhere between the chair and the keyboard.
     
  19. CodeX

    CodeX Guest

    are you talking to me? I am not the OP... you know that right? I know how to dynamically allocate memory lol
     

Share This Page