Solved

c++ - read unknown file

Posted on 2014-03-17
7
651 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

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 32

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

707 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

17 Experts available now in Live!

Get 1:1 Help Now