Getting column data from file stream in C - c

I have a file which is written with a column of data (For example, "250\n 249\n...". Actually, there are 250 rows data with at most 15 digits in a row). I wish to get data from the different files and average them. However, I have no idea how could I get such a large amount of data with single column. I tried the following:
char str[80];\newline
FILE * abc;
abc=fopen("bromo.dat", "r");
fgets(str, 80, msd);
atof(str);
What I got was only the data from the first row. How could get the rest of the data?

You can use strtok to split the number in each line by space character. Then use the atof funcition as you used in your code to convert string to float number.
You should use 2D array to store all numbers in the file, and use another array to store the number in each line:
float number[250][15]; // maximum 250 line with at most 15 digits in each line
int number_each_line[250] = {0}; // the number of digits in each line
The complete program for test:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char str[80];
float number[250][15];
int number_each_line[250] = {0};
FILE * fp;
fp =fopen("bromo.dat", "r");
if (!fp) {
return -1;
}
int i = 0;
while(fgets(str, 80, fp) && i < 250) {
int j = 0;
char *token = strtok(str, " ");
while(token != NULL) {
number[i][j] = atof(token);
j++;
token = strtok(NULL, " ");
}
number_each_line[i] = j;
i++;
}
// print first 10 lines in file
for(int k = 0; k < 10; k++) {
for(int j = 0; j < number_each_line[k]; j++) {
printf("%f ", number[k][j]);
}
printf("\n");
}
fclose(fp);
}
The output of test:
bromo.dat:
1.2 1 4 5
2.1 2 6 7 8
3.5 3 2 3 5.3
2.1 4 6 7 8
2.1 5 6 7 8
2.1 6 6 7 8
2.1 8 6 7 8
2.1 9 6 7 8
2.1 10 6 7 8
./test
1.200000 1.000000 4.000000 5.000000
2.100000 2.000000 6.000000 7.000000 8.000000
3.500000 3.000000 2.000000 3.000000 5.300000
2.100000 4.000000 6.000000 7.000000 8.000000
2.100000 5.000000 6.000000 7.000000 8.000000
2.100000 6.000000 6.000000 7.000000 8.000000
2.100000 8.000000 6.000000 7.000000 8.000000
2.100000 9.000000 6.000000 7.000000 8.000000
2.100000 10.000000 6.000000 7.000000 8.000000

Related

Two different outputs of the same array

I have a question about some odd behaviour with my program.
I have two arrays data and ind_array. Both arrays are initialized in main function. ind_array is filled with some values and data is filled with values using function loadData().
But output of the program depends on where I print values of data array. Before inputting values to ind_array or after.
Look at the first tree numbers of output.
Thanks in advance.
Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define FILE_NAME "DataValues.csv"
#define NUM_ROWS 40
#define NUM_COLUMS 2
#define COMA " ,"
void loadData(double (*data)[2]){
//double data[NUM_ROWS][NUM_COLUMS];
FILE* data_file = fopen(FILE_NAME, "r");
char line[NUM_ROWS];
int i = 0;
while(fgets(line, sizeof(line), data_file)){
char* tok = strtok(line, COMA);
int j = 0;
while(tok != NULL){
char *ptr;
data[i][j] = atof(tok); //const char to double
tok = strtok(NULL, COMA);
j++;
}
i++;
}
}
int main(){
double data[NUM_ROWS][NUM_COLUMS];
double ind_array[0][5];
loadData(data);
for(int j = 0; j < NUM_ROWS; j++){
printf(" %f\n", data[j][0]);
}
printf("\n");
ind_array[0][0] = 2;
ind_array[0][1] = 5;
ind_array[0][2] = 0;
ind_array[0][3] = 3;
ind_array[0][4] = 0;
for(int j = 0; j < NUM_ROWS; j++){
printf(" %f\n", data[j][0]);
}
return 0;
}
Output
1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000
9.000000 10.000000 11.000000 12.000000 13.000000 14.000000 15.000000
16.000000 17.000000 18.000000 19.000000 20.000000 21.000000 22.000000
23.000000 24.00000025.000000 26.000000 27.000000 28.000000 29.000000
30.000000 31.000000 32.000000 33.000000 34.000000 35.000000 36.000000
37.000000 38.000000 39.000000 40.000000
2.000000 0.000000 0.000000 4.000000 5.000000 6.000000 7.000000
8.000000 9.000000 10.000000 11.000000 12.000000 13.000000 14.000000
15.000000 16.000000 17.000000 18.000000 19.000000 20.000000 21.000000
22.000000 23.000000 24.000000 25.000000 26.000000 27.000000 28.000000
29.000000 30.000000 31.000000 32.000000 33.000000 34.000000 35.000000
36.000000 37.000000 38.000000 39.000000 40.000000
Well you are declaring a 0 X 5 array on this line:
double ind_array[0][5];
The total amount of cells in that array is 0 x 5 = 0. You are printing uninitialized memory which is undefined behaviour, switch the 0 for 1.

Reading .txt file and printing results

I'm trying to read the information I printed to a .txt file from a separate program, and display it in this new program. Although when I run the program, it says the file cannot be found. I suspect its my code, and not the file location as I have double checked my hard code, here is what I have so far, if anyone could point me in the right direction that would be great!
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 21
typedef struct data_slice
{
int t; // -> Time
float tp; // -> Valve pressure
float tf; // -> Sodium flow
float tt; // -> Sodium temp in Celsius
} data_slice;
void printIt(data_slice * data);
int main()
{
float num;
FILE *fptr;
data_slice data[ARRAY_SIZE];
if ((fptr = fopen("/Users/captainrogers/Documents/output_data.txt","r")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
fscanf(fptr,"%f \n", &num);
printIt(data);
fclose(fptr);
return 0;
}
void printIt(data_slice * data)
{
// Find the indice holding the time value of -10
int indice = 0;
for (int i = 0; i < ARRAY_SIZE; ++i)
{
if (data[i].t == -10)
{
indice = i;
break;
}
}
// Print results to screen
for (int i = 0, temp = indice; i < ARRAY_SIZE; ++i)
{
printf("%i\t %f\t %f\t %f\n", data[temp].t, data[temp].tp, data[temp].tf, data[temp].tt);
temp = (temp + 1) % ARRAY_SIZE;
}
}
Data I'm trying to print from .txt:
-10 595.000000 15.000000 167.000000
-9 557.000000 17.000000 168.000000
-8 634.000000 17.000000 114.000000
-7 656.000000 10.000000 183.000000
-6 561.000000 13.000000 139.000000
-5 634.000000 17.000000 124.000000
-4 672.000000 19.000000 155.000000
-3 527.000000 14.000000 166.000000
-2 656.000000 11.000000 188.000000
-1 661.000000 18.000000 141.000000
0 689.000000 17.000000 146.000000
1 624.000000 11.000000 104.000000
2 504.000000 20.000000 120.000000
3 673.000000 18.000000 147.000000
4 511.000000 12.000000 114.000000
5 606.000000 14.000000 171.000000
6 601.000000 13.000000 159.000000
7 602.000000 11.000000 127.000000
8 684.000000 10.000000 194.000000
9 632.000000 16.000000 139.000000
10 651.000000 13.000000 168.000000
fptr = fopen("C://Users//captainrogers//Documents//output_data.txt","r"))
try this code if you are sure your document is in the right file.
fopen("/Users/captainrogers/Documents/output_data.txt","r")
If the file can't be found, double-check your path. Maybe first try with the full path including the drive letter.
fscanf(fptr,"%f \n", &num);
Always check the return-value of fscanf(). "%f \n" is probably not the format string you want.
printIt(data);
Main problem: you never read any data into data[].

SSE Matrix Multiplication NxN. Why is this error

I am beginner using SSE instructions, and I try to implement MMM. So, I implemented MMM using matriz 2by2, now I want to implement MMM using matrix NXN
#include <emmintrin.h>
#include <stdio.h>
#include <stdlib.h>
void simd_2x2(int lda, double *A, double *B, double *C)
{
__m128d a, b1, c1;
for (int k = 0; k < lda; k++) {
//printf("%f\n",C[k * lda]);
c1 = _mm_loadu_pd(C + k * lda); //load unaligned block in C
//c2 = _mm_loadu_pd(C + 1 * lda);
for (int i = 0; i < lda; ++i) {
a = _mm_load_pd(A + i * lda);//load aligned i-th column of A
b1 = _mm_load1_pd(B + i + k * lda); //load i-th row of B
//b2 = _mm_load1_pd(B + i + 1 * lda);
c1 = _mm_add_pd(c1, _mm_mul_pd(a, b1)); //rank-1 update
//c2 = _mm_add_pd(c2, _mm_mul_pd(a, b2));
}
_mm_storeu_pd(C + k * lda, c1); //store unaligned block in C
//_mm_storeu_pd(C + 1 * lda, c2);
}
}
int main() {
int n = 2;
double *buf = NULL;
buf = (double *)malloc(3 * n * n * sizeof(double));
double *A = buf + 0;
double *B = A + n * n;
double *C = B + n * n;
simd_2x2(n, A, B, C);
return 0;
}
When n=2 everything work fine:
A = 4.000000 3.000000
2.000000 4.000000
B = 1.000000 3.000000
2.000000 4.000000
C = 0.000000 0.000000
0.000000 0.000000
C = C + A * B = 10.000000 24.000000
10.000000 22.000000
but if n=4 I get the next:
A = 4.000000 0.000000 1.000000 4.000000
2.000000 1.000000 4.000000 0.000000
3.000000 1.000000 2.000000 1.000000
4.000000 1.000000 3.000000 1.000000
B = 1.000000 5.000000 9.000000 13.000000
2.000000 6.000000 10.000000 14.000000
3.000000 7.000000 11.000000 15.000000
4.000000 8.000000 12.000000 16.000000
C = 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
C = C + A * B = 23.000000 59.000000 95.000000 131.000000
16.000000 44.000000 72.000000 100.000000
0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
The last two rows are not calculated, why is this? Can someone help me?
I have almost 5 days reading about SSE but I can not fully understand and neither solve this problem.

Merge sorting a struct

#include<stdio.h>
#include<stdlib.h>
typedef struct points{
float axis[2];
int id;
}Points;
typedef enum{
SortById,
SortByXAxis
}SortType;
Points* fill_Array(char* filename, int* length);
void Print_set(Points* set, int number_of_points);
void mergesort(Points* set, int low, int high, int number_of_points,SortType sort);
void merge(Points* set, int low, int middle, int high, int number_of_points,SortType sort);
int main(int argc, char* argv[])
{
int length;
Points *array;
array=fill_Array(argv[1],&length);
Print_set(array,length);
printf("\n\n");
mergesort(array,0,length,length,SortById);
Print_set(array,length);
return 0;
}
Points* fill_Array(char* filename,int* length)
{
int i;
Points* array;
FILE* file=fopen(filename,"r");
if(file == NULL)
{
return NULL;
}
fscanf(file,"%d",length);
array=malloc(sizeof(Points)* *length);
for(i = 0; i < *length; i++)
{
fscanf(file,"%d %f %f", &(array+i)->id,&(array+i)->axis[0],&(array+i)->axis[1]);
}
fclose(file);
return array;
}
void Print_set(Points *set, int number_of_points)
{
int i;
for(i = 0; i < number_of_points; i++)
{
printf("%d %f %f\n",(set+i)->id,(set+i)->axis[0],(set+i)->axis[1]);
}
}
void mergesort(Points* set,int low,int high,int number_of_points, SortType sort)
{
int mid1;
if((high-low)>=1)
{
mid1 = (low+high)/2;
mergesort(set, low, mid1, number_of_points, sort);
mergesort(set, mid1+1, high, number_of_points, sort);
merge(set, low, mid1, high, number_of_points, sort);
}
}
void merge(Points* set, int low, int middle, int high, int number_of_points, SortType sort)
{
int leftIndex=low;
int rightIndex=middle;
int combinedIndex = low;
Points tempArray[number_of_points];
int i;
while(leftIndex <= middle && rightIndex<= high)
{
if(set[leftIndex].id <= set[rightIndex].id)
{
tempArray[combinedIndex++] = set[leftIndex++];
}
else
tempArray[combinedIndex++] = set[rightIndex++];
}
if(leftIndex == middle+1)
{
while(rightIndex <= high)
{
tempArray[combinedIndex++] = set[rightIndex++];
}
}
else
{
while(leftIndex <= middle)
{
tempArray[combinedIndex++] = set[leftIndex++];
}
}
for( i = low; i < high; i++)
{
set[i] = tempArray[i];
}
}
I am trying to perform a merge sort on an input file using a custom merge sort function. The merge sort functions however are not working and print out down below, the first block being the actual input file printed out to make sure fscanf is reading in everything correctly and the second being the printing after the merge functions are run. The functions are duplicating some of the values and are not sorting them either and I cannot find the mistake in the code. Note that the enum will be used to sort either the ids or the first float values I am just trying to get the merge sort to work before I use it to sort either the ids or those values.
1 13.000000 7.000000
13 14.000000 6.000000
95 7.000000 13.000000
39 0.000000 20.000000
78 10.000000 10.000000
68 3.000000 17.000000
32 6.000000 14.000000
10 19.000000 1.000000
0 18.000000 2.000000
45 17.000000 3.000000
92 4.000000 16.000000
29 5.000000 15.000000
85 8.000000 12.000000
79 15.000000 5.000000
12 16.000000 4.000000
32 1.000000 19.000000
77 9.000000 11.000000
52 12.000000 8.000000
80 11.000000 9.000000
31 2.000000 18.000000
1 13.000000 7.000000
13 14.000000 6.000000
68 3.000000 17.000000
0 18.000000 2.000000
10 19.000000 1.000000
0 18.000000 2.000000
0 18.000000 2.000000
92 4.000000 16.000000
92 4.000000 16.000000
29 5.000000 15.000000
32 1.000000 19.000000
52 12.000000 8.000000
77 9.000000 11.000000
79 15.000000 5.000000
12 16.000000 4.000000
32 1.000000 19.000000
32 1.000000 19.000000
80 11.000000 9.000000
95 7.000000 13.000000
95 7.000000 13.000000
You appear to have gotten confused about the meaning of your boundary indices. Consider the initial call to function mergesort():
mergesort(array,0,length,length,SortById);
You pass the same value for arguments high and number_of_points, which is fine, but it implies that high represents an exclusive upper bound on the indices of the sort range. The mergesort() implementation, however, seems geared for argument high to represent an inclusive bound.
The confusion continues with your merge() function, which is probably the main culprit here. By taking the passed midpoint value as the start index of the right sub-array, it seems to be expecting the midpoint as an exclusive upper bound of the left sub-array, but the current mergesort() implementation passes an inclusive upper bound. On the other hand, some of the index comparisons performed by merge() are appropriate only if middle is an inclusive upper bound of the subarray.
In short, you have a muddle. The basic outline of the algorithm looks fine, but you need to decide (and document for yourself) what your function parameters represent, and reconcile your implementation details with that. Were I you, I would adopt half-open representation for all intervals, so that lower bounds are always inclusive, and upper bounds always exclusive. Among other things, that has the advantage that each midpoint value can be interpreted equally correctly as the (exclusive) upper bound of the left half of its subarray or as the (inclusive) lower bound of the right half.
void mergesort(Points* set,int low,int high,int number_of_points, SortType sort)
{
int mid1;
if((high-low)>1)
{
mid1 = (low+high)/2;
mergesort(set, low, mid1, number_of_points, sort);
mergesort(set, mid1, high, number_of_points, sort);
merge(set, low, mid1, high, number_of_points, sort);
}
}
void merge(Points* set, int low, int middle, int high, int number_of_points, SortType sort)
{
int leftIndex=low;
int rightIndex=middle;
int combinedIndex = low;
Points tempArray[number_of_points];
int i;
while(leftIndex <= middle && rightIndex < high)
{
if(set[leftIndex].id <= set[rightIndex].id)
{
tempArray[combinedIndex++] = set[leftIndex++];
}
else
tempArray[combinedIndex++] = set[rightIndex++];
}
if(leftIndex == middle+1)
{
while(rightIndex < high)
{
tempArray[combinedIndex++] = set[rightIndex++];
}
}
else
{
while(leftIndex < middle)
{
tempArray[combinedIndex++] = set[leftIndex++];
}
}
for( i = low; i < high; i++)
{
set[i] = tempArray[i];
}
}
0 18.000000 2.000000
1 13.000000 7.000000
10 19.000000 1.000000
0 18.000000 2.000000
12 16.000000 4.000000
13 14.000000 6.000000
29 5.000000 15.000000
31 2.000000 18.000000
32 6.000000 14.000000
32 1.000000 19.000000
39 0.000000 20.000000
39 0.000000 20.000000
52 12.000000 8.000000
31 2.000000 18.000000
68 3.000000 17.000000
77 9.000000 11.000000
78 10.000000 10.000000
12 16.000000 4.000000
79 15.000000 5.000000
85 8.000000 12.000000

LAPACK + C, weird behaviour

I am trying to solve a simple linear equations system using LAPACK. I use dbsvg method which is optimised for banded matrices. I've obsereved a realy strange behaviour. When I fill the AT matrix this way:
for(i=0; i<DIM;i++) AB[0][i] = -1;
for(i=0; i<DIM;i++) AB[1][i] = 2;
for(i=0; i<DIM;i++) AB[2][i] = -1;
for(i=0; i<3; i++)
for(j=0;j<DIM;j++) {
AT[i*DIM+j]=AB[i][j];
}
And call:
dgbsv_(&N, &KL, &KU, &NRHS, AT, &LDAB, myIpiv, x, &LDB, &INFO);
It works perfectly. However, when I do it this way:
for(i=0; i<DIM;i++) AT[i] = -1;
for(i=0; i<DIM;i++) AT[DIM+i] = 2;
for(i=0; i<DIM;i++) AT[2*DIM+i] = -1;
It results with a vector filled with NaN. Here are the declarations:
double AB[3][DIM], AT[3*DIM];
double x[DIM];
int myIpiv[DIM];
int N=DIM, KL=1, KU=1, NRHS=1, LDAB=DIM, LDB=DIM, INFO;
Any ideas?
You're not laying out the entries in the band storage properly; it was working before by a happy accident. The LAPACK docs say:
On entry, the matrix A in band storage, in rows KL+1 to
2*KL+KU+1; rows 1 to KL of the array need not be set.
The j-th column of A is stored in the j-th column of the
array AB as follows:
AB(KL+KU+1+i-j,j) = A(i,j) for max(1,j-KU)<=i<=min(N,j+KL)
On exit, details of the factorization: U is stored as an
upper triangular band matrix with KL+KU superdiagonals in
rows 1 to KL+KU+1, and the multipliers used during the
factorization are stored in rows KL+KU+2 to 2*KL+KU+1.
See below for further details.
So if you want a tridiagonal matrix with 2 on the diagonal and -1 above and below, the layout should be:
* * * * * * * ... * * * *
* -1 -1 -1 -1 -1 -1 ... -1 -1 -1 -1
2 2 2 2 2 2 2 ... 2 2 2 2
-1 -1 -1 -1 -1 -1 -1 ... -1 -1 -1 *
LDAB should be 4 in this case. Bear in mind that LAPACK uses a column-major layout, so the actual array should be look like this in memory:
{ *, *, 2.0, -1.0, *, -1.0, 2.0, -1.0, *, -1.0, 2.0, -1.0, ... }
dgbsv was giving different results for the two identical arrays because it was reading off the ends of the arrays that you had laid out.
Is this the exact code you used or just an example? I ran this code here (just cut and pasted from your posts, with a change of AT to AT2 in the second loop:
const int DIM=10;
double AB[DIM][DIM], AT[3*DIM], AT2[3*DIM];
int i,j;
for(i=0; i<DIM;i++) AB[0][i] = -1;
for(i=0; i<DIM;i++) AB[1][i] = 2;
for(i=0; i<DIM;i++) AB[2][i] = -1;
for(i=0; i<3; i++)
for(j=0;j<DIM;j++) {
AT[i*DIM+j]=AB[i][j];
}
printf("AT:");
for (i=0;i<3*DIM;++i) printf("%lf ",AT[i]);
printf("\n\n");
for(i=0; i<DIM;i++) AT2[i] = -1;
for(i=0; i<DIM;i++) AT2[DIM+i] = 2;
for(i=0; i<DIM;i++) AT2[2*DIM+i] = -1;
printf("AT2:");
for (i=0;i<3*DIM;++i) printf("%lf ",AT2[i]);
printf("\n\n");
printf("Diff:");
for (i=0;i<3*DIM;++i) printf("%lf ",AT[i]-AT2[i]);
printf("\n\n");
and got this output
AT:-1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.0000
00 -1.000000 -1.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.0
00000 2.000000 2.000000 2.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.0000
00 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000
AT2:-1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000
000 -1.000000 -1.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.
000000 2.000000 2.000000 2.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000
000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000
Diff:0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.0
00000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.
000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0
.000000 0.000000 0.000000 0.000000
Apparently AT and AT2 are the same. Which I would expect.

Resources