Hello,

I've a c++ project in linux and I want to create a text file and share it between more than one source file so that I can write into it instead of using only printf.
Can anyone help me please?

Thank you in advance,

Recommended Answers

All 5 Replies

So what's the problem?

I want to create the text file one, and after that use it for the whole project. Suppose I have 30 c++ source files (with 30 headers files), I want to write into the file from the file1, file2, etc.

I want to create the text file ONCE, and after that use it for the whole project. Suppose I have 30 c++ source files (with 30 headers files), I want to write into the file from the file1, file2, etc.

well you could do this

ofstream fout("TheOneFile.txt", ios::app);
fout << something;
fout.close();

this way everytime you need to ouput something into the file you open it, store the information, then close the file. this way you can have every file of source code store information into the file without having to worry that another peice of code has access to that file. to be double sure always include this before trying to store to a file

if (!fout)
errorCondition = true;

you really can do anything if the oppening fails i just put this in as an example.

Thanks for your answer.

What I have done is that I included the definition in the a generic header file, opened and closed the text file in the main function and after that used the text file in the other c++ sources.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.