Solved

c++ - read unknown file

Posted on 2014-03-17
7
656 Views
Last Modified: 2014-03-24
Hi,

I need help on how to read unknown file format. I can open that file format using Notepad and I can see the content. I tried to read file in way like we read text file but I cannot.

Please let me know how can I read unknown files which I can open with Notepad.

Thanks
0
Comment
Question by:josephm67
7 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39935783
Perhaps you need to open the file in binary mode. Can you show us the screenshot of how the file looks like in Notepad?
0
 
LVL 24

Expert Comment

by:chaau
ID: 39935787
BTW, you have specified the following tags and topics:
Tags: c++, visual studio, eclipse
Topics: C++ Programming Language, Microsoft Visual C++.Net

What exactly is your language of choice? Is it pure C++, MFC or C++/CLI (i.e. .Net)?
0
 

Author Comment

by:josephm67
ID: 39936379
In Notepad, file show correct text like "This is line 1", This is line 2. The real english characters.

Any help please..
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:josephm67
ID: 39936381
I am using Visual Studio for C++ but I am trying to use pure C++.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 39936401
Hi josephm67,

could you post the code you wrote so far? And it would be great if you could post a sample file here too.

Best regards,

ZOPPO
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 39936976
different software programs encode characters in different ways. notepad can manage text encoded in several formats such as ANSI, Unicode (UTF-16) and UTF-8. find these options by clicking the "Encoding" button on notepad's 'Save As' window.

if it is ansi, you would open and read it in c++ as text like

std::string strfile = "c:\\temp\\myfile.xyz";
std::ifstream ifs(strfile.c_str());
std::string line;
while (std::getline(ifs, line))
{
    // do something with text line
    ...
}

Open in new window


if UTF-16, each character has two bytes and you would read it accordingly.

you would then use std::wstring instead of std::string and std::wifstream indtead of std::ifstream. literals like "c:\\temp\\file" would get preceeded by an L.

if UTF-8 it is a single-byte encoding like with ANSI. so you would use the code above but then have to convert from UTF-8 to ANSI or UTF-8 to UTF-16 if the line contains UTF-8 characters which use multiple bytes for all codes above ascii 7-bit. code to convert from UTF-8 to wide strings is like

const std::locale empty_locale = std::locale::empty();
typedef std::codecvt_utf8<wchar_t> converter_type;
const converter_type* converter = new converter_type;
const std::locale utf8_locale = std::locale(empty_locale, converter);
std::wifstream stream(L"test.txt");
stream.imbue(utf8_locale);
std::wstring line;
std::getline(stream, line);

Open in new window


Sara
0
 

Author Closing Comment

by:josephm67
ID: 39951070
thx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now