FredRaspb
Posts: 19
Joined: Wed Mar 16, 2016 8:05 am

Can't write .txt file

Fri Apr 01, 2016 9:50 am

Hello guys,

I am doing a program that picks up 2 images and compares them with each other, i am doing this on a Windows Computer with Visual Studio, then i take the .cpp file bring it to raspberry, change the directories, apply 'cmake' (with CMakeLists.txt) and 'make' on the .cpp directory to create the executable.

This program also gives me the number of different pixels that it encounters in the int variable 'c'. I want to write that 'c' variable in a .txt document each time i compare images, i used this code in Windows and it works:
codigo w.png
codigo w.png (4.88 KiB) Viewed 1288 times
But when i do all the process to the Raspberry it shows this error which i don't understand:
erro app.png
erro app.png (58.77 KiB) Viewed 1288 times
Even if i take the 'ios:app' part (which i need) i still can't make it work:
erro.png
erro.png (58.2 KiB) Viewed 1288 times
Anyone can help me fix this problem and make it write the .txt file like it does on Windows with VS.

Regards

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Can't write .txt file

Fri Apr 01, 2016 11:20 am

Code: Select all

[email protected] ~/GIT/src $ cat ofstream.cc 
#include <fstream>

int main(int argc, char ** argv)
{
  (void)argc;(void)argv;

  std::string fname("/tmp/foo.txt");
  std::ofstream myfile;
  myfile.open(fname.c_str());
  myfile << "WooT" << (int)42 << std::endl;
  myfile.close();
  return 0;
}

[email protected] ~/GIT/src $ g++ -g -Wall -Werror -Wextra ofstream.cc -o ofstream
[email protected] ~/GIT/src $ ./ofstream 
[email protected] ~/GIT/src $ cat /tmp/foo.txt 
WooT42
[email protected] ~/GIT/src $ 
use ".c_str()" to convert from "std::string" to "char *"

P.S. code blocks good, images bad.

swampdog
Posts: 238
Joined: Fri Dec 04, 2015 11:22 am

Re: Can't write .txt file

Fri Apr 01, 2016 9:37 pm

sprinkmeier is most likely correct.

Personally I prefer to perform such manipulations in memory. eg:

Code: Select all

#include <iostream>
#include <sstream>
#include <fstream>

int
main(void)
{std::ostringstream	x;

 x << "Hello C++ World" << std::endl;

 {std::ofstream		f	("/tmp/z");
  f << x.str();
  if (f.bad()) std::cerr << "oops!\n" ; else std::cout << "oki\n";
 }

 return 0;
}
..then dump the result into a file.
$ g++ -o c c.cc && ./c && cat /tmp/z

[I've had to post this 3 times so will continue in another post]

swampdog
Posts: 238
Joined: Fri Dec 04, 2015 11:22 am

Re: Can't write .txt file

Fri Apr 01, 2016 10:06 pm

[must have been a glitch in my browser]

Unix/Linux has two output streams. There's "stdout" and "stderr" (the input stream is "stdin"). Those are "C" terms. In C++ terminology they are "std::cout", "std::cerr" and "std::cin". Same thing on the command line really.

It depends on the app but the general idea is error messages go to cerr/stderr. Your command line will receive both but if you try to capture it into a file "stderr" wont be there because you'll have done something (put a deliberate error into your code) like..

$g++ -o c c.cc >z
$ cat z
(nothing)

What you need to do is redirect stderr into 'z' as well as stdout..
$ g++ -o c c.cc >z 2>&1
$ cat z
(has the gcc errors)

..but now *you* can't see them as they occur. Do this..
g++ -o c c.cc 2>&1 | tee /tmp/z

All the output from both stdout/stderr get piped into (technically stdin of) 'tee' which both displays output as it gets it and mirrors it into your /tmp/z file.

Fwiw, in the command shell "0" is stdin, "1" is stdout and "2" is stderr so the "2>&1" above means shove all the stdout/stderr output to stdout.

Anyways, now when you get an error you ought to be able to get it into a file. No need for images! :-)

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Can't write .txt file

Fri Apr 01, 2016 10:15 pm

swampdog wrote: Personally I prefer to perform such manipulations in memory. eg:
For small amount of output like this the buffered I/O would probably be just as efficient.
std::ostringstream is definetely worth using when assembling long strings.

Code: Select all

for(...) finalString += someString;
is a lot slower than

Code: Select all

for(...) os <<someString;
finalString = os.str();
As for capturing screen output, use the script utility:

Code: Select all

$ script
Script started, file is typescript
$ date # or whatever command you want to capture)
Saturday 2 April  08:40:58 ACDT 2016
$ exit
exit
Script done, file is typescript
$ cat typescript 
Script started on Sat 02 Apr 2016 08:40:48 ACDT
$ date # or whatever command you want to capture)
Saturday 2 April  08:40:58 ACDT 2016
$ exit
exit

Script done on Sat 02 Apr 2016 08:41:00 ACDT
$
stdout is buffered while stderr is not, so the redirect-to-file trick sometimes mixes things up.

FredRaspb
Posts: 19
Joined: Wed Mar 16, 2016 8:05 am

Re: Can't write .txt file

Mon Apr 04, 2016 1:27 pm

Hello guys,

Thank you all for the answers, i've successfully open and wrote on the file with the "fname.c_str()", even the ios::app worked.

Regards

Return to “C/C++”