473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help needed using ifstream::seekg with windows text file

Hello.
I've searched all over and haven't seen another thread with this
problem. Please bear with me as I try to explain. thanks. :)

I have some programs that need to be cross-platform compatible (unix
and windowsXP). The first program parses a text file and records where
snippets are in terms of where it begins (char offset from begin of
the file) and length (number of chars).

One can almost use "byte" and "char" interchangeably here, given that
sizeof(char) is 1, however it doesn't quite work that way.

The second program tries to get things from this text file with the
information collected from program1. program2 tries to use seekg and
read. something like:

char* snippet = new char[length + 1];
ifstream read(file); // OR ifstream read(file, ios::binary);
read.seekg(offset, ios::beg);
read.read(snippet,length);
read.close();:
snippet[length] = '\0';

The problem is that when the code is as above in text mode, while read
actually reads in the number of characters, seekg seeks to the number
of bytes. So, if I seek to 0, I read in exactly what I need. If I need
to seek to any char length > 0, it doesn't seek to my next offset
correctly, and it appears that the number of characters it's missing
(short) is correlated with the number of newlines there are previous
to that point in the file. (read still reads in the correct number of
characters from that point)

I know that windows and unix treat newlines differently. But I can't
quite understand the behavior on windows to get it to do what I want.
It behaves as if a newline is 1 char with sizeof 2 bytes. (although if
I go through it character by character and print out sizeof(*c) I
never get anything that is 2, it's always 1.)

If I set it to binary mode, both seekg and read work in terms of
bytes. Successive seeks read in snippets one right after the other as
they appear in the text file with no overlap (actually, it drops 1
character in between for which I have no explanation). And each
snippet has fewer visible number of characters than its length.

This problem does not occur on unix and does not occur on windows if I
transfer a text file from unix in "binary" mode so that the \n's don't
get replaced by windows. In these instances, program2 behaves as
expected regardless of whether it's in binary or text mode.

Is there any way to get a file pointer to seek to a place in a text
file according to the number of characters, like the way read behaves
in text mode? This would be the simplest solution.

I guess one could say get program1 to store the snippet information in
terms of bytes and not number of characters. I'm not sure how to do
that. What it does is count characters, and on windows the newline is
still counting as 1 character. I guess I could add an extra byte every
time there is a windows-style newline, but I'm not even sure my
assessment that a windows newline = 1 char of 2 bytes is actually
true.

What would be a more elegant solution that would work on both
platforms?

Thank you for your help.
Jul 22 '05 #1
1 3572
"wtnt" <wt**@konzoo.com> wrote in message
news:4f**************************@posting.google.c om...
Hello.
I've searched all over and haven't seen another thread with this
problem. Please bear with me as I try to explain. thanks. :)

I have some programs that need to be cross-platform compatible (unix
and windowsXP). The first program parses a text file and records where snippets are in terms of where it begins (char offset from begin of
the file) and length (number of chars).

One can almost use "byte" and "char" interchangeably here, given that sizeof(char) is 1, however it doesn't quite work that way.


You simply can't store offsets into files portably in text mode. In
binary mode you can, if you remember that there may be an arbitrary
number of null characters added at the end of the file. See the
Dinkumware online documentation for an explanation.
(http://www.dinkumware.com/refxcpp.html. Go to the C++ table of
contents, and look under "Files and Streams"). Alternatively, see
P.J.Plauger's book on the C standard library.

Jonathan
Jul 22 '05 #2

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

Similar topics

0
by: adamsbarker | last post by:
i'm using Windows to access an ssh server. everything works perfect until i try executing files (ssh2_exec) and sending/receiving files (ssh2_scp_send/ssh2_scp_recv)... in the function manual at...
21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
1
by: vvenk | last post by:
Hello: I wanted to implement windows-based authentication. However when I run this aspx form, I do not see the user name and Authentication type. Can somebody tell me what I am doing wrong?...
1
by: Tim Marshall | last post by:
I'm putting together my first help file (using Easy Help, http://www.easyhelp.com/). So far, so good. I'm able to use the Help File and Help Context ID to have things from my help file pop up...
4
by: Dave | last post by:
Hi, Is there anyway to mimic forms authentication's loginUrl and RedirectFromLoginPage functionality using Windows authentication? We are developing intranet sites using basic authentication...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Thaisrgomes | last post by:
Hello all, I am writing a hands on test in MS acess 2003 , and I need to disable the user access tothe help file. HOw do I do that? I tryed to install acess adn diselecte the help file but I...
1
by: mattc66 via AccessMonster.com | last post by:
Hi All, I am going to use easy help to create my hlp files. I want to just have a button on my form that says HELP. I would like to then set it up so that I open the .hlp file. What would my...
2
by: charlesbritto | last post by:
A C++ program for counting individual string in a text file using file handling functions., for ex: if the text file contains, am a boy,am studying +2,am from chennai Now the result shoud...
2
jkmyoung
by: jkmyoung | last post by:
Does anyone know of good software to create a windows help file? .hlp There seems to be tons out there, but the ones lately I've tried so far aren't that great. Looking for both free and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.