-2

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;


}

R.J. Dunnill
  • 2,049
  • 3
  • 10
  • 21
lm01010
  • 1
  • 1
  • 3
    Show the real code. `leer` is open as input, but you are reading from `FILE* read` ? – rafix07 Jul 07 '19 at 17:41
  • 4
    Why do you read the values as strings to begin with? Why not use e.g. `fscanf(leer, "%lf %lf", &doub1, &doub2)`? – Some programmer dude Jul 07 '19 at 17:41
  • 3
    Turn on more warnings! The compiler should be screaming helpful hints. `z[i]` is a `char` and you assign a double to it. – Ted Lyngmo Jul 07 '19 at 17:42
  • 4
    Why are you using `fscanf` in C++? – melpomene Jul 07 '19 at 17:43
  • 1
    And please take some time to read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). For example, what are the problem you have? What isn't working? Do you get build errors? Do you get run-time error? Invalid results? What is the input to the program? What is the expected and actual output? Etc. – Some programmer dude Jul 07 '19 at 17:44
  • 3
    What problems do you find? Don't say 'it doesn't work', tell us what actually happens. – john Jul 07 '19 at 17:46

1 Answers1

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:

#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

A M
  • 14,694
  • 5
  • 19
  • 44