I try to write some vectors in a file but the atof function gives me problems and I don't know how to solve it. Up to the fscanf line (read, "% s% s \ n", s1, s2); it doesn't present problems to me, then yes.
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include <string.h>
#include <stdio.h>
#include<iomanip>
#include<cmath>
using namespace std;
FILE *write,*read;
double dz, pm ,xm, doub1,doub2;
char s1[10],pz[10], s2[10],x[10], z[10];
int main(){
dz=0.00375;
leer=fopen("data.dat","r");
escribir=fopen("exitdata.dat","w+");//Donde vamos a escribir
for(int a=0; a<5; a++){
for(int i=0; i<10; i++){
fscanf( read,"%s %s \n", s1, s2);
//here begins the problem
doub1 = atof(s1);
doub2 = atof(s2);
z[i]=(doub1+1)*dz;
pz[i]=doub2;
fprintf(escribir,"%s %s \n", s1,s2);
cout<<z<<" "<<pz<<endl;
}
}
fclose(escribir);//cerrar el archivo
fclose(leer);
return 0;
}
The atof function does work very well.
But overall you have many severe problems in your code. First of all, and most important: The code cannot compile. The compiler spits out many errors. And, it shows, what went wrong. So please run the compiler, before you post code with many errors.
Then, I see the C++ tag- Look at your header files. Many have a ".h" at the end. Your are using the language C except in one line:
cout << z << " " << pz << endl;. And this is also wrong. It will not print, what you expected.You are also not using any vector here. And the formatting is really bad.
Let me first make a short code review:
So many many errors and wrong understanding of C and C++. Let me make the code compilable. But it will still not do what you wish, because you have tons of semantic errors.
Compiled with MS Visual Studio 2019
Of course this also does not give you the expected result.
And finally the C++ example of your code. Please read some good C++ book. Please read about the used functions in cppreference. Please try to understand line by line.
Please continue to study