There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Reading strings from unicode file using getline in c++


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
dev_tyagi's Avatar
Junior Member with 2 posts.
 
Join Date: Jul 2005
Experience: Intermediate
02-Jul-2005, 03:11 AM #1
Reading strings from unicode file using getline in c++
hi everyone
i am using C++ for reading unicode formated file i have to read strings to a specified deliminator but this funcion is not working poperly with unicode file
so please help me out to do so
the code i m writing for reading string is
fstream fp,fp1;
fp.open("filename",ios::in);
if (!fp.is_open())
{
cout << "couldn't open file" << endl;
exit(3);
}


else
while(!fp.eof())
{
fp.getline(buffer,2500,'¶');
fp1.open("c:\\notfounddata2.txt",ios:ut);
fp1.write(buffer,2500);
cout<<buffer;
}
fp.close();
fp1.close();
but the prob is its not reading the file properly
so what should i do i m new to c++

with regards
dev
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
02-Jul-2005, 03:07 PM #2
I'm a C (not C++) programmer, but doesn't getline() read only until it encounters a newline character (or CR/LF for DOS)?

Also, I see a problem in that you are opening your output file inside the loop and closing it outside the loop. Open it once and close it once.
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
02-Jul-2005, 09:15 PM #3
^^ only with non-member getline().

For member getline(), you can specify how many characters to read and specify the delimeter.

@dev_tyagi
If it was a regular file, you could just use 182 or 0xB6 for the getline delimiter, but not sure about unicode.

For visual c++, I think you can use wfstream, but not sure.
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run
dev_tyagi's Avatar
Junior Member with 2 posts.
 
Join Date: Jul 2005
Experience: Intermediate
04-Jul-2005, 05:37 AM #4
HI
THANKX FOR UR REPLY
BUT THAT WAS JUST A SAMPLE CODE SO BY MISTAKE I HAD WRITTEN LIKE THAT ACTUALLY I WAS WRITTEN LIKE THIS BUT ITS NOT READING
char buffer[300][2500];
fstream fp,fp1;
int i=0;
fp.open("c:\\notfounddata1.txt",ios::in|std::ios::binary);
fp1.open("c:\\notfounddata2.txt",ios:ut|std::ios::binary);
if (!fp.is_open())
{
std::cout<<"couldn't open file" << endl;
exit(3);
}

else
while(!fp.eof())
{
fp.getline(buffer[i],2500);
fp1<<buffer[i]<<endl;
std::cout<<buffer[i]<<endl;
i++;
}


fp.close();
fp1.close();
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
05-Jul-2005, 03:05 AM #5
Still not sure about the unicode part and getline, but you can ask here and you'll get your answer.

A couple of tips though.

instead of

fstream instance1, instance2
instance1.open("file.txt", ios::in | ios::binary);
instance2.open("file.txt", ios:ut | ios::binary);


you can do

ifstream instance1("file.txt", ios::binary);
ofstream instance2("file.txt", ios::binary);

Also, you shouldn't use exit(). Destructors for anything won't be called. In main() when you want to exit with an error, use return so the destructors get called.

Here's just a simple text file copier as an example.

Code:
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
    ifstream in("in.txt");
    if (!in) {
        cout << "\n" << "Error reading in.txt" << endl;
        return 1;
    }
    ofstream out("out.txt");
    if (!out) {
        cout << "\n" << "Error writing to out.txt" << endl;
        return 1;
    }
    for (string s; getline(in,s) ; ) {
        out << s;
        if ( !in.eof() ) {
            out << "\n";
        }
    }
}
In this case, I don't have to .close() the streams as they will be destructed when main() reaches the end of its scope. Now for example, if the stream is open and I need to delete the file, I'd need to .close() it, but instead of using .close(), it's usually better to put the stream operations in a function and let the stream be closed when the function reaches the end of its scope.
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
05-Jul-2005, 04:51 AM #6
I did figure this out though.


Code:
#include <iostream>
#include <fstream>

using namespace std;

int main() {
    // Write  ¶ to file in unicode
    ofstream out("out.txt", ios::binary);
    if (!out) {
        return 1;
    }
    out << static_cast<char>(0xFFFFFFFF) 
        << static_cast<char>(0xFFFFFFFE) 
        << static_cast<char>(0xFFFFFFB6) 
        << static_cast<char>(0);
}

When you save in unicode with EditPlus for example , each char is 32bits long or 16bits long depending. So if "&" is the first character in the file, the first four characters you get when you read the file will make up the bits of &.

Are you talking about utf-8, utf-16 or utf-32 specifically?

If utf-8, you'd write ¶ to a file like this.

Code:
out << static_cast<char>(0xFFFFFFC2);
out << static_cast<char>(0xFFFFFFB6);
That'll give you some hints, but I would ask at that link I posted.

(You don't really need to use binary mode though)
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run

Last edited by Shadow2531 : 06-Jul-2005 01:09 AM.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:06 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.