473,651 Members | 2,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to read data?

to read float type data, we can use:

....
float f;
fscanf(outfile, "%f", &f);
....

but for double type, how to do that?
....
double d;
fscanf(outfile, "%f", &d);
....

Does that mean I lost data precision?

Feb 7 '06 #1
10 2335
kathy wrote:
to read float type data, we can use:

....
float f;
fscanf(outfile, "%f", &f);
....

but for double type, how to do that?
....
double d;
fscanf(outfile, "%f", &d);
....

Does that mean I lost data precision?

Do it in C++:

double d;

std::cin >> d;

--
Ian Collins.
Feb 7 '06 #2

"kathy" <yq*****@yahoo. com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
to read float type data, we can use:

...
float f;
fscanf(outfile, "%f", &f);
...

but for double type, how to do that?
...
double d;
fscanf(outfile, "%f", &d);
fscanf(outfile, "%lf", &d); ...

Does that mean I lost data precision?


Using "%f" with 'fscanf()' for type 'double'
means you get undefined behavior.

BTW have you considered using std::cin
instead of a FILE* stream? It has overloaded
operators defined for it; the one for the
right type gets called automatically.

float f;
double d;

std::cin >> f; // reads a float
std::cin >> d; // reads a double

-Mike
Feb 7 '06 #3
In C, you can do it like:
//read as a string
char d[40];
fscanf(file,"%s ",d);
///convert it into double using strtod()
call strtod()

Feb 7 '06 #4
upashu2 wrote:
In C, you can do it like:
//read as a string
char d[40];
fscanf(file,"%s ",d);
///convert it into double using strtod()
call strtod()


That's also a classic example of buffer overflow.

At least do:

char d[40];
fscanf(file,"%3 9s",d);
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 7 '06 #5
could you give me example how to use std::cin to read data from file?

Feb 7 '06 #6
kathy wrote:
could you give me example how to use std::cin to read data from file?


He probably means std::ifstream. std::cin and std::ifstream inherit
from std::istream.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 7 '06 #7
could you point it to me where is the article compare std::fstream with
FILE* file = fopen(...). What is the benefit to use the std::fstream ?

Feb 7 '06 #8
kathy wrote:
could you point it to me where is the article compare std::fstream with
FILE* file = fopen(...). What is the benefit to use the std::fstream ?


fstream manages the resources for you, you do not have to explicitly
close the file, fstream will close the file when its destructor is
called, whether explicitly, because it goes out of scope, or because an
exception was thrown.

Additionally it provides the stream operators, buffering etc. for
convenience.

Probably best to see your favourite C++ book for how to use fstream.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 7 '06 #9
"kathy" writes:
could you give me example how to use std::cin to read data from file?


You can't, cin is an object which is by deault, tied to the keyboard. At
least you can't without an extraordinary amount of effort. He meant to use
the overloaded >> operator which is part of <iostream>.

#include <iostream>

int main()
{
using std::ifstream;
using std::cout;
using std::endl;

ifstream in_file("junk07 .txt");
if(!in_file)
{
cout << "Can't open file\n";
//system("pause") ; // needed by my compiler, not really a necessary
part of the program
return 1; // error exit
}
int n;
float x;
double y;
in_file >> n >> x >> y;
cout << n << ' ' << x << ' ' <<y << endl;
//system("pause") ;
}
----------------
Junk27 looked like this:
1024 2.71828 3.1416

All on one line. It could have been on three lines.

It must be in the same directory as the executable produced by your
compiler. You can probably create it with the editor that comes with your
compiler.
Feb 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2306
by: Peter Ammon | last post by:
I would like to read from a pipe in which data may arrive slowly. From experimenting, it looks like os.read() will block until it returns the maximum amount of data you asked for, in the second parameter to read(), or until it hits EOF. I cannot find a way to return only the data that the file object has immediately available. If no data is available, blocking is OK. The only workaround I can think of is to call select() in a loop,...
2
8993
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My question is now, where can I find a manual that describes what the read method does with the ifstream object? I'm sitting here with my Linux/Debian machine, but I have not found any
4
40248
by: francis70 | last post by:
Hi, I have these 2 problem? Is there a way in Oracle to read UNCOMMITED data. i.e. in Oracle the normal behaviour is that a user's updates to a table are visible to other users ONLY when the user commits. But in Informix there is this thing called ISOLATION LEVELS. For example by setting the ISOLATION LEVEL to DIRTY READ, a user will read dirty data, i.e. the last uncommited updated value of a field by some other user. Is
11
12696
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to read the new date. So process B starts "select ..." but does not get the previously inserted row. The timespan between commit and select is very short. (NOTE: two different sessions are used) Questions: 1.) Does commit when returning from call...
18
4876
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
0
4692
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading the Microsoft based files with an OleDbDataAdapter. Then filling the contents of the first worksheet into a dataset. However when I try to add the XML based file to my dataset using an XmlTextReader I can never seem to get it to save to a...
0
4720
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made the client application establish a connection, and send data, which appears in plain, de-crypted text on the server - this works.
6
5703
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
4
2792
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have following data.bin (3 Data appended to 2 Data): 2, data0, data1, 3, data0, data1, data2
1
3923
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware of. f.open(filename,ios::in | ios::out | ios::binary | ios::trunc) The program flow is 1) write some data 2) read the data
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.