Click here to Skip to main content
15,921,548 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to read a .asc file and .dbc file, can anyone suggest me how to read them in windows form using c++.

What I have tried:

i tried searching but not getting correct info
Posted
Updated 28-Nov-17 2:49am
Comments
Member 13475664 28-Nov-17 8:49am    
can i get a code example

1 solution

You read them as binary files like any other file using for example the Windows API functions CreateFile / ReadFile or the C standard library fopen / fread functions. Before reading you have to determine the file size and allocate a buffer of that size plus one. The additional byte is for a trailing NULL byte to be appended after reading because both file types are plain text files.

Aftwerwards you can access the data as char* strings.

Note that opening in binary mode is required because only then the data are read as they are. Otherwise non-MS text files (LF only terminated lines) would be converted to CR - LF terminated lines making the allocarted buffer too small.

[EDIT]
Quote:
can i get a example code for that
C++
#include <sys/stat.h>

struct stat st;
FILE *f = fopen("test.asc", "rb");
if (NULL != f)
{
    fstat(_fileno(f), &st);
    char *lpszFileData = new char[st.st_size + 1];
    fread(lpszFileData, 1, st.st_size, f);
    fclose(f);
    lpszFileData[st.st_size] = 0;
    // Use lpszFileData here

    // Delete when no longer needed
    delete[] lpszFileData;
}

[/EDIT]
 
Share this answer
 
v2
Comments
CPallini 28-Nov-17 8:52am    
[On behalf of Member 13475664]
Can I get sample code for that?
Jochen Arndt 28-Nov-17 9:04am    
Done.

Thank you for the notification.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900