473,714 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparison issues using fstat and very large files in C++

5 New Member
Hey guys,
I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers.
In my program I need to compare the size of a given file against a software buffer of size 3MB. This is needed so as to see which function to use to read from the file. As the files used range from very large (>30GB) to very small (<3MB), I have enabled large file support and I obtain the file size by using the fstat function.

I make sure that large file support is enabled in the header file
Expand|Select|Wrap|Line Numbers
  1. // Larger file support for files greater then 20 gigabytes
  2. #ifndef _LARGEFILE_SOURCE
  3. #define _LARGEFILE_SOURCE
  4. #endif
  5. #ifndef _LARGEFILE64_SOURCE
  6. #define _LARGEFILE64_SOURCE
  7. #endif
  8.  

The code that I am using to compare the files is this
Expand|Select|Wrap|Line Numbers
  1. #define BUF_SIZE   0x300000
  2. ..
  3. ..
  4. ..
  5. ..
  6. ..
  7. struct stat  statbuf;
  8. fstat(dat_fd, &statbuf); //where dat_fd is the file handler
  9.  
  10. off64_t file_size = (off64_t)statbuf.st_size;
  11. off64_t temp_buf_size = (off64_t) BUF_SIZE; //software buffer
  12. if(temp_buf_size > file_size)
  13. {
  14.      small_flag = true; //ie 1
  15. }
  16. else
  17. {
  18.      small_flag = false; //ie 0
  19. }
  20.  
To verify the workings of this comparison and code section, I write out the information to a debug file using this code and unsigned long long identifiers
Expand|Select|Wrap|Line Numbers
  1. Debug::writeDebug("file comparison results -> BUF_SIZE =%llu file_size =%llu small_flag =%d\n", 
  2. temp_buf_size, file_size, small_flag);
  3.  
This works fine for files in the 1-10MB range but fails completely for larger files in the 2-3GB range. Instead of the file size i expect to be printed, i get nonsensical answers and incorrect comparison results.

Here are the debug outputs:
for file size = 1024000 bytes
Expand|Select|Wrap|Line Numbers
  1. file comparison results -> BUF_SIZE =3145728 file_size =1024000 small_flag =1
  2.  
for file size = 10240000 bytes
Expand|Select|Wrap|Line Numbers
  1. file comparison results -> BUF_SIZE =3145728 file_size =10240000 small_flag =0
  2.  
for file size = 3659983208 bytes
Expand|Select|Wrap|Line Numbers
  1. file comparison results -> BUF_SIZE =3145728 file_size =18446744073074567528  small_flag =1
  2.  
As you can see, my typecasting and comparison fails for large files somewhere. The comparison behaves properly if I use unsigned long (32-bit) instead of unsigned long long (64-bit / off64_t) but some of the files I use are greater than 4GB (the limit of an 32-bit number), thus I can't use it. I've double checked the fstat function and st_size is definitely either an off_t or off64_t, depending on whether large file support has been enabled (assuming that off_t = 32-bits and off64_t = 64-bits)

Does anyone have any idea why this simple comparison is not working?

Thanks
Lars
May 8 '07 #1
1 6318
Lars B
5 New Member
After much searching through the code and a lot of debug statements, I have finally solved this problem. It turned out that the size of the value returned by off_t via the fstat function was only 32-bits, not the expected 64-bits as it would be if 64-bit support was enabled.

I checked through my makefile and it seems to show that it is being enabled correctly

Expand|Select|Wrap|Line Numbers
  1. CFLAGS  = -D_FILE_OFFSET_BITS=64 -Wall -Iadmxrc2 -Iadmxrc2/common -Iinclude
  2.  
  3. LDFLAGS =  -L. $(LIBPATH)
  4.  
  5. SRCS =    Debug.cpp \
  6.                 FileReader.cpp \
  7.                 Monitor.cpp \
  8.                 Control.cpp \
  9.                 Main.cpp
  10.  
  11. all: main
  12.  
  13. main: libcommon.a libnet.a $(SRCS:.cpp=.o)
  14.  
  15.       $(CXX) $(LDFLAGS) $(CFLAGS) $(SRCS:.cpp=.o) -lccgnu2 -ldl -ladmxrc2  -o fpc_main -L"./lib" -lcommon -lnet
  16. libnet.a:
  17.         make -C ./lib/net -f net.mak
  18.  
  19. libcommon.a: args.o time.o
  20.         ar r ./lib/libcommon.a args.o time.o
  21.  
  22. args.o: admxrc2/common/args.c admxrc2/common/common.h
  23.         $(CC) -c $(CFLAGS) -o $@ admxrc2/common/args.c
  24.  
  25. time.o: admxrc2/common/time.c admxrc2/common/common.h
  26.         $(CC) -c $(CFLAGS) -o $@ admxrc2/common/time.c
  27.  
  28. clean:
  29.         rm -f $(TARGET) *.o *.a
  30.         make -C ./lib/net -f net.mak clean
  31.  
  32.  
But upon a clean compilation of the files, I noticed that 64-bit support was enabled for the C functions but not the C++ functions

[code]
gcc -c -D_FILE_OFFSET_B ITS=64 -Wall -Iadmxrc2 -Iadmxrc2/common -Iinclude -o args.o admxrc2/common/args.c
gcc -c -D_FILE_OFFSET_B ITS=64 -Wall -Iadmxrc2 -Iadmxrc2/common -Iinclude -o time.o admxrc2/common/time.c
ar r ./lib/libcommon.a args.o time.o
make -C ./lib/net -f net.mak
make[1]: Entering directory `/home/lib/net'
cc -c -o net_endpt.o net_endpt.c
cc -c -o net_error.o net_error.c
cc -c -o net_io.o net_io.c
cc -c -o net_tcp.o net_tcp.c
ar rv ../libnet.a net_endpt.o net_error.o net_io.o net_tcp.o
ar: creating ../libnet.a
a - net_endpt.o
a - net_error.o
a - net_io.o
a - net_tcp.o
make[1]: Leaving directory `/home/lib/net'
g++ -c -o Debug.o Debug.cpp
g++ -c -o FileReader.o FileReader.cpp
g++ -c -o Monitor.o Monitor.cpp
g++ -c -o Control.o Control.cpp
g++ -c -o Main.o Main.cpp
[\CODE]

To rectify this, I specified that the offset to be used was 64-bits in the FileReader.h file, thus making certain that all applicable functions called would return 64-bit values, not 32-bits.

Expand|Select|Wrap|Line Numbers
  1. #ifndef FILEREADER_H_
  2. #define FILEREADER_H_
  3.  
  4. // Larger file support for files greater then 20 gigabytes
  5. #ifndef _LARGEFILE_SOURCE
  6. #define _LARGEFILE_SOURCE
  7. #endif
  8. #ifndef _LARGEFILE64_SOURCE
  9. #define _LARGEFILE64_SOURCE
  10. #endif
  11.  
  12. #define _FILE_OFFSET_BITS   64 //definition of offset size
  13.  
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <features.h>
  17. #include <ctype.h>
  18. #include <cc++/thread.h>
  19.  
This fixed the problem immediately. But I feel that this solution is inelegant, as I would prefer that the Makefile handles the large file support for bith the C files and the C++ files. How does one ensure that the file_offset is 64 for the C++ files as well as the C files in compilation? What must I include in the Makefile so that all the C++ files use large file support?

Thanks, Lars
May 10 '07 #2

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

Similar topics

5
2832
by: democratix | last post by:
Hi, I've only got a couple years experience developing for Access but have recently been experimenting with HTML/javascript for gui and client-side scripting, mysql for database and php for server-side scripting. I've been running it all on the development machine until the application I'm building is advanced enough to start optimising/testing with network lag in mind. So it's no slower, and in some ways at least it seems faster.
5
5687
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere 4.0, SourceOffSite 4.1, VSS Connect 1.5, SourceXT 2.1, VSS Remoting 2.5,
2
4888
by: Martijn | last post by:
Hi, I have an open file handle from which I would like to retrieve the file descriptor for use with fstat. Is this possible or should I redesign my algorithm a bit (I have the feeling I am overlooking something trivial here)? Thanks, Martijn
20
6809
by: Daniel Billingsley | last post by:
I read this fantastic article this morning, which raises some interesting points... like, for example, the TREMENDOUS performance advantage of writing code with using() as opposed to just letting the garbage collection do it's thing. The author appears to be quite neutral, so this is not a "C# is better than java" type of post. In fact, he is pretty brutal to C# (that it apparently deserves) on several points. Skeet, you ought to love...
7
13539
by: bcutting | last post by:
I am looking for a way to take a large number of images and find matches among them. These images may not be exact replicas. Images may have been resized, cropped, faded, color corrected, etc. Approach 1 Programmatically extract the information (such as Eigen Vectors/Eigen Spaces) and store them in a database. Then apply a comparison algorithm between the database entries to find like images. Approach 2
0
2505
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com http://www.livingcosmos.org/Members/sundevil/python/articles/a-comparison-of-python-class-objects-and-init-files-for-program-configuration/view
1
2642
by: ansc1 | last post by:
Hello, I'm new to using php coding. I need help with the following: 1. There is a submit button on the form and is saves information to my database. After clicking on "Save Measurement" it redirects me to another page in my site. What I would like to do is change what page directs it to. Currently the submit button redirects me to page /measure/men_measure. I would like to be able to change this. Please see below my page below:...
16
3664
by: yu_kuo | last post by:
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a systematic way?
11
5145
by: solarisss | last post by:
I have thousands of files whose exitence needs to be checked. I think fstat is too costly for this. Is there any better way for the same ?
0
8801
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9174
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9015
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.