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:
#include<iostream>
#include<fstream> // *** Not used
#include<string> // *** Not used
#include<stdlib.h> // *** Not used
#include <string.h> // *** Not used
#include <stdio.h
#include<iomanip> // *** Not used
#include<cmath> // *** Not used
using namespace std;
FILE* write, * read; // *** Major bug. You are not using this variables, instead you are using 'leer' and 'escribir'
double dz, pm, xm, doub1, doub2;
// *** Do not use plain arrays in C++. Never
char s1[10], pz[10], s2[10], x[10], z[10]; // *** You want to be pz and z strings!
int main() {
dz = 0.00375;
// *** fopen is deprecated in C++
leer = fopen("data.dat", "r"); // *** Major bug. Variable leer has not been defined
escribir = fopen("exitdata.dat", "w+");//Donde vamos a escribir // *** Do not write spanish// *** Major bug. Variable escribir has not been defined
for (int a = 0; a < 5; a++) { // *** Why 2 loops? And "a" is never used. Maybe you wan to read 5 values?
for (int i = 0; i < 10; i++) { // *** Why this loop? You want to copy data in the char array.
// *** You are now invoking the loop body 50times
// *** fscanf is deprecated in C++
fscanf(read, "%s %s \n", s1, s2); // *** Read is a none initialize variabel System will crash.You opened leer
//here begins the problem // ** no, the problem began with trying fscanf on a none initialize file pointer
doub1 = atof(s1);
doub2 = atof(s2);
z[i] = (doub1 + 1) * dz; // Complier error. You are trying to assign a double to 1 char
pz[i] = doub2; // Complier error. You are trying to assign a double to 1 char
fprintf(escribir, "%s %s \n", s1, s2);
cout << z << " " << pz << endl; // *** Will output nothing. You are passing an empty char[]
}
}
fclose(escribir);//cerrar el archivo
fclose(leer);
return 0;
}
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
#include<iostream>
#include <stdio.h>
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;
#pragma warning(suppress : 4996)
read = fopen("r:\\data.dat", "r");
#pragma warning(suppress : 4996)
write = fopen("r:\\exitdata.dat", "w+");
for (int a = 0; a < 5; a++) {
for (int i = 0; i < 10; i++) {
#pragma warning(suppress : 4996)
fscanf(read, "%s %s \n", s1, s2);
doub1 = atof(s1);
doub2 = atof(s2);
//z[i] = (doub1 + 1) * dz; // No meaning
//pz[i] = doub2; // No meaning
fprintf(write, "%s %s \n", s1, s2);
}
}
fclose(write);
fclose(read);
return 0;
}
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.
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
struct TwoDoubles
{
double doub1;
double doub2;
// Overwrite extractor operator
friend std::istream& operator >> (std::istream& is, TwoDoubles& td) {
return is >> td.doub1 >> td.doub2;
}
// Overwrite inserter operator
friend std::ostream& operator << (std::ostream& os, const TwoDoubles& td) {
return os << td.doub1 << ' ' << td.doub2;
}
};
int main()
{
// Open file with source data
std::ifstream is("r:\\data.dat");
// If file could be opened
if (is) {
// Open file containing results
std::ofstream os("r:\\exitdata.dat");
// If file could be opened
if (os)
{
// Define a vector of doubles and initialize it
std::vector<TwoDoubles> values{ std::istream_iterator<TwoDoubles>(is),std::istream_iterator<TwoDoubles>() };
constexpr double dz = 0.00375;
// Now we will calculate. Multiply all doubles with dz;
std::for_each(values.begin(), values.end(), [dz](TwoDoubles & td) { td.doub1 = (td.doub1 + 1) * dz; });
// Write everything to output file
std::copy(values.begin(), values.end(), std::ostream_iterator<TwoDoubles>(os, "\n")) ;
}
}
return 0;
}
Please continue to study