Solved

c++ - read unknown file

Posted on 2014-03-17
7
713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 25

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 25

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 34

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

635 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