Thread: File I/O (Reading from a Random-Access File)

  1. #16
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Yeah that looks like memory corruption/bad pointers to me. I see you are using the ID as the index into the file.
    Last edited by abachler; 02-12-2008 at 04:49 PM.

  2. #17
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Quote Originally Posted by abachler View Post
    I see you are using the ID as the index into the file.
    Yes. Can i do that?
    & What would you suggest.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by abachler View Post
    As for using template, the guy is just learning the basics, there is no point in throwing too much at him too fast. Stick to basic types, yeah im just so used to using LPVOID I shoudl have used void*, but thats better than using templates.
    Hehehe... I'm not sure I agree with that. It's always better to be type-safe rather than using generic pointers. In C++, we rarely need the void* anymore and we should avoid it too IMHO.
    The templates are indeed better than the generic void*, but that's if the OP has learned to use them yet. I don't know.

    yes Next and Prev should be LINK*
    Now that's even better!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    hmmmm . ok.... o_0 I don't know WTF are you talking about but I need some help here. ^^

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, let's think about it here.
    First, your operator >> is not supposed to ask for data. That's your main program's doing. The object should just serialize itself. And it should probably do that in such a way that it writes a total of sizeof(yourobject) bytes so it seek alright.
    You need to separate code for object and program.
    Then you realize that IDs can be stored in any order, right? I can type in 1000 for the first, and then 10 for the second. The program would seek to 1000 * sizeof(myobject) and expect the data to be there, but it isn't.

    What you need is an index. A map, perhaps. Map id to position in file.
    Write that contents of the map to the file too when you close the program as an index, and load the index when the program starts.
    Then the problem should be fixed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    :| The problem is that you are assuming I'm an expert. I"m just a newbie so what you just said right there doesn't make any sense to me whatsoever.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think I was perfectly honest with you. Try to think into terms what happens if you enter 1000 for ID, for example. How will the program react? Use a debugger if you must. Se why you get what you get.
    You need an index. Or you can simply make the ID automatic for every object. Or omit it completely.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Ah, it's your logic in circle's operator >> that's broken. Think about the method again and what it does, and why it can't possibly work the way you want it to.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #24
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Quote Originally Posted by CornedBee View Post
    Ah, it's your logic in circle's operator >> that's broken. Think about the method again and what it does, and why it can't possibly work the way you want it to.
    Like everyone said i let the loop generate the ids, but still not working:

    Code:
    ostream& operator <<(ostream& output, const Circle& aCircle)
    {
    	output << aCircle._circleId << "    " << aCircle._circleRadius;
    
    	return (output);
    }
    
    istream& operator >>(istream& input, Circle& aCircle)
    {
    	int quantity;
    	cout << "\nHow many circles do you want to add ";
    	input >> quantity;
    
    	for (int i = 0; i < quantity; ++i)
    	{
    		aCircle._circleId = i;
    		input >> aCircle._circleRadius;
    	}
    
    	return (input);
    }
    Here's an output example:

    http://i29.tinypic.com/27zbz43.jpg

  10. #25
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Like everyone said i let the loop generate the ids, but still not working
    I do not see where you "add" Circles.
    You always work with the same variable - overwriting it on each loop iteration
    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

  11. #26
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Right, let's try this again. Perhaps this time without flaming.

    You still haven't thought about >> and its purpose enough. Moreover, you haven't thought about the implementation and what it does enough.

    Consider this: a variable holds one item of data. (The data can be large and complex, depending on the type of the variable.) >> should fill one variable. Consider what data a single Circle object holds, and what your >> tries to do.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM