Thread: Getting Garbage Value

  1. #1
    Registered User
    Join Date
    Jan 2014
    Location
    Singapore
    Posts
    6

    Getting Garbage Value

    Basically my program creates a binary file. Then user can add records consisting of id number and name and write to that file, update it, and create a text file version of that file. Update option will display what is in the file. After entering id and name, user can add more records by pressing Y.

    My problem now is that I sometimes get garbage value when the update/display option is selected and also when I select the option to create a text file version of that binary file. I can't figure out what I did wrong and I hope you can help me. This is my code for writing to the binary file.

    Code:
    void getRecord(fstream& afile)
    {
        char stuId[MAX];
        char stuName[MAX];
        
        char newLine[MAX];
        int size = 0;
        int size1 = 0;
            
        int lineSize = 0;
    
        cout << "Enter student id: ";
        cin >> stuId; 
        cin.clear();
        cin.ignore(100, '\n');
        cout << "Enter student name: ";
        cin.getline(stuName, MAX);
    
        size = strlen(stuId);
        size1 = strlen(stuName);
            
        strcpy(newLine, stuId);
        strcat(newLine, " ");
        strcat(newLine, stuName);
        strcat(newLine, "\n");
        puts(newLine);
        
        lineSize = strlen(newLine);
        afile.write(newLine, lineSize);
        cout << newLine;                   
        
        cout << "Student " << stuName << " appended" << endl;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I'd query why you are using C-style strings and string operations with C++ I/O, but that's not related to your question.

    In any event, you haven't provided enough information.

    1) How is the stream afile being opened? Is it an ofstream, an ifstream, or what? Is it being opened in binary mode?
    2) The relationship between the function you've shown and "update/display option" is unclear.
    3) Is the file opened in binary or text mode? Bear in mind these modes - depending on implementation - can affect handling of characters like newline.
    4) Is the code that READS from the file working the same way? i.e. if you are writing in binary mode, are you reading in binary mode? If not, why not?



    In future, rather than just providing a function which may or may not be implicated in your problem, try whittling your code to provide a small but complete (i.e. other people can compile and execute) sample of code that exhibits the same problem. In the process of producing such a sample, you might have an "aha" moment, and realise what the problem is - yourself. If not, at least you can write a post asking for help that is not playing the forum equivalent of blind-man's-bluff.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Location
    Singapore
    Posts
    6
    Duly noted. You're right, I did manage to find the problem. It's with line 25. When I comment that out, no more garbage values.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, actually, your problem is more than just line 25. Consider line 13:
    Code:
    cin >> stuId;
    You did not specify the field width, so if the user enters MAX or more characters, you will have a buffer overflow.

    Next, consider lines 22 and 23:
    Code:
    strcpy(newLine, stuId);
    strcat(newLine, " ");
    So, you copy stuId to newLine. Both can store null terminated strings of up to MAX-1 characters in length, so if there was no buffer overflow when reading into stuId, you're okay. But then you append " ". This is a potential buffer overflow. The same goes for the subsequent lines 24 and 25.

    One way to avoid this is to compute the maximum length of the resulting string beforehand and thus create newLine to have the necessary storage.
    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

  5. #5
    Registered User
    Join Date
    Jan 2014
    Location
    Singapore
    Posts
    6
    I'm not fully sure, but I think it works. So far so good when creating text file. There are no garbage values in it. I made these changes.
    Code:
        size = strlen(stuId);
        size1 = strlen(stuName);
        newSize = size + size1 + 3;     
        char *newLine = new char[newSize];
         
        strcpy(newLine, stuId);
        strcat(newLine, " ");
        strcat(newLine, stuName);
        strcat(newLine, "\n");
        puts(newLine);
        
        lineSize = strlen(newLine);
        afile.write(newLine, lineSize);
    Now only update shows garbage at the end of the list. Can you look at my update function and tell me what is wrong? I am assuming it's the size problem as well or perhaps the cout of stuRecord.

    Code:
    void updateRecord(fstream& afile, char *afileName)
    {
        streampos size;
         
        afile.open(afileName, ios::in | ios::out | ios::binary | ios::ate);
    
        size = afile.tellg();
        afile.seekg(0, ios::beg);
          
        char *stuRecord = new char [size];
        afile.read(stuRecord, size);    
        cout << stuRecord;
            
        afile.close();
    
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to null terminate the string in updateRecord.

    Maybe it is time for grumpy's query: why are you using C-style strings and string operations with C++ I/O?
    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

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You aren't null-terminating the character string after reading in the data.


    You aren't deleting stuRecord's memory.


    There's no point passing in afile. It should just be a local variable.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Jan 2014
    Location
    Singapore
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Maybe it is time for grumpy's query: why are you using C-style strings and string operations with C++ I/O?
    To be honest, I don't know. I mean, I don't know that's what I'm doing.

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Well, if you're not in a class and being taught to do it that way. IE you are teaching yourself, lets get you off on the right foot. Actually, we should do that either way.

    Heres an excellent reference point for C++:
    C++ reference - cppreference.com

    And then obviously you'll want at this point to narrow in on the IO area and read up on C++ style IO manipulation.

  10. #10
    Registered User
    Join Date
    Jan 2014
    Location
    Singapore
    Posts
    6
    Thanks for the replies. I have decided to do things differently. I am now using structures to add the number and name and everything is working perfectly. I've bookmarked that site, it looks amazing and I'll definitely check there more often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. garbage value
    By parvathi in forum C Programming
    Replies: 6
    Last Post: 12-29-2013, 03:36 AM
  2. Garbage appearing
    By DeanWinchester in forum C Programming
    Replies: 3
    Last Post: 05-29-2012, 03:21 AM
  3. Garbage Collection
    By Orborde in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2005, 11:18 PM
  4. File Garbage
    By Vicious in forum C++ Programming
    Replies: 9
    Last Post: 07-20-2004, 03:34 PM
  5. garbage collection
    By joed in forum C Programming
    Replies: 4
    Last Post: 04-01-2004, 01:47 PM

Tags for this Thread