473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

File reading

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.
Expand|Select|Wrap|Line Numbers
  1. char * read(char * filename){
  2.     FILE * fo;
  3.  
  4.     fo = fopen(filename, "r");
  5.     if(fo){
  6.         char * buffer = NULL;
  7.         size_t result;
  8.         int length;
  9.         length = file_length(fo);
  10.         result = fread(buffer,1,length,fo);
  11.         fclose(fo);
  12.         return buffer;
  13.     } else {
  14.         fclose(fo);
  15.         return NULL;
  16.     }
  17. }
  18.  
Jan 21 '09 #1
5 2263
l034n
15
Well, it does indeed have errors. Check out this one.

Expand|Select|Wrap|Line Numbers
  1. unsigned char* ReadFile(const char* szFileName, long& length);
  2.  
  3. int main(int argc, char* argv[])
  4. {
  5.     long len = 0;
  6.     const char* filename = "c:\\test.txt";
  7.     unsigned char* buffer = ReadFile(filename, len);
  8.  
  9.     if(buffer)
  10.     {
  11.         // Do something with the buffer
  12.         //.....
  13.  
  14.         // And clean up after yourself
  15.         delete[] buffer;
  16.         buffer = NULL;
  17.     }
  18.  
  19.     return 0;
  20. }
  21.  
  22. unsigned char* ReadFile(const char* szFileName, long& length)
  23. {
  24.     length = 0;
  25.     unsigned char* buffer = NULL;
  26.     FILE* fo = fopen(szFileName, "r");
  27.  
  28.     if(fo)
  29.     {
  30.         fseek(fo, 0, SEEK_END);
  31.         length = ftell(fo);
  32.         fseek(fo, 0, SEEK_SET);
  33.  
  34.         if(length)
  35.         {
  36.             buffer = new unsigned char[length];
  37.             fread(buffer, length, 1, fo);
  38.         }
  39.  
  40.         fclose(fo);
  41.     }
  42.  
  43.     return buffer;
  44. }
  45.  
You may also consider using the *_s fuctions (like fopen_s).

Cheers
Jan 21 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Jan 21 '09 #3
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.
Jan 21 '09 #4
Does the buffer have to be unsigned char * ?
Can it be const char * ?

I am new to C, sorry.
Jan 23 '09 #5
l034n
15
Yes, it can be a const char* if you don't want to modify it. So, the line:

Expand|Select|Wrap|Line Numbers
  1. unsigned char* buffer = ReadFile(filename, len);
  2.  
would be:

Expand|Select|Wrap|Line Numbers
  1. const unsigned char* buffer = ReadFile(filename, len);
  2.  
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.
Jan 23 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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....
1
AdrianH
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...
6
Atran
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...
2
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?...
13
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...
0
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...
0
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
0
BarryA
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...
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.