What is the best way to read a file in C?
I have heard of using fread but then how do you find the size of the file?
This is my example code but it seems to have odd errors. -
char * read(char * filename){
-
FILE * fo;
-
-
fo = fopen(filename, "r");
-
if(fo){
-
char * buffer = NULL;
-
size_t result;
-
int length;
-
length = file_length(fo);
-
result = fread(buffer,1,length,fo);
-
fclose(fo);
-
return buffer;
-
} else {
-
fclose(fo);
-
return NULL;
-
}
-
}
-
5 2263
Well, it does indeed have errors. Check out this one. -
unsigned char* ReadFile(const char* szFileName, long& length);
-
-
int main(int argc, char* argv[])
-
{
-
long len = 0;
-
const char* filename = "c:\\test.txt";
-
unsigned char* buffer = ReadFile(filename, len);
-
-
if(buffer)
-
{
-
// Do something with the buffer
-
//.....
-
-
// And clean up after yourself
-
delete[] buffer;
-
buffer = NULL;
-
}
-
-
return 0;
-
}
-
-
unsigned char* ReadFile(const char* szFileName, long& length)
-
{
-
length = 0;
-
unsigned char* buffer = NULL;
-
FILE* fo = fopen(szFileName, "r");
-
-
if(fo)
-
{
-
fseek(fo, 0, SEEK_END);
-
length = ftell(fo);
-
fseek(fo, 0, SEEK_SET);
-
-
if(length)
-
{
-
buffer = new unsigned char[length];
-
fread(buffer, length, 1, fo);
-
}
-
-
fclose(fo);
-
}
-
-
return buffer;
-
}
-
You may also consider using the *_s fuctions (like fopen_s).
Cheers
You should be able to seek to the end of the file (SEEK_END) and then do a tell to get the offset from the beginning.
Banfa 9,065
Expert Mod 8TB
As long as you opened the file in binary mode, if you opened it in text mode then that is not a reliable method of getting the file size.
Does the buffer have to be unsigned char * ?
Can it be const char * ?
I am new to C, sorry.
Yes, it can be a const char* if you don't want to modify it. So, the line: -
unsigned char* buffer = ReadFile(filename, len);
-
would be: -
const unsigned char* buffer = ReadFile(filename, len);
-
And also change the return value of ReadFile(). I wrote it as unsigned char* again because it was how i did it in the first example. But if you want to use char* instead of unsigned char* it doesn't matter, since they are of the same size (1 byte), just interpreted differently. However, i don't know why do you want to use a char*. The manipulation of bytes is better with unsigned char, hence BYTE is defined as unsigned char.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Lokkju |
last post by:
I am pretty much lost here - I am trying to create a managed c++
wrapper for this dll, so that I can use it from c#/vb.net, however, it
does not conform to any standard style of coding I have seen....
|
by: AdrianH |
last post by:
Assumptions
I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming.
FYI
Although I have called this...
|
by: Atran |
last post by:
Hello:
In this article: You will learn to Write or Read A Text File.
Let's Begin:
First Create a new project (ConsoleApp or WinApp).
And Make sure your program uses these namespaces:
using...
|
by: Zach |
last post by:
I compiled a game client and it crashed (segmentation fault) resulting
in a core file being generated. I'm trying to find out exactly what
caused it to crash. Any ideas how I can do this with gdb?...
|
by: rohit |
last post by:
Hi All,
I am new to C language.I want to read integers from a text file and
want to do some operation in the main program.To be more specific I
need to multiply each of these integers with another...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |