为什么功能对我不起作用?C
Why does the atof function not work for me? C++
我尝试在文件中写入一些向量,但是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;
}
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;
}
请继续学习
相关文章:
- 为什么我的打印功能不起作用?链表
- Cuda 基本程序 (将值写入矩阵和 std:cout 不起作用) ;主功能不启动
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 虚拟功能不起作用
- C++插入排序错误功能不起作用
- 使用链表.为什么我的插入功能不起作用?
- 功能指针不起作用(C )
- 为什么功能对我不起作用?C
- C 所有质数功能不起作用
- 朋友功能不起作用(语法错误)
- Winpcap:暂停/停止嗅探pcap_breakloop功能不起作用
- 我的模板功能具有通用引用不起作用
- C 类是功能不起作用的参数
- 现在将功能从主型到原型不起作用
- 排序功能不起作用
- TIM6,基本时间生成功能不起作用
- 功能调用不起作用,但控制台仍允许输入
- 二进制树级别明智的打印功能不起作用
- glutkeyboardfunc呼叫功能不起作用
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符