473,749 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read data from files using fstreams

Hello.

I'm moving from Visual C++ 6.0 to Visual C++ .NET 2003 and I saw the
changes to the streams. I'm rewriting my code and finished doing it
yesterday.

All seemed to be right, but the file reading.

My old code has lines like the following function call:

(file is a ifstream, var is a WORD, that is a typedef unsigned sort)
here is the line:
file.read(&var, 2)

Well, when this line is executed, the results are not what I'm looking
for: it reads inconsistent (arbitrary?) data. In my old code this line
worked. It is supposed to read 2 bytes from the file and asign them to
the WORD in var.

For this reason, I think that Visual C++ 6.0 and .NET 2003 has some
slight differences for reading in the way I want to read.

Maybe I'm not properly using the function. I mean... maybe I don't
understand well the mechanism in C++ to read BYTES, WORDS, DWORDS
UWORDS, etc.

So I request your help and would be thankful is someboy teach me about
reading using standar C++ streams in this .NET 2003.

Thank you very much.

Nov 17 '05 #1
2 988
"José Manuel (JMN CandyBeat)" <jm*@superjmn.c om> wrote in message
news:uK******** ******@TK2MSFTN GP10.phx.gbl
My old code has lines like the following function call:

(file is a ifstream, var is a WORD, that is a typedef unsigned sort)
here is the line:
file.read(&var, 2)

Well, when this line is executed, the results are not what I'm looking
for: it reads inconsistent (arbitrary?) data.


Are you opening the file in text mode (without ios::binary flag) by any
chance? What you see may be caused by CRLF to \n translation.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken
Nov 17 '05 #2
Igor Tandetnik wrote:
"José Manuel (JMN CandyBeat)" <jm*@superjmn.c om> wrote in message
news:uK******** ******@TK2MSFTN GP10.phx.gbl
My old code has lines like the following function call:

(file is a ifstream, var is a WORD, that is a typedef unsigned sort)
here is the line:
file.read(&va r, 2)

Well, when this line is executed, the results are not what I'm looking
for: it reads inconsistent (arbitrary?) data.

Are you opening the file in text mode (without ios::binary flag) by any
chance? What you see may be caused by CRLF to \n translation.


Whops! I solved the problem, it's because I was reading the wrong
position in the file and that explains why the reads was incoherent!

(By the way, I was using ios::binary | ios::in)

Thank you!!
Nov 17 '05 #3

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

Similar topics

3
3722
by: Albert Tu | last post by:
Dear there, We have an x-ray CT system. The acquisition computer acquires x-ray projections and outputs multiple data files in binary format (2-byte unsigned integer) such as projection0.raw, projection1.raw, projection2.raw ... up to projection500.raw. Each file is 2*1024*768-byte big. I would like to read those files and convert to ascii files in %5.0f/n format as projection0.data ... projection500.data so that our
4
2054
by: Clint Ruen | last post by:
Hello all, I have written out a data structure using the binary flag on an ofstream. The struct/class is something like this class SomeData { public: int data1;
0
4703
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...
8
2338
by: dosworldguy | last post by:
I have been having a very peculiar issue from a long time. I have an application where multiple clients read from a shared set of files. When a record is changed, sometimes the win9x clients still read the old data (if it was read earlier) and this is causing data corruption. WinNT clients inlcuding windows2000 & XP do not have this issue. The program is complied in VC++, console mode. I am unable to understand the cause. I flush the...
7
2148
by: Tracks | last post by:
I have old legacy code from vb5 where data was written to a file with a variant declaration (this was actually a coding error?)... in vb5 the code was: Dim thisdata as integer Dim thatdata Dim someother as integer thatdata = ubound( Array1 )
9
3563
by: fazulu deen | last post by:
Hi all, I have to read two text file(opcode,jump)... I guess the following statements are correct(but it is not reading the data from that files and also not checking the presence of the files,why so??) unsigned h, i=0;
6
32918
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line). Is there some way to burst read the whole file and later "extract" each line? Thanks in advance, Thomas Kowalski
10
22739
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app) outFile.write("teststring", 10); outFile.close(); If I leave out the ios::ate and ios::app modes my string is written to the start of the file as I'd expect but I want to write the data to
4
2776
by: unknown | last post by:
Hi, I'm having files with a name pattern like "log_record-YY-MM-DD" and i would like to create a list of the files available. I'm wondering how i can do this, any ideas? Thank you! Ron -- weeks of software enineering safe hours of planing ;)
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
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
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
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...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
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
4608
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...
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.