Herezh_dev/Resolin/Matrices_externes/MV++/mvvdio.cc

124 lines
2.3 KiB
C++
Raw Permalink Normal View History

2021-09-28 17:28:23 +02:00
#include <stdio.h>
#include "vecdefs_GR.h" // modif GR
#include "iotext.h"
#include "Sortie.h"
int writetxtfile_vec(const char *filename, const MV_Vector_double &A)
{
FILE *in_file;
in_file = fopen(filename, "w");
if (in_file == NULL)
{
fprintf(stderr,"Cannot open file: %s\n", filename );
Sortie(1);
}
int N = A.size();
for (int i=0; i<N; i++)
{
fprintf(in_file, "%g\n", A(i));
}
fclose(in_file);
return 0;
}
int readtxtfile_vec(const char *filename, MV_Vector_double *Aptr)
{
FILE *in_file;
char line[82];
char *line_ptr;
int count=0;
double tmp;
MV_Vector_double &A = *Aptr;
in_file = fopen( filename, "r");
if (in_file == NULL)
{
cerr << "Cannot open file: " << filename << endl;
Sortie(1);
}
// one vector element per line
while ( line_ptr = fgets(line, 82, in_file))
if (sscanf(line_ptr, "%lg", &tmp) >= 1) count++;
rewind(in_file);
A.newsize(count);
for (int i=0; i< count; i++)
{
if (fscanf(in_file, "%lg", &A(i)) < 1)
{
printf("Error reading %s\n", filename);
Sortie(1);
}
}
fclose(in_file);
return 0;
}
int writetxtfile_vec(const char *filename, const MV_Vector_int &A)
{
FILE *in_file;
in_file = fopen(filename, "w");
if (in_file == NULL)
{
fprintf(stderr,"Cannot open file: %s\n", filename );
Sortie(1);
}
int N = A.size();
for (int i=0; i<N; i++)
{
fprintf(in_file, "%d\n", A(i));
}
fclose(in_file);
return 0;
}
int readtxtfile_vec(const char *filename, MV_Vector_int *Aptr)
{
FILE *in_file;
char line[82];
char *line_ptr;
int count=0;
MV_Vector_int &A = *Aptr;
int tmp;
in_file = fopen( filename, "r");
if (in_file == NULL)
{
cerr << "Cannot open file: " << filename << endl;
Sortie(1);
}
// one vector element per line
while ( line_ptr = fgets(line, 82, in_file))
if (sscanf(line_ptr, "%d", &tmp) >= 1) count++;
rewind(in_file);
A.newsize(count);
for (int i=0; i< count; i++)
{
if (fscanf(in_file, "%d", &A(i)) < 1)
{
printf("Error reading %s\n", filename);
Sortie(1);
}
}
fclose(in_file);
return 0;
}