extra file

closed account (4w7X92yv)
Hello

On C++ is there someway to make your program:
make a new file named coördinates
everytime the program starts it has to save the sentence on the file: new track.
put data into that file like x: 50 y: 60 z: 90 (enter)
and keep saving the coördinates on that file for as long as the program runs.

Best Regards
Joriek
Yes.

If you want to create a new file, you can truncate the existing file or create a file with a new name.

If you want to truncate the file you can write:
1
2
3
#include <fstream>
//...
std::ofstream log("logfile.txt", std::ios::trunc);


If you want a new name, you could use the date/time to make up a name:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <sstream>
#include <iomanip>
#include <time.h>
//...

// get time
time_t now = time(0);
struct tm *t = localtime(&now);

// make up a string
std::ostringstream os;
os << std::setfill('0');
os << "logfile" << '-'
    << std::setw(2) << (t->tm_year - 100)
    << std::setw(2) << (t->tm_mon + 1)
    << std::setw(2) << (t->tm_mday + 1) << '-'
    << std::setw(2) << (t->tm_hour)
    << std::setw(2) << (t->tm_min) << ".txt";
std::string filename = os.str();

// open the file
std::ofstream log(filename.c_str());


Then you can write coordinates throught the program:
 
log << "x:" << coordx << " y:" << coordy << std::endl;
Last edited on
closed account (4w7X92yv)
Thanks!!
The only problem is that it only saves up 1 pair of coordx-y
it has to save an unlimited amount.

what should i change?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    do{
          
    system ("CLS");
 
    std::string coordx;
    std::string coordy;
    
    cin >> coordx;
    cin >> coordy;
          
    std::ofstream log("test.txt", std::ios::trunc);
    log << "x:" << coordx << " y:" << coordy << std::endl;
    
    }while (1);
    
    system("pause");
    return 0;
}
Move the file object out of the loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ofstream log("test.txt", std::ios::trunc);

    int coordx, coordy;
    std::cin >> coordx >> coordy;

    while (coordx != -1)
    {
        log << "x:" << coordx << " y:" << coordy << std::endl;
        std::cin >> coordx >> coordy;
    }

    return 0;
}
closed account (4w7X92yv)
Thank you so much!! it works
Last edited on
You do have to open the file, see my original example. If you don't instantiate log, you can't write to it, right?

The ostringsteam is used to format the filename. It's not a log sync.
closed account (4w7X92yv)
Another problem:

I let C++ run to my 3000 lines long program (that works) and when it reaches the

 
log << "x:" << x1 << " y:" << y1 << " z:" <<  z1 <<std::endl;


and it gives this error:
" Invalid operand of types '<unknown type>' and 'const char [3]' to binary 'operator<<' "

the variables x1, y1, z1 are double

what do i do wrong?
Dunno what that is. Can you reproduce it in a small program and post that please.
closed account (4w7X92yv)

first there is serial communication

the data recieved is like this: 10 4a 11 22 33 44 55 66 77 88 10 03
if the data has correct id 4a then it should its ready for the next step
if not the data has to be ingnored

if 4a then it gets devided in several pieces (x = 11223344 and y = 55667788 ...) (the 10 03 is stop bit)

then the x and y go trought a HEX to BIN converted and then they get converted whit IEEE754 BIN32 to DEC

if you want ill send you an email whit my entire program

serial communication

1
2
3
4
5
std::string id_strnormal = "4a";    
std::string start_str = code.substr(0, 2);
std::string id_str = code.substr(2, 2);
if (id_str == id_strnormal)
//id is 4a and correct 


HEX to BIN converter

BIN to DEC converter with IEEE754

//the DEC coördinates are outputted and should be putted in the .txt file
Last edited on
closed account (4w7X92yv)
nvm fixed it
thanks for all the help you gave me kbw
Topic archived. No new replies allowed.