C语言中fscanf函数读取double型浮点数的问题

为什么利用c语言读取txt数据显示全为0 读取自定义txt数据文档,在控制台显示读取数据全为0,不知道是什么原因。在fluent中输出结果全为0。这个是txt数据文本。 阅读详情

转载时请注明出处和作者联系方式
作者联系方式:Lutx (80437#zj.com)

 

在C语言里面, 将一个浮点数输出到文件中, 可以使用符号%f, 如

 

double a = 0.0;

fprintf(fp, "a = %f", a);

 

而如果要从文件中读取一个double类型的浮点数, 就不能用%f, 而要用%lf才可以. 如

 

double a;

float b;

fscanf(fp, "%f", &a);  // 这里得到的a的数值不是所期望的值

fscanf(fp, "%lf", &a); // 正确

fscanf(fp, "%f", &b);  // 正确

 

追究一下原因, 是因为格式符"%f"指示的是一个float类型(4个字节), 而我们给的指针位置(&a)是一个双精度(double)类型, 占8个字节, 这样在上面的第一个fscanf语句中, 实际上把a当初float类型, 也就是只填了8个字节的前4个字节, 而后面的4个字节并没变, 这样变量a的数据就不是一个正确的值.而格式符"%lf"利用修饰符l来表示这是一个长类型的浮点数(即双精度), 所有会得到正确的值.

 

而我们通常在程序中习惯定义浮点数为double类型, 所以在scanf函数中要谨慎使用"%f", 而应该使用"%lf".

 

别再死磕公式了!用Python+NumPy手把手实现机器人逆运动学数值求解(附避坑指南) 本文详细介绍了如何使用Python和NumPy实现机器人逆运动学(Inverse Kinematics, IK)的数值求解方法,包括牛顿-拉弗森法、雅可比矩阵计算及奇异位形处理。通过实战代码和避坑指南,帮助开发者快速掌握现代机器人学中的核心算法,提升工程实践能力。 阅读详情

相关推荐

MS2107 CVBS转USB2.0 音视频采集芯片,支持1080P@30HZ输出

MS2107 是一款视频和音频采集芯片,支持CVBS转USB2.0 ;可以将 CVBS、S-Video 和音频信号通过 USB接口传送到 PC、智能手机和平板电脑上预览或采集。

TEL18967112838的博客 2081

double输入

#include   void main()   {     double a,b;     printf("\n");     scanf("%lf,%lf",&a,&b);//double的符号是%lf     printf("a=%f,b=%f",a,b); //输出的时候

° ﹎.- 看淡1.苆х. 1327

Cadence Allegro新手避坑:从原理图到PCB,解决‘netrev.lst does not exist’的完整流程

本文详细介绍了Cadence Allegro从原理图设计到PCB导入网表的完整流程,特别针对新手常见的'netrev.lst does not exist'错误提供了系统解决方案。内容包括环境准备、原理图设计规范、网表生成与导入技巧,以及设计数据管理最佳实践,帮助工程师高效完成PCB设计任务。

weixin_42685438的博客 490

c语言fscanf读取文件,中文乱码。读取都是0

fscanf读取全是0,在文件开始有无用行数据的干扰,

m0_60352504的博客 1234

python实现带符号的二进制数转换十进制数 (附完整源码)

python实现带符号的二进制数转换十进制数 (附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 565

人体红外传感器HC-SR501

本文记录的是如何用stm32使用HC-SR501。实验平台:野火指南者stm32f103vet6。实验现象:一旦有人体经过,LED会亮红色。HC-SR501 传感器工作原理人体都有恒定的体温,一般在 37 度,所以会发出特定波长 10UM 左右的红外线,被动式红外探头就是靠探测人体发射的 10um 左右的红外,进行工作的。人体发射的 10um 左右。

weixin_43772611的博客 1万+

【转载】fscanf和fprintf函数从文件中格式化读取double型数据

在C语言里面, 将一个浮点数输出到文件中, 可以使用符号%f, 如  double a = 0.0; fprintf(fp, "a = %f", a);  而如果要从文件中读取一个double类型的浮点数, 就不能用%f, 而要用%lf才可以. 如  double a; float b; fscanf(fp, "%f", &a);  // 这里得到的a的数值不是所期望的值 fscan

GengLUT 3115

python基础:工作会用到的95%的基础python代码都在这了(万字详解!)

本文是一份Python编程语言的快速参考备忘单,涵盖了Python基础语法和常用数据类型。主要内容包括:Python入门指南(打印、变量声明)、内置数据类型(字符串、数值、列表、元组、集合、字典等)、控制结构(判断、循环)、函数定义、文件处理操作以及字符串操作方法(切片、循环遍历等)。这个备忘单特别适合初学者快速查阅Python基础语法,同时也提供了数据类型转换、f-string格式化字符串等实用技巧。通过简洁的代码示例展示了Python的基本语法规则和典型用法。

数据知道的博客 8170

C语言读double型数据

用C语言将一个文件中的数据读入一个double型数组

u012514940的专栏 1615

ROSTCM6软件下载及语义网络分析详细操作教程(附网盘链接)

ROSTCM6是武汉大学沈阳教授研发编码的国内目前唯一的以辅助人文社会科学研究的大型免费社会计算平台。该软件可以实现微博分析、聊天分析、全网分析、网站分析、浏览分析、分词、词频统计、英文词频统计、流量分析、聚类分析等一系列文本分析。

m0_64336780的博客 4万+

第一章 千牛实时获取消息之ws服务端搭建

以上就是今天要讲的内容,本文仅仅简单介绍了ws服务端的搭建,后面将介绍ws客户端的搭建和配合ws服务器的测试。

不想说话的树专栏 1854

【转载】fscanf和fprintf函数从文件中格式化读取double型数据 分类: ...

【转载】fscanf和fprintf函数从文件中格式化读取double型数据 C语言中fscanf函数读取double型浮点数的问题(点击打开转载链接1) 在C语言里面, 将一个浮点数输出到文件中, 可以使用符号%f, 如 double a = 0.0; fprintf(fp, "a = %f", a); 而如果要从文件中读取一个double类型...

LL54543的博客 586

《YOLO26/YOLO11/YOLOv8改进与系统实战》,改进与系统实战专栏目录和介绍

YOLOv8 是目前比较火和比较成熟的深度学习框架,是2023年1月发布的,由 Ultralytics 团队开发。Ultralytics自YOLOv5 开始一直积极维护和更新 YOLO 框架,因此 YOLOv8 也享有持续的维护与升级支持。因此我写下《手把手教你YOLOv8实战》专栏,专门为那些对计算机视觉、深度学习、以及目标检测技术感兴趣的读者设计。通过本专栏,你将深入理解 YOLOv8 的核心原理与实际应用,从0 开始学习并掌握如何使用 YOLOv8 完成各类目标检测任务,帮助你快速上手并掌握 YOLO

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 1万+

c语言fscanf读取double,c – 使用fscanf读取double

我想从文本文件中读取double例如31 39.9316476397222 116.113516352222我尝试了两种,而不是工作.我只能读取前几个十进制数字,例如39.93164但不是39.9316476397222有谁知道为什么?谢谢!int NodeID;double _lat,_long;fscanf (pFile, "%d %lf %lf", &NodeID,&_lat...

weixin_39687542的博客 976

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

C语言从txt文本中读取多行用逗号分隔的数据

例如数据为: 0.5712,0.45356,0.74399 0.58775,0.4721,0.77793 0.51964,0.42228,0.69693 将每一列的数据保存在一个double型数组中。 解决方案: 1.利用fscanf()函数:#include <stdio.h> #include<stdlib.h>int main() { FILE *fp = fopen("C

yanxiaopan的博客 1万+

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

fscanf读取double类型数据

double a; fscanf(fp,"%lf",&a); 需要用%lf,而不能用%f

pi314159265357859的专栏 1805

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

完美解决C语言gcc编译器不能读取小数数据TXT文件的问题

在c文件目录下准备好一个write_ata2020.txt,内容如下: 0.841471 0.909297 0.14112 -0.756802 -0.958924 -0.279415 0.656987 0.989358 0.412118 -0.544021 -0.99999 -0.536573 0.420167 0.990607 0.650288 -0.287903 -0.961397 -0.750987 0.149877 0.912945 C程序为: #incl

源代码杀手的博客 948

minGW在fprintf与fscanf操作long double类型时偷的懒

minGW在fprintf与fscanf操作long double类型时偷的懒前言准备工作环境测试代码验证编译阶段运行失败的解决方案:花式换格式字符结论 前言 当年没少跳printf和scanf输出输入double类型数据的坑。所以在研究c99标准的时候特别注意了关于fprintf和fscanf关于输出浮点型数的说明。 原来在使用printf输出double的时候,转义字符应该为%f或者%F 而...

William_hsu的博客 910

文件读取fscanf新的认识

1.读取一个double型 在c语言里面,将一个浮点数输出到文件中,可以使用符合%f. 如:double a = 0.0; fprintf(fp, "a=%f",a); 而要从文件中读取一个double类型的浮点数,就不能用%f,而要用%lf才可以。如: double a; float b; fscanf(fp, "%f", &a); //错误 fscanf...

weixin_33841503的博客 237

C中从文本读取double数据注意到地方

1.fscanf()参数“%lf”-->double,并且注意各个参数中的间距同文本中数据的间距相同。2.double一定要初始化,否则程序中签字的bug.3.double和float型:float 为单精度,有效数字为6~7double 为双精度,有效数字为15~16 但他们在输出时,小数点后都有6位小数。所以输出时只有格式化输出才会显示全部. 例子: #include

azurehilly的IT小路...... 691

c文件读取问题

txt数据文件内容如图: 执行代码为 #include <stdio.h> int main() { FILE *fr; char a[]="d:\\projects\\chenping\\data1.txt"; float x[2],m; //double x[2],m; int i,j; ...

aizong1639的博客 159
上一篇: MACBook EFI升级的后患
下一篇: QTableView表格中出现多余的Checkbox
Lutx
博客等级 码龄25年 50粉丝 41原创
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值