我尝试在文件中写入一些向量,但是atof函数给我带来了问题,我不知道如何解决。 直到fscanf行(读为“%s%s \ n”,s1,s2);它不会给我带来问题,然后是。
#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;
}
答案 0 :(得分:0)
atof函数运行得很好。
但是总的来说,您的代码中存在许多严重的问题。首先,也是最重要的:代码无法编译。编译器吐出许多错误。而且,它表明出了什么问题。因此,在发布有很多错误的代码之前,请运行编译器。
然后,我看到C ++标记-查看您的头文件。许多文件的末尾都有一个“ .h”。您正在使用语言C,但以下一行除外:cout << z << " " << pz << endl;
。这也是错误的。它不会按照您的预期打印。
您在这里也没有使用任何向量。而且格式真的很糟糕。
首先让我进行简短的代码审查:
#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;
}
如此之多的错误和对C和C ++的错误理解。让我使代码可编译。但是它仍然无法满足您的期望,因为您有大量的语义错误。
与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;
}
当然这也不能给您预期的结果。
最后是代码的C ++示例。请阅读一些不错的C ++书。请在cppreference中阅读有关使用的功能的信息。请尝试逐行了解。
#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;
}
请继续学习