Thread: esbo's data sharing example

  1. #1
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK. I am not too sure what you are asking.

    I think you want to share data between two programs?

    If so I would would write the data into a file and save it.

    Then the other program can read the data out of the file.

    I think that is easiest.

    I think I did a similar thing, but the same program used the data but it could have been a
    different program.

    It worked very well and is effient and easy, you put the data into a structure and save
    it, then read it back.

    In this case the data is an array of a structures.

    The good thing is you can change the structure and it still works.

    It is a great bit of code as it does so easilly and is blindly fast :O)

    It will read in about 6 magabytes in seconds, or less than a second.

    It is not clear if both programs will be runniing at the same time from your question,
    in which case you might need to coordinate the writing and the reading of the data.

    If that is the case the errno bit might help, it probably will return an error if the file
    is curently being written to I would imagine.

    Code:
    // you might need some of these header files
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <io.h>
    #include <fcntl.h>
    #include <errno.h>
    
    
    #define MAXLIST 30000
    
    
    
    
    
    
    FILE  *dataptr;
    
    typedef struct {
    			char name[40];		
    			float stack;	
    			float dolstack;
    			float tempval;
    			int pfr;
    			float tempstack;
    			} PLYR;
    			
    PLYR pname[MAXLIST]; // an array of structures called pname
    
    
    
    
    
    datasave(){
    
    		if (	(dataptr=fopen("data.doc", "wb+" )) != NULL) {
    				fwrite(  pname, sizeof( pname   ), 1, dataptr);
    				fclose(dataptr);
    			
    		}
    		else {
    			printf("\n error %d", errno); // not needed
    			puts("\nCant creat datafile");
    			exit(2);
    
    		}
    	
    
    }
    
    dataread(){
    
    		if (	(dataptr=fopen("data.doc", "rb" )) != NULL) {
    			fread(  pname, sizeof( pname   ), 1, dataptr);
    			fclose(dataptr);
    			
    		}
    		else {
    			printf("\n read errorerror %d", errno); // not needed
    			puts("\nCant read datafile");
    			exit(2);
    
    		}
    
    }
    So one the first program would use datasave and the second program would use dataread.

    You don't need the errno stuff (probably).

    It is even easier if it is a single structure, you could make your array size 1 (MAXLIST)
    if you have a single structure and then use the same code.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This code is pits. If you're going to show examples, at least do it properly. You use global variables. Bad. Your functions don't return anything. Bad. You should explicitly set the return type and RETURN SOMETHING OF THAT TYPE.
    Not to mention those functions terminate the program if they fail. Incredibly bad code. They are functions and as thus, they should probably return if they fail, NOT kill the whole program.
    And there are better ways to share stuff between processes.
    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.

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Elysia View Post
    This code is pits. If you're going to show examples, at least do it properly. You use global variables. Bad. Your functions don't return anything. Bad. You should explicitly set the return type and RETURN SOMETHING OF THAT TYPE.
    Not to mention those functions terminate the program if they fail. Incredibly bad code. They are functions and as thus, they should probably return if they fail, NOT kill the whole program.
    And there are better ways to share stuff between processes.
    Everything you said in you post is worthless.
    The code is excellent. Of the highest order, infact I would go as far to say it is impossible to
    improve on it.

    You have provided no justification for any of you criticisms you have just pulled them out
    of the air.
    The scope of the variables in every case is entirely correct, it's global data.
    The function returns no value because there is nothing to return.
    It either succeeds or terminates the program.
    The program cannot run if the function fails so there is no point in doing anything
    other than aborting. Anything else is a complete waste of time and effort.
    It does apear you got something right in the past.
    Time to move on now I think!!!
    Last edited by esbo; 01-07-2008 at 02:32 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by esbo View Post
    Everything you said in you post is worthless.
    The code is excellent. Of the highest order, infact I would go as far to say it is impossible to
    improve on it.
    No, YOU are setting VERY bad examples.
    That code is VERY poor.

    You have provided no justification for any of you criticisms you have just pulled them out
    of the air.
    I did not.

    The scope of the variables in every case is entirely correct, it's global data.
    And global variables are to be avoided. Everytime there's a global variable somewhere, you'll see someone suggesting to get rid of them.

    The function returns no value because there is nothing to return.
    All functions that return nothing must return void. Your functions returns int. And what's worse, you don't even return anything, so the return value is undefined.
    All functions should explicitly return a type.

    It either succeeds or terminates the program.
    The functions should return success or fail and main should terminate the program.
    Yes, your code works fine in this example, but in a real program, this is the worst possible kind of thing. You're setting a very bad example to anyone who reads the code.

    The program cannot run if the function fails so there is no point in doing anything
    other than aborting. Anything else is a complete waste of time and effort.
    But it is still not the functions' responsibility to terminate the application!

    Post count means nothing. You could be a professional or a newbie, but it doesn't matter. Your code, however, is not what I would see from an experienced developer.
    It's alright to post code even if you're inexperienced, but this code shows an extreme amount of poor things.
    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.

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Again you can provide no justification for any of you criticism.
    For example "you'll see someone suggesting to get rid of them", so if someone
    suggested you jumped in the fire would you do it?????

    I don't mindless adhere to what other people say untill they can provide some sort of
    justification for it.

    The code works fine in several real programs and has done so for several years without
    failure or requirement for change or maintance, that is more than enough justiation for me.

    If you want to make work for yourself and change it to your 'pprofessional standard' that is fine by me. Go ahead and change it, quite frankly I have better things to waste my time on.

    It is no skin off my nose if you want to waste your time

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How you write your code is fine, but at least I would appreciate that you don't teach newbies your unsafe and sloppy approach to coding.
    There are many examples by members of this board to avoid global variables. You want examples? Let me see if I can find some.

    http://cboard.cprogramming.com/showt...ghlight=global
    http://cboard.cprogramming.com/showt...ghlight=global
    http://cboard.cprogramming.com/showt...ghlight=global
    http://cboard.cprogramming.com/showt...ghlight=global

    4 examples for you.
    Last edited by Elysia; 01-07-2008 at 03:26 PM.
    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.

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Elysia View Post
    How you write your code is fine, but at least I would appreciate that you don't teach newbies your unsafe and sloppy approach to coding.
    There are many examples by members of this board to avoid global variables. You want examples? Let me see if I can find some.

    http://cboard.cprogramming.com/showt...ghlight=global
    http://cboard.cprogramming.com/showt...ghlight=global
    http://cboard.cprogramming.com/showt...ghlight=global
    http://cboard.cprogramming.com/showt...ghlight=global

    4 examples for you.
    http://cboard.cprogramming.com/showt...ghlight=global

    What a laugh.
    The data is global even if you make it local as there is only one function (main).
    People use global variables because 90&#37; of the time it is the sensible way to
    do things.
    It they though it was a problem then obviously would not do it.
    Nobody wants to be passing data all around a program like some kind of mindless idiot.
    It make coding harder and the program run slower.
    Two of the main aims of programming 'experts'.
    Last edited by esbo; 01-07-2008 at 06:54 PM.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Can you show me a program that uses a variable that is both local and global?

    People use local variables because it makes extensibility a heck of a lot easier. Do you understand why?

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by robwhit View Post
    Can you show me a program that uses a variable that is both local and global?
    As the program only consists of main then making the variables local
    to main is pointless when they are global in the sense that they are availabe globally anyway.
    ie to all parts of the program.
    Last edited by esbo; 01-07-2008 at 07:33 PM.

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by robwhit View Post
    People use local variables because it makes extensibility a heck of a lot easier. Do you understand why?

    And if a requirement of extensibility was that all the variables needed to be global
    exactly wha would you have gain by spending a lot of time and effort making
    variables local?????

    Do you understand why your question is based upon an unproven assumption?

    Do you understand why most people could not care less about extensibility
    even if your initial assumption was correct?

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by esbo View Post
    As the program only consists of main then making the variables local
    to main is pointless when they are global in the sense that they are availabe globally anyway.
    ie to all parts of the program.
    Code:
    void func();
    
    int main()
    {
        int x;
        func();
    
        return 0;
    }
    
    void func()
    {
        x = 6;
        return;
    }
    you mean like that?
    Quote Originally Posted by esbo View Post
    And if a requirement of extensibility was that all the variables needed to be global
    exactly wha would you have gain by spending a lot of time and effort making
    variables local?????
    Not much. However, all I heard about global variables is that they hinder extensibility. How would they help in extensibility? AFAIK, this could be a new programming paradigm, and I am really interested to find out about it. Are there any links you could post?
    Do you understand why your question is based upon an unproven assumption?
    I understand that communication relies on many assumptions, because to not do so would make it exorbitantly impractical. However, I do not understand what the assumption was.
    Do you understand why most people could not care less about extensibility
    even if your initial assumption was correct?
    No, I do not understand why people would not care about extensibility. Why would they not care? Are you implying that you are one of those people? And what was the assumption?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by esbo View Post
    What a laugh.
    The data is global even if you make it local as there is only one function (main).
    No, you moron. A real program uses lots of functions, not just main.

    People use global variables because 90% of the time it is the sensible way to
    do things.
    No, you moron. Pretty much any developer out there uses lots more local variables than global ones. Except for you. And they do it because it makes more sense.

    It they though it was a problem then obviously would not do it.
    It's always a problem, you moron. Global variables are not always optimized, they're more error prone, they always stay in memory, etc. So they're always a problem. We consider carefully before we make it global. Otherwise we make it local.

    Nobody wants to be passing data all around a program like some kind of mindless idiot.
    It make coding harder and the program run slower.
    On the contrary, it makes it faster and it makes the design better. Local variables can be optimized, globals are harder because any function can access them! The compiler has a hard time optimizing global variables but with local ones, it knows exactly what functions can access it and can optimize better. Sorry, you fail.

    Two of the main aims of programming 'experts'.
    Yes, which is not you, because you are showing yourself to be a total newbie and does everything you shouldn't.
    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.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > As the program only consists of main then making the variables local
    > to main is pointless when they are global in the sense that they are availabe globally anyway.
    > ie to all parts of the program.
    The point is doing it right when it matters, not when it's optional.

    Sure you can argue until you're blue in the face about equivalence when all you have is a main(), but no real program consists only of just one function. OTOH, some of yours might, but then that's your handicap, not mine.

    In a large program of hundreds, if not thousands of files, a mass of global variables with NO mechanism for tracking or establishing ownership is a disaster waiting to happen. Perhaps you need to read the design of C++ to understand this concept.

    Here's one for you smart boy.
    You've got 4 global arrays, and you want to sort each one of them. Do it without using pointers, or without duplicating code.

    > The code is excellent. Of the highest order, infact I would go as far to say it is
    > impossible to improve on it.
    OK, I want to add the feature which allows you to undo a "save" by maintaining the old and new copies of the database in memory. This of course would be a one-line change had you passed a decent set of parameters into the function. But oh no, we've got to basically rewrite the entire code.
    This would barely get a "pass" if this were the first assignment. It would certainly get a fail at the end of the course, and would get you fired (eventually) in a professional environment.

    I would comment on your poker fiasco, but it would be a waste of effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by robwhit View Post
    Not much. However, all I heard about global variables is that they hinder extensibility.
    Heard??? Heard?

    Did you actually see any proof of this?
    Where is there proof?
    I would prefer a mathetical proof if possible, but if all you can find is some windbag waffling on using big ill defined words he has made up then I will take a look at it.
    You see the thing is I have no problems programming as I so so where are all these problems
    which I am supposed to be having.
    I have experience none whatsoever. Maybe you have had problems?
    Maybe the person who taught you how to program had a lot of programs?
    Maybe he was just a rubbish programmer and will have problems however he programs
    it is just that it will take twice as long to wriite the program.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I originally wanted to close tommy_chai's thread, but I figured the fellow deserved better treatment of his thread for what was not his fault.

    I would prefer a mathetical proof if possible, but if all you can find is some windbag waffling on using big ill defined words he has made up then I will take a look at it.
    You are asking a mathematical proof for what, exactly?
    Last edited by laserlight; 01-08-2008 at 02:35 PM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. COM data sharing
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 09-02-2007, 02:48 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM