Solved

c++ - read unknown file

Posted on 2014-03-17
7
668 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.

777 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