Solved

c++ - read unknown file

Posted on 2014-03-17
7
687 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 clear a vector as well as how to detect empty vectors in C++.

761 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