Thread: How to Link Various Files

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Lisbon, Portugal
    Posts
    19

    How to Link Various Files

    So, I have this college project, and I need to make a POS application.

    My POS (Point of Sale) has two working modes, as the supervisor, and as an employee.

    To make it simpler, me and my work-mate decided to make various .c programs, to make it simpler to read the code, and possibly understand what's wrong.

    So, we have the main.c file, in which you access to the interface, depending on how you run the program command.

    So far, so good, but, since we want the interface to be a separated file ( interface.c ) how do I, link it to the main.c? It is simply creating an interface.h with the function headlines and including it in main.c?? And how would I compile that with gcc?

    Another question, the project bases a lot on .xml file reading/writing/scanning data, can I just type:

    Code:
     fopen = ("Database.xml", "w");
    ?

    And, other thing, that is this "atoi" function??

    Thanks in Advance!!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    gcc main.c interface.c
    And then ./a.out to execute it if on *nix.

    You can open an XML file in text mode, but you might want to make it easier on yourself by having a library read the XML. Either way, you can do it anyway you want.

    Look up the proper string permission requests for reading/writing/etc. for fopen(). Make sure you don't trash the contents of the file and lose the data by opening it for destructive writing.

    atoi() just attempts to return an integer based upon the C string you give it. You should google for information on it, or run "man atoi" or whatever the actual command is on *nix systems to get information on it.
    Last edited by MacGyver; 12-30-2007 at 04:25 AM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    Lisbon, Portugal
    Posts
    19
    Quote Originally Posted by MacGyver View Post

    You can open an XML file in text mode, but you might want to make it easier on yourself by having a library read the XML. Either way, you can do it anyway you want.

    Look up the proper string permission requests for reading/writing/etc. for fopen(). Make sure you don't trash the contents of the file and lose the data by opening it for destructive writing.
    Okay, I never heard of this in College, so.. I can just make a file, say "read_xml.c" and make a read_xml.h file?

    Well, considering I have, numbers, and characters to write/replace/read, if I just make the function write/read between the "<" ">" and "</" tags, would I be fine?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Chinfrim View Post
    Okay, I never heard of this in College, so.. I can just make a file, say "read_xml.c" and make a read_xml.h file?
    I meant a library already written, but yeah, definitely, you could make it into a separate module if you think you could reuse it later.

    Quote Originally Posted by Chinfrim View Post
    Well, considering I have, numbers, and characters to write/replace/read, if I just make the function write/read between the "<" ">" and "</" tags, would I be fine?
    Of course. Some people just don't like the idea of sloshing through a bunch of XML, and trying to parse it all. If you feel comfortable with file IO enough to handle it all yourself, and your XML files will be relatively simple, then writing your own XML read/write functions might be good.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Location
    Lisbon, Portugal
    Posts
    19
    Okay, thanks a lot, MacGyver, guess I'll have to study a bit more of file reading/writing,

    Specially the part where the function finds a line and write in a specific spot.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    To make it simpler, me and my work-mate decided to make various .c programs, to make it simpler to read the code, and possibly understand what's wrong.

    So, we have the main.c file, in which you access to the interface, depending on how you run the program command.
    Regrading your first question, if you wanted to link you interface.c file into your main. What you have to do just create an object of your interface and then link the object file into your mian. So for example

    Code:
    gcc -c -o interface.c
    
    gcc -o main main.c interface.o
    That will create you main as your exe file and just run your main.

    ssharish

  7. #7
    Registered User
    Join Date
    Dec 2007
    Location
    Lisbon, Portugal
    Posts
    19
    Okay, but still I need to create the "interface.h" file and write the:

    Code:
    #include "interface.h"
    Where interface.c would have, for example, function A, which would create the menu, B which would read the file, and C, which would write on the file.

    interface.h should only be:

    Code:
     
    #include<lib1.h>
    #include<lib2.h>
    
    A;
    B;
    C;
    ??

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Chinfrim View Post
    Okay, but still I need to create the "interface.h" file and write the:

    Code:
    #include "interface.h"
    Where interface.c would have, for example, function A, which would create the menu, B which would read the file, and C, which would write on the file.

    interface.h should only be:

    Code:
     
    #include<lib1.h>
    #include<lib2.h>
     
    A;
    B;
    C;
    ??
    Your .h file would normally have all the function prototype and macros and all. What you have to do is include your .h file both in interface.c and main.c. And now create the interface.o file and now link the .o file to your main.c file. You might well need to provide the path for your header file as well. Like say for example

    Code:
    gcc -c interface.c interface.h
    assuming that you have the .h file in the same dir.

    ssharish

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    For future reference, never compile header files unless you know what you're doing and you're using precompiled headers.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I tried with not including the header file while compiling, but i got an error message and decided to pass is as parameter to gcc. And everything went fine. May be the .h file path wasn't right.

    So perhaps i should say to OP, if you have your .h file in the current directory don't include them while compiling.

    ssharish

  11. #11
    Registered User
    Join Date
    Dec 2007
    Location
    Lisbon, Portugal
    Posts
    19
    Meh, it's too much to learn in one week, while studying for another subjects, I guess I'll fail.. sigh.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Failing is up to you.

    If you work on it slowly, a little day by day, you can do it in a week, depending on how much work there is.

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Chinfrim View Post
    Meh, it's too much to learn in one week, while studying for another subjects, I guess I'll fail.. sigh.
    Why do you think, that way. Don't ever think you will fail. If you think that way you will definitely fail. Think positive.

    ssharish

  14. #14
    Registered User
    Join Date
    Dec 2007
    Location
    Lisbon, Portugal
    Posts
    19
    Quote Originally Posted by MacGyver View Post
    Failing is up to you.

    If you work on it slowly, a little day by day, you can do it in a week, depending on how much work there is.
    Well, considering, I don't know how to exactly read from a file, and write in it, how to make dynamic structs, and this project is all about that, I have a long way to go.

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    fgets() and fgetc() are the way to read text.

    One way to read/write, is to read from the file, while making your changes to the strings you are reading. At the same time, write the new version to a new file. When you're done, close both files, delete the original, and rename the new one with the name of the original. There are other ways to possibly go about it, but this is a pretty simplistic approach.

    Dynamic structs.... I assume you mean dynamically sized arrays of structs, or something similar. It's the same concept as a dynamic array for ints or any other basic type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  4. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  5. reading from files
    By recluse in forum C Programming
    Replies: 25
    Last Post: 12-26-2004, 10:33 AM