Is it Possible? - Running a Program with Data Structures in Separate Files

In summary, the conversation discusses the possibility of running a program with the main function located in a separate .c file from the data structures and input file it uses. It is confirmed that this is possible, and an example of how to compile the program is provided. The conversation also mentions the use of an additional .c file for functions, and it is explained that it can also be compiled along with the other files.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

I have the main function of a program in a .c file.
The main function uses data structures which are defined in an other .c file.
And the main function uses also a .txt file to get some inputs.

Is it possible to run the program if the main function is an other file as the definition of the structures?? (Wondering)
 
Technology news on Phys.org
  • #2
mathmari said:
Hey! :eek:

I have the main function of a program in a .c file.
The main function uses data structures which are defined in an other .c file.
And the main function uses also a .txt file to get some inputs.

Is it possible to run the program if the main function is an other file as the definition of the structures?? (Wondering)

Hi! (Happy)

Sure.
Your program should look something like:

input.txt:
Code:
ss=1
ss=101

stars.h:
Code:
typedef struct starsy {
  int ss;
} starsy_t;

main.c:
Code:
#include <stdio.h>
#include "stars.h"

starsy_t StarS[N];
int nrStarS = 0;

int main() {
  FILE *fp = fopen("input.txt", "r");
  if (fp != NULL) {
    while (1 == fscanf(fp, "ss=%d", &StarS[nrStarS].ss)) {
      nrStarS++;
    }
    fclose(fp);
  }
  return 0;
}
(Wasntme)
 
  • #3
I like Serena said:
Hi! (Happy)

Sure.
Your program should look something like:

input.txt:
Code:
ss=1
ss=101

stars.h:
Code:
typedef struct starsy {
  int ss;
} starsy_t;

main.c:
Code:
#include <stdio.h>
#include "stars.h"

starsy_t StarS[N];
int nrStarS = 0;

int main() {
  FILE *fp = fopen("input.txt", "r");
  if (fp != NULL) {
    while (1 == fscanf(fp, "ss=%d", &StarS[nrStarS].ss)) {
      nrStarS++;
    }
    fclose(fp);
  }
  return 0;
}
(Wasntme)

Ahaa... Ok! (Malthe)

And to compile it what do I have to write??

[m]gcc main.c[/m] ??

(Wondering)
 
  • #4
mathmari said:
Ahaa... Ok! (Malthe)

And to compile it what do I have to write??

[m]gcc main.c[/m] ??

(Wondering)

Yep! (Nod)
 
  • #5
Can it also be that besides [m]input.txt[/m], [m]stars.h[/m] and [m]main.c[/m] there is also an other .c file ([m]stars.c[/m]) where there are all the functions (Beginning(), Consitution(), destruction(), ... )?? (Wondering)

If so, how can I compile it?? (Wondering)

Because when I write [m]gcc main.c[/m] I get:

[m]
/tmp/ccacL3pX.o:main.c:(.text+0x122): undefined reference to `_Initialization'
/tmp/ccacL3pX.o:main.c:(.text+0x1a7): undefined reference to `_Constitution'
/tmp/ccacL3pX.o:main.c:(.text+0x28a): undefined reference to `_Beginning'
/tmp/ccacL3pX.o:main.c:(.text+0x38b): undefined reference to `_asteroid_constitution'
/tmp/ccacL3pX.o:main.c:(.text+0x455): undefined reference to `_destruction'
/tmp/ccacL3pX.o:main.c:(.text+0x542): undefined reference to `_asteroid_freefloating_boom'
/tmp/ccacL3pX.o:main.c:(.text+0x676): undefined reference to `_freefloating_collection_boom'
/tmp/ccacL3pX.o:main.c:(.text+0x76d): undefined reference to `_SolarSystem_Combiner'
/tmp/ccacL3pX.o:main.c:(.text+0x819): undefined reference to `_Search_asteroid'
/tmp/ccacL3pX.o:main.c:(.text+0x8cf): undefined reference to `_get_asteroids'
/tmp/ccacL3pX.o:main.c:(.text+0x97b): undefined reference to `_type_planetary'
/tmp/ccacL3pX.o:main.c:(.text+0xa1d): undefined reference to `_type_ffcol'
/tmp/ccacL3pX.o:main.c:(.text+0xabf): undefined reference to `_type_StarSystem
/tmp/ccacL3pX.o:main.c:(.text+0xb01): undefined reference to `_type_universe'
/tmp/ccacL3pX.o:main.c:(.text+0xb2c): undefined reference to `_termination'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: /tmp/ccacL3pX.o: bad reloc address 0x11c in section `.rdata'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: final link failed: Invalid operation
collect2: ld returned 1 exit status
[/m]

(Wondering)
 
  • #6
mathmari said:
Can it also be that besides [m]input.txt[/m], [m]stars.h[/m] and [m]main.c[/m] there is also an other .c file ([m]stars.c[/m]) where there are all the functions (Beginning(), Consitution(), destruction(), ... )?? (Wondering)

If so, how can I compile it?? (Wondering)

Because when I write [m]gcc main.c[/m] I get:

[m]
/tmp/ccacL3pX.o:main.c:(.text+0x122): undefined reference to `_Initialization'
/tmp/ccacL3pX.o:main.c:(.text+0x1a7): undefined reference to `_Constitution'
/tmp/ccacL3pX.o:main.c:(.text+0x28a): undefined reference to `_Beginning'
/tmp/ccacL3pX.o:main.c:(.text+0x38b): undefined reference to `_asteroid_constitution'
/tmp/ccacL3pX.o:main.c:(.text+0x455): undefined reference to `_destruction'
/tmp/ccacL3pX.o:main.c:(.text+0x542): undefined reference to `_asteroid_freefloating_boom'
/tmp/ccacL3pX.o:main.c:(.text+0x676): undefined reference to `_freefloating_collection_boom'
/tmp/ccacL3pX.o:main.c:(.text+0x76d): undefined reference to `_SolarSystem_Combiner'
/tmp/ccacL3pX.o:main.c:(.text+0x819): undefined reference to `_Search_asteroid'
/tmp/ccacL3pX.o:main.c:(.text+0x8cf): undefined reference to `_get_asteroids'
/tmp/ccacL3pX.o:main.c:(.text+0x97b): undefined reference to `_type_planetary'
/tmp/ccacL3pX.o:main.c:(.text+0xa1d): undefined reference to `_type_ffcol'
/tmp/ccacL3pX.o:main.c:(.text+0xabf): undefined reference to `_type_StarSystem
/tmp/ccacL3pX.o:main.c:(.text+0xb01): undefined reference to `_type_universe'
/tmp/ccacL3pX.o:main.c:(.text+0xb2c): undefined reference to `_termination'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: /tmp/ccacL3pX.o: bad reloc address 0x11c in section `.rdata'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: final link failed: Invalid operation
collect2: ld returned 1 exit status
[/m]

(Wondering)

[m]gcc main.c stars.c[/m]
(Wasntme)
 
  • #7
I like Serena said:
[m]gcc main.c stars.c[/m]
(Wasntme)

Ahaa... Ok! Thank you very much! (Yes)
 

FAQ: Is it Possible? - Running a Program with Data Structures in Separate Files

Is it possible to run a program with data structures in separate files?

Yes, it is possible to run a program with data structures in separate files. In fact, it is a common practice in programming to separate different components of a program into different files for organization and modularity.

Why would someone want to use separate files for data structures in a program?

Separating data structures into different files allows for better organization and readability of code. It also allows for easier maintenance and updates, as changes can be made to a specific file without affecting the rest of the program.

Are there any disadvantages to using separate files for data structures?

One disadvantage is that it may add some complexity to the program, as different files need to be linked together. Additionally, it may be more time-consuming to write and debug a program with data structures in separate files.

Can data structures in separate files communicate with each other?

Yes, data structures in separate files can communicate with each other. This is typically done through function calls or by passing variables between files.

Are there any best practices for using separate files for data structures?

It is recommended to plan and organize the structure of your program before creating separate files for data structures. It is also important to use consistent naming conventions and to properly link the files together. Additionally, it is helpful to regularly test and debug the program to ensure proper communication between the separate files.

Similar threads

Replies
33
Views
2K
Replies
2
Views
2K
Replies
41
Views
4K
Replies
5
Views
1K
Replies
57
Views
4K
Back
Top