Thread: Something wrong with output

  1. #16
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    @Quzah: yeah, I just figured the crash has something to do with the struct. And yeah, maybe I shouldn't have included that menu thingy when I posted my code.

    Now what to do next? It seems Adak's hypothesis is right. But I'm not quite sure which part of my code I should change to make the buffer fit my struct. Any tips? I want to insist on putting 7 data in my struct by the way (and sorry about the inconsistency in my posts...struct originally had 4 data only but now it has 7). What should I do with the j? I already tried Adak's advice but it seems to lack some more adjustments.

  2. #17
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    Quote Originally Posted by Adak View Post

    So I would go through the add record function and allow only a maximum length of 1 less than the length of the field it is to fit into. That will leave a space for the end of string char: '\0', to be placed there.
    Um...may I ask how? I don't quite get what you're referring to. Oh, and thanks for the help again.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    See my edit I just made in my last post for an example for scanf()

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Start over.

    1 - Figure out what your file should look like.
    2 - Make a sample file.
    3 - Make a program that loads that and displays it.
    4 - Make a program that writes what your file should look like.
    5 - Make a program that loads and writes however many you want.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    Quote Originally Posted by Adak View Post
    See my edit I just made in my last post for an example for scanf()
    when you tried doing that in your compiler, did it work fine? In my case, it didn't. This is what I did in the add_record() (is it correct?):

    Code:
    void add_record() 
    {
    
       record stud;
       char ch;
       FILE *fp;
        fp=fopen("std.dat","r+");
        if(fp==NULL)
    	fp=fopen("std.dat","w");
    
        clrscr();
        gotoxy(35,1);printf("\n\nADD STUDENT RECORD\n\n");
    
        do
        {
            printf("\n\nStudent number (must be 9 digits) = ");
            scanf("%lld", &stud.snum);
            printf("\nPassword                          = ");
    	scanf("%50s", &stud.password);
            printf("\nLast name                         = ");
    	scanf("%31s", &stud.lname);
            printf("\nFirst name                        = ");
    	scanf("%31s", &stud.fname);
            printf("\nMiddle initial                    = ");
    	scanf("%3s", &stud.mi);
            printf("\nBirthdate (MMDDYY)                = ");
    	scanf("%7s", &stud.bday);
            printf("\nCourse (ex.BSM)                   = ");
    	scanf("%6s", &stud.course);
    
    	fseek(fp,0,SEEK_END);
            fwrite(&stud,sizeof(stud),1,fp);
    
            printf("\n\nAdd another record? Y if yes\n\n");
            ch=toupper(getche());
    
        } while(ch=='Y');
    
        fclose(fp);
        getch();
    }
    May I see what how you did it? Maybe I didn't do it right.
    Last edited by preeengles; 04-14-2009 at 09:41 PM.

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you are using fwrite, you should be pairing it with fread. You are writing the entire structure, not just the length of the string. Consider:
    Code:
    char array[ 100 ] = "Foo";
    If I write that with fwrite, using sizeof to get the array size, as you are with your structure size, it's going to write out 100 characters. If I scan it in with fscanf, I'm only going to read four. The other 96 are still waiting to be read.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    @Quzah: I tried that but the only difference it made was it placed the latest record inputted at the end of the file.

    @Adak: Really sorry if I'm being dumb. I have a feeling I'm doing it wrong. May I see how you did it? Oh, and I think it said 'No record found' cuz the original entry was changed in the memory because of the overflow.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, you must leave room for the end of string char.

    This revision to your add records function got that working right:

    Code:
    void add_record() 
    {
    
       record stud;
       char ch;
       FILE *fp;
       if((fp=fopen("std.dat","r+")) == NULL) {
          printf("\n Unable to Open std.dat File - exiting");
          exit(1);
       }
       clrscr();
    
       gotoxy(35,1);printf("\n\nADD STUDENT RECORD\n\n");
    
       do
       {
          printf("\n\nStudent number (must be 9 digits) = ");
          scanf("%lld", &stud.snum);
          printf("\nPassword                          = ");
    	   scanf("%49s", &stud.password);
          printf("\nLast name                         = ");
    	   scanf("%30s", &stud.lname);
          printf("\nFirst name                        = ");
          scanf("%30s", &stud.fname);
          printf("\nMiddle initial                    = ");
          scanf("%2s", &stud.mi);
          printf("\nBirthdate (MMDDYY)                = ");
          scanf("%6s", &stud.bday);
          printf("\nCourse (ex.BSM)                   = ");
          scanf("%5s", &stud.course);
    
          fseek(fp,0,SEEK_END);
          fwrite(&stud,sizeof(stud),1,fp);
    
          printf("\n\nAdd another record? Y if yes\n\n");
          ch=toupper(getche());
    
       } while(ch=='Y');
    
       fclose(fp);
       getch();
    }
    I would put the student number into a smaller number. Nine digits is in the hundred million range, after all. I can't imagine a school having to use that many record numbers.

    Anyway, trim down your input sizes for your other functions where you get input from the user - like modify records, and see how that works.

  9. #24
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    *SIGH* I'm getting really ........ed with this already. Just a while ago, the program worked fine. I thought the problem was finally done but when I tried rerunning the program, it crashed again.

    I'm losing hope already. What's with all this random errors anyway? Sometimes, I have to exit the IDE or restart to computer just to have the code working right again. It's very inconsistent. Funny thing is, with the other codes I've made, this inconsistency never occurred. I'll try looking into other aspects. Maybe this wasn't just caused by the overflow. I asked someone to run my program (the one w/ the original long code) in his computer and he said he wasn't able to reproduce the bug. Weird...

    I'm also considering quzah's suggestion about redoing my program. If you have some more ideas as to the cause of the bug, please do tell.

    Thanks again.

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by preeengles View Post
    *SIGH* I'm getting really ........ed with this already. Just a while ago, the program worked fine. I thought the problem was finally done but when I tried rerunning the program, it crashed again.

    I'm losing hope already. What's with all this random errors anyway? Sometimes, I have to exit the IDE or restart to computer just to have the code working right again. It's very inconsistent. Funny thing is, with the other codes I've made, this inconsistency never occurred. I'll try looking into other aspects. Maybe this wasn't just caused by the overflow. I asked someone to run my program (the one w/ the original long code) in his computer and he said he wasn't able to reproduce the bug. Weird...

    I'm also considering quzah's suggestion about redoing my program. If you have some more ideas as to the cause of the bug, please do tell.

    Thanks again.
    Don't get frustrated! De-bugging is always harder than writing out the original code, particularly if it's not as clear as it should be.

    But your program is quite clear, so let's carry on, here. You will *have* to develop your troubleshooting skills, and this is a great place to do it. Much better than sitting by yourself, looking cross-eyed at the monitor, without a clue.

    I only added two records to the student data file, but the display worked over and over, without any errors (which it did *not* do before), but that depends on what data you entered.

    I suspect your friend had no problems, because he just luckily entered data that was shorter than the buffer space, which left room for the end of string char.

    Since the display works fine, we can say that the writing out of the data, and the reading in from the file, must also be working, with good (but not perfect), confidence.

    One problem I've found in a few functions is that you open the file with a local FILE pointer. Then you exit the function, and leave the file still open. So the file is open, but you can't get to the file pointer, because it's no longer in scope, and has vanished.

    Then in another function, the file is opened again by another local FILE pointer, and not closed before the function exits.

    The del_SR function also looks like somebody was trying to build a "piano", there - lots of extra code.

    It will take another day before I get the time to make up a half decent version to show you. Relax for a day, and then you can take the rough version i'll post up, and polish it off, from there, ok?

    TTYTomorrow.
    Last edited by Adak; 04-15-2009 at 07:18 AM.

  11. #26
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    Quote Originally Posted by Adak View Post
    What did you do in the program when it crashed? Had you been running it through modify record() ? I'll bet so.
    Yeah, the display works fine, and only crashes when I modify one of the records inputted.

    Quote Originally Posted by Adak View Post
    I suspect your friend had no problems, because he just luckily entered data that was shorter than the buffer space, which left room for the end of string char.
    Well he said he ran the program on Unix...I've never tried Unix before. Would running the program on Unix really make a difference?


    Quote Originally Posted by Adak View Post
    So the *big* problem remaining is the modify record function. Any other function that, by itself (without calling modify record), causes a crash or error?
    Yes. I tried entering two records and then called delSR() which deletes one record.

    delSR() is supposed to search the student first and display it on screen, then ask confirmation from the user if he truly wishes to delete the record displayed. It works fine when the student to be deleted is the 1st record added by the user. However, when the user prompts to delete the 2nd record, it would display weird characters like the ones shown on my first post.

    It says something like this:

    Code:
    Student record found:
    
    STUDENT #: 11111111
    NAME      :  ░i╘↕§ever
    BDAY       : 010101
    COURSE  : 54
    
    Are you sure you want to delete the above student record?


    Quote Originally Posted by Adak View Post
    Here's what I'd like you to do, delete the student.dat file, which is probably corrupted by the modify function, and add new student records. *DO NOT RUN MODIFY RECORD()*!

    Now run the program through every function, in order, 5 times, *except for modify record*. One function 5 times, then the next function 5 times, and so on.
    I've already tried doing those before. They all technically worked fine, except that the displayed characters were weird. Like I said in my first post, if only one record is added, everything works perfectly fine.


    One problem I've found in a few functions is that you open the file with a local FILE pointer. Then you exit the function, and leave the file still open.
    Hee...sorry about that. I thought it was ok.

    The del_SR function also looks like somebody was trying to build a "piano", there - lots of extra code.
    It's long because I had to copy the retained data to a new file and rename it as the original (that explains all the strcpys in there). del_SR's supposed to delete one record only. I couldn't think of any other method other than what I did. Forgive me for my noobness

    It will take another day before I get the time to make up a half decent version to show you. Relax for a day, and then you can take the rough version i'll post up, and polish it off, from there, ok?
    Thank you much for all the help. I should've constructed my question in a clearer way in my first post to avoid all this ambiguity. Sorry about that. I'm just no good with explaining.
    Last edited by preeengles; 04-15-2009 at 07:52 AM.

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I've found the other problem with the code, and will have a rough version for you, tomorrow. Del_SR and several others, have one or more fatal problems.

    Just relax, and I'll upload the rough version, tomorrow. You don't need to run anything right now.

    Unix is no different, those systems just have more memory, typically. More memory means you might be able to run much longer before the program crashes, but it would *definitely* crash, as is.

  13. #28
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you have not yet removed & in scanf calls that read strings?
    also checking the return value of scanf and fwrite would be a good idea.

    could you post smallest compilable sample illustration the problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #29
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    quzah's asked me the same thing before (i.e. to post only a small sample)...but I don't even know exactly which part caused the problem.

  15. #30
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by preeengles View Post
    quzah's asked me the same thing before (i.e. to post only a small sample)...but I don't even know exactly which part caused the problem.
    so copy your workproject to the side and start removing part that are not essential to the bug. Compile and see that the bug still exists...

    If removing something eliminates the bug - return it and examine closely.

    If you still do not understand the issue - continue removing other parts till you get sample with the reasonable size that could be posted on the forum
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 07:55 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 10:00 PM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 07:58 PM
  4. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 01:58 PM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM