Re: basic function call an function under fstream

<[email protected]>
Newsgroups gmane.linux.debian.user
Message-ID <apAr3/[email protected]>
On Thu, Aug 27, 2026 at 11:56:51AM +0000, Jean-Christophe wrote:
> On Thu, 2026-08-27 at 07:22 +0200, [email protected] wrote:
> > On Wed, Aug 26, 2026 at 07:15:00PM +0000, Jean-Christophe wrote:
> > > hello,
> > > 
> > > i try to write an my_files.txt
> > > 
> > > and see the output on tty
> > 
> > As Dan Ritter said in this thread: you are calling fonction_deux:
> > 
> > > bool fonction_deux (){
> > > 	fstream my_file;
> > > 	my_file.open("my_file.txt", ios::out);
> > > 	if (!my_file) {
> > > 		cout << "File not created!";
> > 
> >         ...and here, you are writing to stdout (what you call "tt", I
> > think)
> > 
> > > 	}
> > > 	else {
> > > 		cout << "File created successfully!\n" << endl;
> > >  		cout << fonction_un() << endl;
> > > 
> > > 	my_file.close();
> > > 	}
> > > 	var_fonction_deux = true;
> > > 	return var_fonction_deux;
> > > }
> > 
> > and in all the other places too ("cout" writes to stdout).
> > If you want to write to your fstream my_file, which is
> > connected to "my_file.txt") you have to do something like
> > 
> >   my_file << "some text going to file" << endl;
> > 
> > (See https://www.w3schools.com/cpp/ref_fstream_fstream.asp for a more
> > complete example).
> 
> bool fonction_deux (){
> 	fstream MyFile("my_file.txt");
> 	if (!MyFile) {
> 		cout << "File not created!";
> 	}
> 	else {
> 		cout << "File created successfully!\n" << endl;
>  		 MyFile << fonction_un();
> 
> 		MyFile.close();
> 	}
> 	var_fonction_deux = true;
> 	return var_fonction_deux;
> }
> 
> I was inspired by the code, but I have the following compilation error:
> 
> $ g++ -std=c++23 test-fonction.cpp test-fonction.out 

Try: g++ -std=c++23 test-fonction.cpp -o test-fonction

(note the "-o" option and leaving out the ".out" suffix: by convention
binaries have no suffix, and the C++ compiler possibly tries to do
something different when seeing one, I don't know).

One more thing: what do you expect this line to do?

>  		 MyFile << fonction_un();

Cheers
-- 
t
signature.asc (application/pgp-signature, 195 B)
-----BEGIN PGP SIGNATURE-----

iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCapAr2QAKCRAFyCz1etHa
RsJ/AJ9UnIcKv/SSxUHxBYwS1ha3fD3ZcgCeN2fiYCy0PDFVIYbMJQqg6LnsAzM=
=xFhh
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.