Reading a part of the program (which is not a function) from another file.

Hi everyone,

I have a program like the following:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include whatever_should_be_included

int main()
{

whatever-1

whatever-2

whatever-3

.
.
.


}




All those whatevers are codes in the program, including if conditions, loops and really whatever. I want those "whatever"s to be saved in files, let's say plain text files or whatever extension is necessary, let's say "exten" be the extensions, then I want to save them as:

chunk-1.exten, chunk-2.exten, .... etc files that will contain: whatever-1, whatever-2,... etc chunks of lines of codes and my program now should look like:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include whatever_should_be_included
#include those_chunks_maybe

int main(){


chunk-1.exten;  //or whatever syntax necessary

chunk2.exten;

chunk-3.exten;

.
.
.


}




I am a beginner in C++ and in programming in general so please go easy on me, a step by step clear answer with a bit of explanation will be really helpful.
Last edited on
nolyc wrote:
You're trying to do X, and you thought of solution Y. So you're asking about solution Y, without even mentioning X. The problem is, there might be a better solution, but we can't know that unless
you describe what X is.



#include is equivalent to copy paste the content of the file.
Suppose my program (X) is:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> //etc

int main()
{


        definitions of a lot of variables here   //call this chunk variable-chunk

     
       if(condition 1){ do a lot of things 1};    //call this chunk condition chunk
  
       for(int i blah blah blah){ do the looping and also a lot of stuff inside... }  //call this the loop chunk

    here goes a few paragraphs of other codes...   //call this misc-chunk


}



I need those chunks to be cut and pasted into files named variable-chunk.extension, condition-chunk.extension, ... etc. and I want those files to be put at those exact positions by using include or something like that, so that they act like as if they paste their contents at those positions in the main program. So the final code should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>  //etc
#include <those created chunk files maybe ?>

int main()
{

    variable-chunk.extension   must be called here

    condition-chunk.extension  should be called here

   etc

}



But I do not want those chunk files to be function like because then I have to pass the variables (defined inside variable-chunk.extension file) as arguments to all those files, I don't want that. I just want those chunk files to act as fragments of my program, plain dull code-texts pasted from those chunk-files and read by the main program.. no issues with variables etc, the variables defined inside the variable-chunk.extension should do the job once and for all.

Can this be done ?



PS: Regarding your comment:

"#include is equivalent to copy paste the content of the file.", doesn't seem like so. If I cut and paste the fragments of my program in some files and simply include them wherever necessary, the compiler says that the variables used inside those files are not defined in the current scope, but I surely defined the variables in the beginning of my main program.
Last edited on
> Suppose my program (X)
your program is Y, the "solution".
I want to know what are you trying to solve, why do you think it is a good idea to have this copy-paste hell.

> If I cut and paste the fragments of my program in some files and simply
> include them wherever necessary, the compiler says that the variables used
> inside those files are not defined in the current scope
if you've got an issue with your code, post your code.
to detect the problem we need to see what you are doing wrong.
1
2
3
4
5
6
7
//foo.cpp
#include <iostream>
int main(){
   std::cout <<
#include "asdf.h"
   << '\n';
}
1
2
//asdf.h
"asdf"



> But I do not want those chunk files to be function like because then I have
> to pass the variables (defined inside variable-chunk.extension file) as
> arguments to all those files, I don't want that.
¿why is such a problem to pass an argument?
¿why do you even want the chunk files? (just write everything in main)
>if you've got an issue with your code, post your code.

Actually there is no problem with my code, it works perfectly if I write everything in the main ! I just wanted to see if this chunking is possible or not. Anyway thanks a lot for replying. I will try your solution.
Topic archived. No new replies allowed.