I have set of data point (x_i,y_i) from a text file. How can I write a C-program that reads those data, send the data to Sage, computes the Pearson correlation and send the result back to C. I have no idea how can I use C to give input to some Linux-program and read its output to a variable.
OK, let me get it straight: you are working on a C program, and within that program you need to calculate Pearson's correlation coefficient. You'd like to pass these calculations to Sage rather than code them yourself.
Now, I don't know Sage, but I guess it is possible to run it from command line. Assuming that you can prepare an input file or files for Sage, and run the calculations in Sage producing an output, I would then use the system from stdlib.h (man 3 system) to call the command line. Here is the outline of the steps in your C program:
prepare temporary files for Sage's input and output
construct the command line of Sage
use system() to run the command line
parse the temporary file name where Sage stored it's output.
That said, I would not do it using Sage. Pearson correlation coefficient is easy enough to implement in C, and if you do it, your program will not depend on the whole Sage installation.
Related
I got 5 files generated by a fortran code like this
longP=8
OPEN(unit=20,FILE="GMt_2.dat",ACTION="write",ACCESS='Direct',RECL=longP)
count1=1
do J=K,fact
READ(10,*)XA,XB,YA,YB,ZA,ZB,rho
call Grv('f',Nx,Ny,dimg,Dx,Dy,XO,YO,XA,XB,YA,YB,ZA,ZB,rho,G,elev,Svec)
do I=1,dimg
WRITE(UNIT=20,rec=count1)Svec(I)
count1=count1+1
end do
WRITE(*,*)J
end do
dim(2)=J-1
fact=fact+fact1
call flush(20)
CLOSE(20)
which returned with an unreadable file format, my professor said "its binary, machine code" My goal here is to concatenate the information in those 5 files in one array to perform some processing. how can I achieve this?.
The code you show writes the data using unformatted I/O and direct access. You'll need to read it using unformatted I/O as well. You could use direct access or, and this would be my recommendation, stream access (ACCESS='STREAM' in the OPEN statement.) Open each file in sequence, read the data and then write it using the same mechanism to your single file. Your question is ambiguous enough to not allow a more detailed response.
so basically I have a C program which does a lot of computation based on an input .txt file and outputs a value. I want to run it 100 times and then work out the average, obviously this would be tedious to do individually.
So I've tried to research a bit about scripting etc and I've found things like this:
https://answers.yahoo.com/question/?qid=20091206100348AAaJPP8
Am I supposed to just do this in my command prompt? (I'm on Windows btw)
Thanks for any help :)
You're on Windows, so you can use a DOS batch script (.bat) to run your program N times using a loop (or N separate commands if that's easier for you). Use the >> symbol at the end of the command to append the output to a file. See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true for more info on this, and search google for dos bat file for help on getting started with writing batch scripts.
Try this:
Have the program append the data into the text or csv file concerned and then write another program where you can run the program for a defined number of times. Use the function system(). It accepts a string as argument and executes it in the CUI.
Hope that helps.
I am not an experienced programmer but I recently have to check C code translated from Matlab, in order to ensure that with the same data set, C and Matlab are giving the identical results.
Watches are what I am able to use until now but that requires me to sit in front of the screen and watch the values of a variable change at each loop. Is there a way to trace and log the values of a variable into a .txt file so that I can plot it later to compare with the results from Matlab?
I have already tried to write the values into some .txt file with 'fwrite' but there is not the ideal solution as I have to do that in the C code that I want to check. Embarrassed I am currently reading about tracepoint of gdb and will try to use that but I am still unsure if that is what I need. If you have some tips about what else I try.
I´m using Windows 7 and work with CodeBlocks.
Thanks!
Christina
You can set a breakpoint command to run when your watchpoint hits; have it log and execute a continue command to let the program keep running.
I'm working on a project, and I made a C program that reads the date, time, and wave height from a .txt file stored on my computer, converts the date and time to GPS time for use at a scientific research institution, and outputs GPS time and wave height to the screen. However, the text file that I am working with is actually stored at http://www.ndbc.noaa.gov/data/realtime2/SPLL1.txt . Is there any way that I could have my C program open the text file from the web address rather than from my local hard drive?
FYI: to access the file on my computer I used fopen and to interact with the data contained I used a combination of fgets and fscanf.
It is much more involved to get a web-resource than to read a file from disk, but you can absolutely do it, for example by using a library such as libcurl.
An alternative strategy is to make components and tie them together with bash or other scripting. Your C program could for example read from standard input, and you could make a bash script something like this:
curl http://www.ndbc.noaa.gov/data/realtime2/SPLL1.txt | ./the_program
This way, you could keep your core C program simpler.
How to control programs written in C from asterisk AGI-application in features.conf?
I want to control my program by pushing keys on the telephone (dtmf-tones). I was reading about using pipes for passing on standard I/O. My first idea was:
$ Asterisk | c_program
then have standard output in AGI-script by printf()
The second idea was to use:
$ printf parameter_a >> file
to write to file and then let my c-program read the file and evaluate the contents as parameter.
Has anyone tried out or has experience with similar tasks/problems?
there are no way do like u show.
posible solutions:
1) Run your program with arguments using system(). you can do that with or without AGI.
2) Use linux pipe(special files)
3) Start your program as AGI/EAGI script, your program must work acordinly, see CAGI
4) Use database table(task) and asterisk realtime for put record in it.
most correct is 3) if ur program is not demon and 2,4 if ur program is demon.
also you can use in dialplan