I have a text file with numbers: two numbers on each row, separated by a space. Each pair of numbers represents an (x, y) co-ordinate. I am trying to write this in C, because it's the language I know, but I am working in Visual Studio 2010. The code I have is as follows:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define MAXPOINTS 10
int _tmain(int argc, _TCHAR* argv[])
{
double points [MAXPOINTS];
int i;
for (i = 0; i < MAXPOINTS; i++) {
points[i] = 0.0;
}
FILE* pFile;
pFile = fopen ("points.txt","r");
if (pFile == NULL)
{
printf("Could not open file\n");
return 0;
}
rewind (pFile);
i = 0;
while (fscanf(pFile, "%f %f", &points[i], &points[i + 1]) == 2) {
printf("blah\n");
i = i + 2;
}
for (i = 0; i < MAXPOINTS; i++) {
printf("[%d] = %f\n", i, points[i]);
}
fclose (pFile);
return 0;
}
The output is:
blah
blah
blah
[0] = 0.000000
[1] = 0.000000
[2] = 0.000000
[3] = 0.000000
[4] = 0.000000
[5] = 0.000000
[6] = 0.000000
[7] = 0.000000
[8] = 0.000000
[9] = 0.000000
Where points.txt has three rows:
100 200
300 400
500 500
I can't figure out why the numbers aren't being read into the array.
Any ideas?
%f format requires pointer to float, and you give pointer to double.
Related
I'm honestly going nuts here trying to understand why this generic array printer implementation is not working. Well, it works for an array of int's, but not for double's. What am I missing here?
void array_printer(FILE* stream, void* data, size_t data_type_size, size_t nr_rows, size_t nr_cols, char *format){
size_t offset = 0;
for (size_t r=0; r<nr_rows;++r){
for (size_t c=0; c<nr_cols; ++c){
fprintf(stream,format, *((int8_t*)data + offset*data_type_size));
//fprintf(stream,format, *((char*)data + offset*data_type_size)); // same behaviour
offset++;
}
fprintf(stream,"\n");
}
}
void array_print_double_2D(FILE* stream, double* data, size_t nr_rows, size_t nr_cols){
array_printer(stream, data, sizeof(double), nr_rows, nr_cols, " %lf ");
}
void array_print_int_2D(FILE* stream, int* data, size_t nr_rows, size_t nr_cols){
array_printer(stream, data, sizeof(int), nr_rows, nr_cols, " %d ");
}
int main(){
double *data_double = calloc(12, sizeof(double));
data_double[0] = 1;
data_double[1] = 2;
data_double[2] = 3;
array_print_double_2D(stdout, data_double, 3, 4);
/*
0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 what??
0.000000 0.000000 0.000000 0.000000
*/
int*data_int = calloc(12, sizeof(int));
data_int[0] = 1;
data_int[1] = 2;
data_int[2] = 3;
array_print_int_2D(stdout, data_int, 3, 4);
/*
1 2 3 0
0 0 0 0 correct
0 0 0 0
*/
Expression *(int8_t*)(data+...) will return int8_t which is not consistent with printf specifier " %lf ". This results in Undefined behavior.
The problem can be efficiently solved with macros that generate the printers.
#include <stdio.h>
#include <stdlib.h>
#define DEFINE_ARRAY_PRINT_2D(TYPE, FMT) \
void array_print_ ## TYPE ## _2D(FILE* stream, void* data, size_t nr_rows, size_t nr_cols) { \
size_t offset = 0; \
for (size_t r=0; r<nr_rows;++r){ \
for (size_t c=0; c<nr_cols; ++c){ \
fprintf(stream,FMT, ((TYPE*)data)[offset]); \
offset++; \
} \
fprintf(stream,"\n"); \
} \
}
// generate printers
DEFINE_ARRAY_PRINT_2D(int, " %d ")
DEFINE_ARRAY_PRINT_2D(double, " %lf ")
int main(){
double *data_double = calloc(12, sizeof(double));
data_double[0] = 1;
data_double[1] = 2;
data_double[2] = 3;
array_print_double_2D(stdout, data_double, 3, 4);
int *data_int = calloc(12, sizeof(int));
data_int[0] = 1;
data_int[1] = 2;
data_int[2] = 3;
array_print_int_2D(stdout, data_int, 3, 4);
return 0;
}
Produces output:
1.000000 2.000000 3.000000 0.000000
0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
1 2 3 0
0 0 0 0
0 0 0 0
As expected.
You're casting the memory to int8_t* and dereferencing it, what were you expecting? I'd provide some sort of enumeration to distinguish between the typesand cast to the right one when needed.
Below I have a sample table stored in a text file - random.dat.
0.00 0.01 2.30 300
2.00 0.015 1.45 102
4.00 0.03 6.08 174
6.00 5.95 0.05 225
Now I want to read the contents of the table and store into an array. I have the following code that almost does the job.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j;
int M = 3, N = 3;
FILE *fp;
double value1[M][N];
fp = fopen("random.dat","r");
if (fp == NULL)
{
exit(1);
}
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
fscanf(fp, "%lf", &value1[i][j]);
if (feof(fp))
{
break;
}
}
}
\*Just to check that my code has stored the arrays properly, I put the following printf statements*/
printf("%lf\n", value1[0][0]);
printf("%lf\n", value1[0][1]);
printf("%lf\n", value1[0][2]);
printf("%lf\n", value1[0][3]);
printf("%lf\n", value1[1][0]);
printf("%lf\n", value1[1][1]);
printf("%lf\n", value1[1][2]);
printf("%lf\n", value1[1][3]);
}
The values that I want and thought the program would give me:
value1[0][0] = 0.00 value1[0][1] = 0.01 value1[0][2] = 2.30 value1[0][3] = 300
value1[1][0] = 2.00 value1[1][1] = 0.015 value1[1][2] = 1.45 value1[1][3] = 102
Instead, it gives me,
value1[0][0] = 0.00 value1[0][1] = 0.01 value1[0][2] = 2.30 value1[0][3] = 300.0
value1[1][0] = 300.0 value1[1][1] = 2.00 value1[1][2] = 0.015 value1[1][3] = 1.45
I couldn't figure out a solution for this. Can someone point where I have made an error?
Thank you
If M and N are 3, value1 [0] [3] is out of bounds. Change N to 4.
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[].
So right now I am simply trying to take a dummy data file,
1.30640 1
0.91751 1
0.49312 1
0.49312 0
1.79859 1
1.86360 1
0.12313 1
0.12313 0
-0.19091 1
1.82377 1
0.63205 1
0.63205 0
1.23357 1
0.62110 1
0.80438 1
and at the moment store it as a gal_matrix for manipulations later. Below is code which simply at the moment, given a data file name finds out how long it is (i.e. number of rows), initializes a gsl_matrix struct and then try to scan the text file into that matrix, called chain.
#include <stdio.h> // Needed for printf() and feof()
#include <math.h> // Needed for pow().
#include <stdlib.h> // Needed for exit() and atof()
#include <string.h> // Needed for strcmp()
#include <gsl/gsl_matrix.h> // Needed for matrix manipulations
/*------------ Defines -----------------------------------------------------------------*/
#define MAX_SIZE 10000000 // Maximum size of the time series array
#define NUM_LAG 1000 // Number of lags to calculate for
/*
*----------------------------------------------------------------------------------------
* Main program
*----------------------------------------------------------------------------------------
*/
int main(int argc, char* argv[]) {
//--------Initialization--------------------------------------------------------------
double ac_value; // computed autocorrelation value
int i,j; // Loop counter
long int N;
double mean, variance;
gsl_matrix * chains;
char filename[100];
FILE* in_file; // input file
FILE* out_file; // output file
int no_params; // number of parameters to calculate autocorrelation for
int first_column; // Which column first corresponds to a chain
int ch; // to determine number of samples in file
printf("-------------------------------------------------------------------------\n");
//--------Check that there are the correct number of arguments passed-----------------
if(argc != 4) {
printf("usage: ./auto_corr chainfile no_params first_column \n");
exit(1); // 0 means success typically, non-zero indicates an error
}
//--------Extract arguments-----------------------------------------------------------
sprintf(filename,"%s",argv[1]); // convert input file to string
in_file = fopen(filename,"rb"); // open input file for reading
no_params = atoi(argv[2]);
first_column = atoi(argv[3]);
//--------What is the number of samples in chain file?--------------------------------
N = 0; // Initialize count
while(!feof(in_file)) {
ch = fgetc(in_file);
if(ch == '\n'){
N++;
}
}
printf("Number of samples: %li\n", N); // print number of samples
if (N > MAX_SIZE) { // throw error if there are too many samples
printf("ERROR - Too many samples! MAX_SIZE = %i", MAX_SIZE);
exit(2);
}
//--------Generate a gsl matrix from the chains---------------------------------------
printf("%i\n", no_params);
chains = gsl_matrix_alloc(N, no_params); // allocate memory for gsl_matrix(rows, cols)
// print the matrix (for testing)
printf("Chain matrix \n");
for (int m=0;m<N;m++) { //rows
for (int n=0; n<no_params;n++) { // columns
printf("%f ",gsl_matrix_get(chains,m,n));
}
printf("\n");
}
// gsl_matrix_fprintf(stdout,chains,"%f"); // easy way to print, no formatting though
gsl_matrix_fscanf(in_file, chains); // read in chains to the gsl_matrix
fclose(in_file);
The error is occurring in the gsl_matrix_fscanf line, and the output I am seeing is
$ ./auto_corr auto_corr_test_data.dat 2 0
-------------------------------------------------------------------------
Number of samples: 15
2
Chain matrix
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 0.000000
0.000000 0.000000
0.000000 0.000000
0.000000 0.000000
0.000000 0.000000
0.000000 0.000000
gsl: ./fprintf_source.c:165: ERROR: fscanf failed
Default GSL error handler invoked.
Abort trap: 6
I forgot to rewind the input file after I determined the number of rows.
Ok so I have never taken a C programming course and I have a general idea of what Arrays are, but I cant seem to figure out why this array has a certain output.
#include <stdio.h>
#include <time.h>
#include <stdlib.h> // include srand function
#pragma warning(disable : 4996)
#define Size 15
main() {
int a[Size];
int i;
double b1[Size], b2[Size];
srand( (unsigned)time( NULL ) ); // Use current time as seed
// apply (start) timestamp
for (i = 0; i < Size; i++)
{
a[i] = rand() % 100;
// initialize the array using random number between 0 and 99
b1[i] = a[i]/5;
printf("a[%d] = %d\n", i, a[i]);
printf("b1[%d] = %f\n", i, b1[i]);
b2[i] = a[i];
b2[i] = b2[i]/5;
printf("b2[%d] = %f\n", i, b2[i]);
}
// apply (end) timestamp
//compute the time elapsed and display time in seconds
}
For my homework I need to run this code and explain why b1 and b2 have same or different values. I cant seem to wrap my head around what is happening here:
for (i = 0; i < Size; i++)
{
a[i] = rand() % 100;
// initialize the array using random number between 0 and 99
b1[i] = a[i]/5;
printf("a[%d] = %d\n", i, a[i]);
printf("b1[%d] = %f\n", i, b1[i]);
b2[i] = a[i];
b2[i] = b2[i]/5;
printf("b2[%d] = %f\n", i, b2[i]);
}
If someone could explain what the code is doing here I would very much appreciate it.
Thanks!
Edit- This is the output once I run the program:
a[0] = 8
b1[0] = 1.000000
b2[0] = 1.600000
a[1] = 74
b1[1] = 14.000000
b2[1] = 14.800000
a[2] = 78
b1[2] = 15.000000
b2[2] = 15.600000
a[3] = 64
b1[3] = 12.000000
b2[3] = 12.800000
a[4] = 53
b1[4] = 10.000000
b2[4] = 10.600000
a[5] = 6
b1[5] = 1.000000
b2[5] = 1.200000
a[6] = 71
b1[6] = 14.000000
b2[6] = 14.200000
a[7] = 4
b1[7] = 0.000000
b2[7] = 0.800000
a[8] = 7
b1[8] = 1.000000
b2[8] = 1.400000
a[9] = 57
b1[9] = 11.000000
b2[9] = 11.400000
a[10] = 55
b1[10] = 11.000000
b2[10] = 11.000000
a[11] = 13
b1[11] = 2.000000
b2[11] = 2.600000
a[12] = 55
b1[12] = 11.000000
b2[12] = 11.000000
a[13] = 96
b1[13] = 19.000000
b2[13] = 19.200000
a[14] = 25
b1[14] = 5.000000
b2[14] = 5.000000
Because you implicitly convert int to double and both times do that different ways.
b1[i] = a[i] / 5 ;
/* ^ ^ int ^
* | +-- int
* +-- implicit convertion to double */
b2[i] = b2[i] / 5 ;
/* ^ double ^
* + int implicitly converted
* to double */
In first expression you divide int by int where you lose precision (i.e. 7 / 2 gives you 3, not 3.5),
In second expression you divide double by double. That doesn't lead to that.