|
[Sponsors] |
read a .dat file with fseek and fscanf problem |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
September 23, 2016, 02:46 |
read a .dat file with fseek and fscanf problem
|
#1 |
Member
Federica Biano
Join Date: Feb 2016
Location: Genova, Italy
Posts: 39
Rep Power: 10 |
Hello!
I need your precious help to solve my problem. I've never programmed in C++ and I tried recently to modify a library in sixDoFRigidBodyMotion. When I compiled it no errors came out, but when I run the code results are completely wrong. Let me explain: I need to read for each timestep the forces.dat file and in particular the last line of it and take only two force's components. I used the functions fseek to go to the end of file and return at the beginning of the last line, and the function fscanf to read the line and save the two force's components into two variables. This is my code Code:
FILE *forze; forze = fopen("postProcessing/forces/1/forces.dat", "r"); float timestep; float fxp; float fyp; float fzp; float fxv; float fyv; float fzv; float fxpo; float fypo; float fzpo; float mxp; float myp; float mzp; float mxv; float myv; float mzv; float mxpo; float mypo; float mzpo; fseek(forze,-271,SEEK_END); fscanf(forze, "%f ((%le %f %f) (%le %f %f) (%f %f %f)) ((%f %f %f) (%f %f %f) (%f %f %f))", ×tep, &fxp, &fyp, &fzp, &fxv, &fyv, &fzv, &fxpo, &fypo, &fzpo, &mxp, &myp, &mzp, &mxv, &myv, &mzv, &mxpo, &mypo, &mzpo); fclose(forze); forzaxPrevIter = fxp + fxv + fxpo; alfaPrevIter = (alfaini_ * 3.1415 / 180) + asin(motion.orientation()[3]); alfa = alfaPrevIter; forzax = forzaxPrevIter; } restraintPosition = motion.transform(refAttachmentPt_); vector v = motion.velocity(restraintPosition); restraintForce = vector((-1)*forzax,0,(-1)*forzax*tan(alfa)); restraintMoment = vector::zero; What can I do to solve it? I tried also to make a C++ program and it does what I need, but when I put the code into openfoam it doesn't anymore. Any help would be very appreciated! Federica |
|
September 27, 2016, 10:56 |
solved
|
#2 |
Member
Federica Biano
Join Date: Feb 2016
Location: Genova, Italy
Posts: 39
Rep Power: 10 |
If you are interested in the correct code, there you are:
Code:
FILE *forze; forze = fopen("postProcessing/forces/1/forces.dat", "r"); float timestep; float fxp; float fyp; float fzp; float fxv; float fyv; float fzv; float fxpo; float fypo; float fzpo; float mxp; float myp; float mzp; float mxv; float myv; float mzv; float mxpo; float mypo; float mzpo; char c; fseek(forze,-285,SEEK_END); while(c != '\n') { c=fgetc(forze); } fscanf(forze, "%f ((%f %f %f) (%f %f %f) (%f %f %f)) ((%f %f %f) (%f %f %f) (%f %f %f))", ×tep, &fxp, &fyp, &fzp, &fxv, &fyv, &fzv, &fxpo, &fypo, &fzpo, &mxp, &myp, &mzp, &mxv, &myv, &mzv, &mxpo, &mypo, &mzpo); fclose(forze); forzaxPrevIter = fxp + fxv + fxpo; alfaPrevIter = (alfaini_ * 3.1415 / 180) + asin(motion.orientation()[3]); alfa = alfaPrevIter; forzax = forzaxPrevIter; |
|
February 20, 2017, 14:35 |
|
#3 |
Member
Emre
Join Date: Nov 2015
Location: Izmir, Turkey
Posts: 97
Rep Power: 10 |
Hello,
Did you solve the problem? It seems interesting. |
|
|
|