473,569 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File Pointers - geting a logical problem..

11 New Member
Hello,

I'm facing a big logical problem while writing a parser in VC++ using C.

I have to parse a file in a chunk of bytes in a round robin fashion.
Means, when I select a file, the parser will read first 512kb(IBUFFSIZE ) of data, then move to next file and parse the same way. This way I can parse a number of file spreaded over different directory uniformly.

I'm keeping a meta data in a file where I'm keeping the track of file parse and the size of file parse.

Now, I'm using fseek() function I'm moving the file pointer.

Expand|Select|Wrap|Line Numbers
  1.  // if the file is parsing for the first time 
  2. if ( TotalFileSize > IBUFFSIZE){
  3.    fseek(in_file_pointer,IBUFFSIZE,SEEK_SET);
  4.    FileSizeToParse = ftell(in_file);
  5. }
  6.  
  7. //if the file is parsing for the second time
  8. FileSize = TotalFileSize - AlreadyParsedFileSize;
  9.  
  10. if ( FileSize > IBUFFSIZE){
  11.    fseek(in_file_pointer,(AlreadyParsedFileSize+IBUFFSIZE),SEEK_SET);    
  12.    FileSizeToParse = ftell(in_file) - AlreadyParsedFileSize;
  13. }
  14.  
  15. /* setting the file to the position from where to parse [for the first time its 0, in the second pass it will be the value thats already parse] */
  16.  
  17. fseek(in_file,AlreadyParsedFileSize,SEEK_SET);
  18.  
  19. // the loop to read data in buffer and parse the data in memory
  20. while ((EOFFLAG=fgets(ibuff, FileSizeToParse , in_file_pointer))!= NULL) {
  21.        /// parsing logic come here
  22.  }
  23.  
The PROBLEM with this logic is First time its parsing the chunk of data its parsing ok..
But when the file pointer is moving to the DataAlreadyPars ed and then fetching data from the file with fgets(), its retrieving the entire chunk of data from the beginning of the file to the location its specified. i.e. instead of stating from AlreadyParsedFi leSize to IBUFFSIZE, its taking FileBeginning to AlreadyParsedFi leSize+IBUFFSIZ E.

Is there any method of specifying the From Byte size and To Byte Size in fgets() function. Because for this bug the parser is parsing data that already been parsed. I'm getting duplicate data, and its the number of duplication is the number of times the file is been read.

Can anyone suggest/advice me how to get this thing done. As I'm using windows OS (VC++), I cant use much in built c function in file operation.

I have got a lot of solution form this site.. that helped me to build this parser, so I hope this time also I'll get a solution to this nagging bug.




Thanks
SouravM
Jul 6 '07 #1
7 2841
weaknessforcats
9,208 Recognized Expert Moderator Expert
Why are you not just reading the file in 512 byte chunks??
Expand|Select|Wrap|Line Numbers
  1. char*  rval = fgets(char *str, IBUFFSIZE,in_file_pointer))
  2. if (rval)
  3. {
  4.      /* parse file here */
  5. }
  6.  
  7.  
  8.  
All you need is a FILE* for each of your files and have this inside a function that returns rval. When 0 is returned, you are at the end of that file.
Jul 6 '07 #2
souravmallik
11 New Member
Why are you not just reading the file in 512 byte chunks??
Expand|Select|Wrap|Line Numbers
  1. char*  rval = fgets(char *str, IBUFFSIZE,in_file_pointer))
  2. if (rval)
  3. {
  4.      /* parse file here */
  5. }
  6.  
  7.  
  8.  
All you need is a FILE* for each of your files and have this inside a function that returns rval. When 0 is returned, you are at the end of that file.
Thanks for pointing out the problem!!

Actually I'm parsing some data which is delimited by New Line Character.So I'm using the while loop using the fgets() function.

The fgets function reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.

I had a impression that IBUFFSIZE that I'm using for chuck read, will read that much data from the file, but now I realize that the fgets function is setting ibuff variable till the new line character is found.

So the code you suggested won't work for me. I need to get something that will get a chunk of data in a buffer and then within the buffer I can use fgets() to scan each data which is new line terminated record.

Can you advice how to do that..

Thanks for understanding my problem...
Regards
Sourav
Jul 9 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
So the code you suggested won't work for me. I need to get something that will get a chunk of data in a buffer and then within the buffer I can use fgets() to scan each data which is new line terminated record.
fgets() will fetch characters until a \n. This may overrun your buffer. You can't have ot both ways. Either you read 512 bytes or you read a string of any length.

If in fact, these are strings and the length canbe anything, I would use a nominal buffer that is guaranteed to hold 85% of the strings. Then I would call getch() until the buffer was full or I encountered a \n. Then I would call strcat() and refill the buffer until I reached the \n.

fgets() will require your buffer to be larger than the string by at least 2 bytes. It puts the \n in the buffer followed by a \0.
Jul 9 '07 #4
souravmallik
11 New Member
fgets() will fetch characters until a \n. This may overrun your buffer. You can't have ot both ways. Either you read 512 bytes or you read a string of any length.

If in fact, these are strings and the length canbe anything, I would use a nominal buffer that is guaranteed to hold 85% of the strings. Then I would call getch() until the buffer was full or I encountered a \n. Then I would call strcat() and refill the buffer until I reached the \n.

fgets() will require your buffer to be larger than the string by at least 2 bytes. It puts the \n in the buffer followed by a \0.

Thanks for answering... I have cope up with my problem somehow..

What I'm doing is:
I'm scanning the whole file with fgets() with a minimum buffer, then for implementing the 'chunk read', I'm using ftell() function to get the size of file after every fgets(), when the file size is equal to 512kb (or any other chunk value set before) its breaking from the loop and continue parsing the next file.

Will the ftell(), make my program slower, as its called every time in the loop (theoretically) ?

This logic is working for me... can you advice anything else..

Thanks again..

Sourav
Jul 10 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
Will the ftell(), make my program slower, as its called every time in the loop (theoretically) ?
Forget issues of fast or slow until the program is working. And keep in mind an issue a 1 MHZ is 30% of an issue at 3 MHZ.

Concentrate on bug-free operation. Leave the optimization until it is absolutely needed.
Jul 10 '07 #6
r035198x
13,262 MVP
Forget issues of fast or slow until the program is working. And keep in mind an issue a 1 MHZ is 30% of an issue at 3 MHZ.

Concentrate on bug-free operation. Leave the optimization until it is absolutely needed.
I don't wish you to scare you weakness, but you may want to know that there are some cats looming close by.
Jul 10 '07 #7
souravmallik
11 New Member
Forget issues of fast or slow until the program is working. And keep in mind an issue a 1 MHZ is 30% of an issue at 3 MHZ.

Concentrate on bug-free operation. Leave the optimization until it is absolutely needed.

Yea my program is working fine... and i'm getting what I was looking for..
Thanks.
Jul 11 '07 #8

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

Similar topics

14
2449
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a "counted" string and data in a binary file. Given... #include <stdio.h> int main(int argc, char *argv) { char bstring;
51
2505
by: Richard Hengeveld | last post by:
Hi all, I'm trying to understand how pointers for function parameters work. As I understand it, if you got a function like: void f(int *i) { *i = 0; }
5
1893
by: DraguVaso | last post by:
Hi, I need a SECURE way to copy parts of a file. I'm having files which contains a whole bunch of records. In one 'fysical' file I'm having one or more logical files. What I need to do is to copy a logical file (a part of the fysical file) into a new file. But the 'big' problem is: these records contains bankstatements, so I can't take...
12
4070
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and VB.NET get compiled into the same code, then I don't understand why VB.NET can't support pointers Thanks for any info Lance
14
2811
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of contents, use the "Bookmarks" tab in Adobe Acrobat. Comments, corrections, praise, nits, etc., are still welcome!
39
19581
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
3
6091
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never learned file input/output very well (I self taught all the programming I know...and my c++ book was not good) and so I'm not sure what's wrong with this....
4
3285
by: solarrobor | last post by:
I am currently porting a program from C to C++. The program needs to read the file "/proc/bus/usb/devices". I am able to open and read from the file using FILE but not when I use fstream. Why does it work with FILE and not fstream? What is the solution to making it work using fstream? I am using GCC 4.2.3 on Fedora 8
11
4929
by: krreks | last post by:
Hi. I'm trying to figure out pointers and memory allocation in C. I've read quite a few explanations on the internet and have read in a few books that I've got, but I'm not so much smarter yet... What I want do do is to create an array of five pointers to characters... I want to make it possible to assign a pointer to the params array...
0
7695
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...
0
7612
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...
0
7922
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. ...
0
8119
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...
1
7668
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
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
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.