How to use the data obtain in one C file to another C file?

This is a discussion on How to use the data obtain in one C file to another C file? within the C Programming forums, part of the General Programming Boards category; Hi guys, First, I detect coordinates for left and right corners of a mouth in Corners.c and saved in struct ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    How to use the data obtain in one C file to another C file?

    Hi guys,
    First, I detect coordinates for left and right corners of a mouth in Corners.c and saved in struct Coordinate for 72 images.
    How can I able to link it to another C file, let's say try5.c so that it can be used for template matching purpose when i execute 72 images in a loop in try5.c?
    Sorry because I cannot provide the full program because it is a way too long to read. These are the parts which i think very important in order to solve the problem. Help from anyone will be appreciated.

    Thank you very much.

    Code:
    Corners.c
    struct Coordinate
    {
    	int x;
    	int y;
    };
    susan_corners(in,r,bp,max_no,corner_list,x_size,y_size){
    :
    :
    struct Coordinate left_corner, right_corner;
    }
    
    try5.c
    :
    :
    #define NUM 72
    :
    main()
    {
    :
    :
    if((fp33 = fopen("facecolor_input.dat","r")) == NULL){
    		printf("File input filename2  can not open\n");
    		exit(1);
    	}
    	for(k=0;k<NUM;k++){
    		fscanf(fp33,"%s",filename33[k]);
    	}
    	fclose(fp33);
    :
    :
    for(total = 0; total<NUM; total ++){    
    	
    		printf("\nimage==>%d\n",(total+1));
    
    /** read the color data from the file of input face image **/
    		read_data_color(filename33[total],inpcolor);
    :
    :
    /*     Template Matching   */
    //example for eye template matching
                 XX1=56;   // Left X coordinate for eye template
    	YY1=44;
    	XX2=164; //right X coordinate for eye template
    	YY2=44;
    // plf_x, plf_y, prg_x, prg_y are the left and right x & y coordinates from the full input image which has been obtained in Corners.c
    	affine_recog_a(plf_x, plf_y, prg_x, prg_y, XX1, YY1, XX2, YY2);
    	cross_correlation_recog_a(TEMPLATE_a, image_out_a, &CC1[0]);
    :
    :
    }
    Best regards,
    Tommy

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Maybe this will be of use:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    It's for C++, but the same principles applies for C.
    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
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Thank you very much. i will read and try to understand. Hope I can ngain some help from this forum in future because i am a beginner in C.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Any idea in how to save the struct data in header file in a C file before being reused for another C file?

    Thank you.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by tommy_chai View Post
    Any idea in how to save the struct data in header file in a C file before being reused for another C file?
    No idea what you're asking.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    My idea is similar to MacGyver's, but I'll have a punt,

    You want to share the same struct data between C files via headers?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Define the struct in a header file and include it in the appropriate c files. You can check the FAQ for more information on sharing data between files. It's written for C++, though, but it works just as well for C.
    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. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by zacs7 View Post
    You want to share the same struct data between C files via headers?
    Yeah, that might be the question. If it is - extern might be the solution.
    Kurt

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Share the variable or share the actual struct definition?
    If you share the struct definition, all you need to do is define it in a header and include that.
    If you want to share a variable between c files, then you need to define it in one file and add "extern" before it in a header and include that.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Sorry to cause such confusion. Actually, i want to share the same data in two C files. I am using Visual C++ platform. Now i have manually typed down those values and saved in .h file. In another C file, i include the header file and reuse the data, it works.
    The problem is how can I save the data into a header file by C codings automatically? In this case, i need to save the data from a struct. How should i write the codes?
    Another case, if the data is just a Double/Float value, how to write it into a text file or header file?

    Thanks for the replies. I really need help.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can use fwrite to write memory directly to a file in binary format (so for instance you could write out one struct at a time, directly as it's stored internally), and then use fread to read it back in.

    If you want something "human readable" then there's fputs/fprintf and fgets/fscanf.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, obviously your source files contain functions. And the obvious way to make data accessible between functions is to use function arguments and pass the struct (or the address of the struct).

    I like esbo's universal solution to any problem too
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Note: esbo's "solution" as mentioned by anon and Elysia's reply have been deleted from this thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by laserlight View Post
    Note: esbo's "solution" as mentioned by anon and Elysia's reply have been deleted from this thread.
    I note you were unable to supply a reason.


    Seems like censorship to me.

    Did you ask the origanal poster if they would like responses censored?

    No I guess not.

    I guess you know everything and are faultless.

    Well there is your first fault.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    I think it is because you spam your stupid code everywhere and everyone complains on it and you do nothing to fix it, unlike others who actually admits their faults and try to correct their code.
    Your code is so poor in our eyes that you actually do more harm than good teaching it to newbies.

    I really don't mind my reply was deletes as long as I don't have to see your code in the thread, so that I don't have to go out of the way to tell everyone, or at the very least, the OP, that they should ignore your code because it's so poorly written.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  4. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 02:42 AM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21