Hi I have a program, say file1.c which included file1.h, the header file which had lots of #define macros and structure definitions used by file1.c
I have decided to split the source code into 3 separate files (for logical reasons)
file1.c
file2.c
file3.c
Each of these have a corresponding header file which contain only function prototypes:
file1.h (I renamed the earlier file1.h, which had all the #defines and structure defs. to, say, abc.h) file2.h
file3.h
So, Earlier everything was in file1.c. I removed a set of related functions and put them in file2.c, And another set of related functions and put them in file3.c
file1.c includes...
file2.c includes..
file3.c includes..
Now my question is:
1) What would be an appropriate name for abc.h ?? This has lots of #define macros and structure definitions which is used by all three source files (file1.c, file2.c and file3.c) and hence is #included by all 3 of them.
2) Suppose the original source code had LOTS of global variables. The variables are used by all 3 source files. So how do I deal with them ? Can I put all these global variables in a header file and include it in all the 3 source files ? Will that work ? Or should I leave the global variables in file1.c and then declare them as "extern" in file2.c and file3.c ? (keeping in mind that there are LOTS of them).
3) Can header files contain variable declarations/definitions ???
What happens if I have a header file which has:
int a = 5
and I include this file in a some .c file and use 'a' and change its value ?
1. Make the header file names have some relation to their contents.
2. Dont put global definitions in headers, only put "extern" references to them, I recommend don't put externs in source files.
eg have extern int myFoo,myBar;
3. Only define actual globals once in a source file
Think of it this way....
* A source file is a private implementation file.
* A header file describes the public interface to that implementation.
If you change a global variable, say from an int to a long, then you only have to change it in the specific source it's declared, and the header for that same file, not in a number of other source files.
2. Dont put global definitions in headers, only put "extern" references to them, I recommend don't put externs in source files.
Sorry porter, I did not understand.. should I put global variables in a header file or in a source file ?
Quote:
Originally Posted by porter
If you change a global variable, say from an int to a long, then you only have to change it in the specific source it's declared, and the header for that same file, not in a number of other source files.
So, say in file1.c I have declared a global variable: int x
Then in file2.c I have declared it as extern: extern int x
Now if I change int x to long x, that means I have to change it in multiple files right? file1.c and file2.c
I like using definitions in header's and sources to do this also, like so:
file.h
The top defintion (_FILE_H) prevents recursive includes of the header, common practice.
The next will define all the globals as extern unless _FILE_C is defined. Of course, in file.c you'd define that before including file.h as follows:
file.c
This way I know that one and only one C file (the one that defined _FILE_C, which should be file.c) contains the definitions of all the globals.
Meh...just my personal style that I picked up...sometimes I'll do this too:
file.h
This is probably arguable whether or not it is good, because it tells the "user" including the header what the initial value of the variable is, which you may want to hide.
Hi all,
Newbie here. First of all, sorry if I made any mistakes while posting this question in terms of rules. Correct me if I am wrong. :b:
I have a .dat file whose name is in the format of 20170311_abc_xyz.dat. The file consists of records whose first column consists of multiple dates in... (2 Replies)
Hi Friends ,
Please guide me with the code to extract multiple files from one file .
The File Looks like ( Suppose a file has 2 tables list ,column length may vary )
H..- > File Header....
H....- >Table 1 Header....
D....- > Table 1 Data....
T....- >Table 1 Trailer....
H..-> Table 2... (1 Reply)
Hi,
I have a space delimited text file with multiple columns 102 columns. I want to break it up into 100 files labelled 1.txt through 100.txt (n.txt). Each text file will contain the first two columns and in addition the nth column (that corresponds to n.txt). The third file will contain the... (1 Reply)
Hello,
I am new to UNIX etc and am trying to split a large number of files, all with the extension .fas in the same folder, into smaller files wherever a string of 5ns occurs.
So this file:
>File1.fas
nnnnnaaaaaattgattttctcagtatcgacgaatatggcgcagaaagttgaataa ... (1 Reply)
Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine.
So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files.
My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
To split the files
Hi,
I'm having a xml file with multiple xml header. so i want to split the file into multiple files.
Test.xml
---------
<?xml version="UTF_8">
<emp: ....>
<name>a</name>
<age>10</age>
</emp>
<?xml version="UTF_8">
<emp: ....>
<name>b</name>
<age>10</age>... (11 Replies)
I have a file that logs multiple sessions. What I would like to do is split this file inclusive of the lines that include "starting session" and "shutting down" and ignore the data before and after the beginning of the first session and the end of the last session. The output files can be called... (2 Replies)
hi all
i have a some huge html files (500MB to 1GB). Each file has multiple
<html></html> tags
<html>
.................
....................
....................
</html>
<html>
.................
....................
....................
</html>
<html>
.................... (5 Replies)
Hi,
I needs to split *.txt files from single directory depends on the some mutltiple input values. i have wrote the code like below
for file in *.txt
do
grep -i -h "value1|value2" $file > $file;
done.
My requirment is more input values needs to be given in grep; let us say 50... (3 Replies)