How to use VTK with C language? - c

I've already installed CMake, but I haven't undertood how to use The visualizer Toolkit!I have done a .dat file with C and I want to make a .vtk file.

A .vtk structured grid looks like this :
# vtk DataFile Version 2.0
Really cool data
ASCII
DATASET STRUCTURED_GRID
DIMENSIONS 2 2 1
POINTS 4 float
0 0 0
0 0 2
0 1 0
0 1 1
POINT_DATA 4
SCALARS volume_scalars char 1
LOOKUP_TABLE default
1 2 3 4
Using the toolkit is not absolutely mandatory to write such a file. You can try to use it if you are working with c++. http://www.vtk.org/Wiki/VTK/Examples/Cxx/StructuredGrid/StructuredGrid
Or you can use fopen(), fprintf() and fclose() in stdio.h.
Combine things like :
#include <stdio.h>
...
FILE* f = fopen("bla.vtk","w");
if(f==NULL){printf("file vtk, failed to open\n");}
fprintf(f, "# vtk DataFile Version 2.0");
...
fprintf(f,"%f %f %f\n",x,y,z);
...
fclose(f);
Good luck !

Related

How can I read a string from PDF file in C?

I want to create a program that process the edit distance from two file, My code works with strings read from a txt file. But now I want to read strings from PDF DOC exc. How can I read strings from this files? I tryed with the func fread but it not works.
This is the code that i wrote:
void method () {
FILE *file;
char *str;
if ((file = fopen("C:/Users/latin/Desktop/prova.pdf", "rb")) == NULL) {
printf("Error!\n");
}
fread(&str,18,1,file);
printf("%s",str);
}
prova.pdf is a PDF file that contains this string : ciaoCiao merendina .
It is possible to do this in plain C. Adobe did it. Artifex did it. Others have done it. But as commented, it is a ton of work. But I can outline the steps to give you a feel for what's involved.
First you could read the "Magic Number" at the start and check that it is actually a PDF. It should start with %PDF- followed by a version number. But apparently many PDF producers don't conform to this requirement.
Next, you need to skip to the very end of the file and read backwards, looking for something like:
startxref
1581
%%EOF
That number is the byte-offset of the start of the X-Reference table which lists the binary offsets of all the "objects" in the file. An object can be a Page or a Font or a Content Stream or many other things.
Looking at the X-Reference table, you'll see something like this:
xref
0 4
0000000000 65535 f
0000000010 00000 n
0000000063 00000 n
0000000127 00000 n
0000000234 00000 n
trailer
<<
/Root 1 0 R
/Size 4
>>
The line /Root 1 0 R tells you which object is the root of the document tree. You'll need to examine this object to find the top-level Pages object which looks like this:
2 0 obj
<< /Kids [ 3 0 R ]
/Type /Pages
/Count 1
>>
endobj
The Kids element here contains a reference to the first Page object which looks like this:
3 0 obj
<< /Contents [ 4 0 R ]
/MediaBox [ 0.0 0.0 612.0 792.0 ]
/Type /Page
/Parent 2 0 R
>>
endobj
Then you'll need to find the Contents object referenced here. A Content stream, if it's not encrypted or compressed, will show you the drawing commands and text commands being drawn to the page.
5 0 obj
<<
/Length 15660
>>
stream
BT F1 10.0 Tf 30.0 750.0 Td (<< ) Tj ET BT F1 10.0 Tf 50.0 738.0 Td (/)
Tj ET BT F1 10.0 Tf 56.0586 738.0 Td (astring) Tj ET BT F1 10.0 Tf 86.7852
738.0 Td ( ) Tj ET BT F1 10.0 Tf 89.2852 738.0 Td (\() Tj ET BT F1 10.0 Tf
92.6133 738.0 Td (this string data) Tj ET
[...lots more commands follow...]
endstream
endobj
Text commands will always be bracketed by BT ... ET. In here, you can finally see the strings wrapped in parens. But you'll have to pay attention to the coordinates 30.0 750.0 Td of each string to figure out which ones are part of the same logical line.
If the PDF was created from a word processor, it is likely to contain text in this form but with lots of caveats. It might have re-encoded fonts and the text strings will no longer represent ASCII characters but just positions in the font's encoding vector. If the PDF was created from a scanned document, it may just contain images of the pages with no text content at all unless it has gone through a conversion involving OCR.

Writing my output into two columns in a text file .txt in C

I've looked around but couldn't find a satisfying solution... Basically I made a function that calculates the probability distribution of x number of loss in a portfolio of n credits... And I am trying to write the output in a text file into two columns where the first column would be the X (number of defaults) and second column would be the P(density function of each loss).. something like this:
X P
1 0.005
2 0.003
3 0.005
4 0.005
5 0.005
etc.
I've looked around and people suggested using negative- sign in front of my %d and %f when using fprintf but no luck....
Here's a sample of my code and the output it gives me...
Code:
for(i=0;i<d+1;i++)
{
Densite= gsl_ran_binomial_pdf(i,p,d);
fprintf(pF,"%-5d %-20f .\n",i, Densite);
}
Output:
0 0.005921 .
1 0.031161 .
2 0.081182 .
3 0.139576 .
4 0.178143 .
5 0.180018 .
6 0.150015 .
7 0.106026 .
8 0.064871 .
9 0.034901 .
10 0.016716 .
How to remedy?
Thanks in advance! (complete noob that started coding in C like two days ago..)
Did you run the executable program on Windows or Linux? If Window please use \r\n for new line.

Open file with formatted variable name in Julia

I have a list of files numbered gll_01.tab, gll_02.tab, ...., gll_20.tab in a subdirectory of my parent directory. These files are tabular data files.
I want to open/read files with user-specified input.
I can do:
a = 3
open("directory/gll_0$a.tab")
But using this approach, I would have to define two separate variable names for (01 to 09) and for (10 to 18). How can I use variables or strings with name 02, 03, ..., etc?
In python, I can have an equivalent command:
a = 4
g = '{:02d}'.format(a)
f = open('directory/gll_%s.tab' %g)
Is there an equivalent string formatting command in Julia?
A simple answer in this case would be to use lpad:
a = 3
open("directory/gll_$(lpad(a,2,"0")).tab")
If you need more fancy formatting you can use e.g. https://github.com/JuliaIO/Formatting.jl, in this case this would be:
using Formatting
a = 3
open("directory/gll_$(fmt("0>2", a)).tab")
Another option is to use #sprintf, docs are here. With that you can use %02d as a formatting option that would pad a digit d to length 2 with 0s preceding it:
julia> using Printf # this is in the standard library
julia> #sprintf("directory/gll_%02d.tab", 1)
"directory/gll_01.tab"
You can use this in your open statements too. Here they are in action:
julia> for i in 5:10
println("$i file is: $(#sprintf("directory/gll_%02d.tab",i))")
end
5 file is: directory/gll_05.tab
6 file is: directory/gll_06.tab
7 file is: directory/gll_07.tab
8 file is: directory/gll_08.tab
9 file is: directory/gll_09.tab
10 file is: directory/gll_10.tab

Python: How to compare 2 file text?

I have 1 large file text A and 1 small file text B. Now, I want compare file B and file A to see what is unique in file B.
For example:
File A:
1
2
3
4
5
File B
2
3
6
7
==> ouput
6
7
What is best solution for this ? I searched some thread in the website but i think my question is different because my file is large. Thanks
The below is my code but it doesn't work
with open('C:/unique.txt', 'wb') as out:
for line in open ('C:/B.txt'):
for line1 in open ( 'C:/A.txt' ):
if line != line1:
out.write(line)
I tried this and it worked for me. Hope this helps
with open('C:/unique.txt,'r+') as text:
with open('C:/unique2.txt','r+') as text2:
for read in text.readlines():
for read2 in text2.readlines():
if read2 not in read:
print(read2)

How do I read and parse a .dat file in C?

I have a file called resistors.dat and I need to get my program to read and parse the values from the file into my program.
How would I read a file like this in C?
Read from the le resistors.dat (supplied on Blackboard) similarly to what you have done in Problem 2 of Lab 12. Each line in resistors.dat now represents one row: Ria, Rib and Ric (i = 1; 2; : : : ; n) of the circuit. Expand Problem 2 of Lab 12 to calculate the total resistance of the circuit. Hint: The total resistance is given by 1 R = 1 R1 + 1 R2 + 1 R3 + : : : + 1 Rn where Ri is the sum of resistances in one input row. In a loop, compute the sum of the inverse resistances 1=Ri. After the input has finished, compute the inverse of this sum to obtain the final result.
This is the content of resistors.dat:
64.35 35.52 85.37
90.43 12.99 80.40
98.37 32.63 78.42
3.82 82.74 52.61
3.75 72.47 49.05
96.73 16.07 23.46
48.15 36.62 83.64
51.96 27.19 22.38
4.18 46.07 91.21
96.94 8.17 50.45
0
There are several ways to accomplish this. I expect that your Resistors.dat file looks something like this:
r=1
r=20
r=22
r=2
I suggest you do something like this:
fopen to open the file, fgets in a while loop until the end of the file (!EOF), to read each line. Then use sscanf to parse each line.

Resources