473,378 Members | 1,396 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,378 software developers and data experts.

setbuf does not seem to work.

Hi all,

I'm trying to understand how setbuf works and came across this from google for setbuf.
http://www.cplusplus.com/reference/clibrary/cstdio/setbuf/
With fully buffered streams, writing operations are not intended to be written directly to the device associated with them; the data is accumulated in the buffer and written to the device as a block when it is filled.

I have written a simple c program to accomplish the above, but the code does not seem to work. read_char [line 65 and 67 does not read the character from the file after the buffer is full. Can someone please help me to correct my understanding?

The following is the logic:
Step1 : Open file for read and write i.e "w+"
Step2 : setbuf for buffer of size 5 bytes i.e setbuf(fp, buffer)
Step3 : Keep writing a character 'A' until the buffer is full in a for loop.
Step 3.1 : Write a char 'A' to file
Step 3.2 : Get the cur file position
Step 3.3 : rewind and read a char
Step 3.4 : if the char read in 3.3 is 'A', return as the program fails.
Step 3.5 : Set the file pointer back to cur_pos
Step4 : Write one more char 'A' to the file after the buffer is full
Step5 : Read the char from the file, after rewinding. The read_char should have a 'A'.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. char* buffer = NULL;
  7. #define BUF_SIZE            5    
  8. int counter  = BUF_SIZE;
  9. const char write_char = 'A';
  10. char read_char;
  11. fpos_t cur_pos = -1;
  12.  
  13. FILE * fp = fopen( "test", "w+" );
  14. if( !fp )
  15. {
  16.     return 1;
  17. }
  18.  
  19. buffer = (char *)malloc(BUF_SIZE);
  20.  
  21. if( !buffer )
  22. {
  23.     return 1;
  24. }
  25.  
  26. setbuf( fp, buffer );
  27.  
  28. /* Keep writing until the buffer is full */
  29. for ( ; counter ; counter-- )
  30.     {
  31.         /* Write character 'A' to the file */
  32.         if( fputc( write_char, fp ) == EOF )
  33.             {
  34.                 /* writing char failed */
  35.                 return 1;
  36.             }                
  37.  
  38.          /* remember the file pointer position */               
  39.           fgetpos ( fp, &cur_pos );
  40.  
  41.           rewind( fp );
  42.  
  43.           read_char = fgetc( fp );
  44.  
  45.           if( 'A' == read_char )    
  46.           {
  47.                  return 1;
  48.           }
  49.  
  50.         /* Set the file pointer to the position it was last written */     
  51.          fseek( fp, cur_pos, SEEK_SET );    
  52.     }
  53.  
  54. /* Counter is 0 here and hence the buffer is full */
  55.  
  56. /* Write character 'A' to the file , after the buffer full condition */
  57. if( fputc( write_char, fp ) == EOF )
  58.      {
  59.            /* writing char failed */
  60.            return 1; 
  61.      }              
  62.  
  63. fgetpos ( fp, &cur_pos );
  64. rewind(fp);
  65. read_char = fgetc( fp );fseek( fp, cur_pos, SEEK_SET );
  66.  
  67. if( 'A' != read_char )    // I'm expecting read_char to have 'A' as the buffer is full 
  68.      {
  69.          return 1;
  70.      }
  71. fseek( fp, cur_pos, SEEK_SET );
  72. return 0;
  73. }
Jan 26 '10 #1
1 2254
Banfa
9,065 Expert Mod 8TB
You have not read you own reference correctly (or fully). setbuf assumes that the buffer passed to it is BUFSIZ bytes long. BUFSIZ is defined in stdio.h, on my version of MinGW and in VC++ 2008 it is defined to 512 which is not too surprising as that is the size of a harddisk sector.

By defining your buffer to be 5 bytes long all you have done in invoked undefined behaviour as the streaming routines will have assumed that you passed a buffer of the correct size (512 bytes) and will be writing outside the bounds of your array.
Jan 27 '10 #2

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

Similar topics

5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
7
by: Jenbo | last post by:
I know this has come up frequently but I can't seem to put my finger on the precise problem as it applies to me. I have a workgroup with a 2003 box with Sql server on it. Also, on this, I have an...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
9
by: kernelxu | last post by:
hi, everyone. now, I'am confused on such a problem: function setbuf(stdin, NULL) or setvbuf(stdin, NULL, _IONBF, 0) can set the stadard input stream unbuffered. however, why does my program...
4
by: Uchiha Jax | last post by:
Hello everyone, I am a plenty silly person who is trying to learn .NET remoting through trial and error (all articles I read are going over my head at the moment (mostly) so I thought i'd give...
5
by: tindog | last post by:
I seem to be caught in a bit of a conundrum with C#. First of all getting books on VS 2003.net then VS 2005 comes out and further a book I have bought to just learn just the language C# (in 21...
3
by: nsh | last post by:
mailing.database.mysql, comp.lang.php subject: does "LOAD DATA" EVER work?!? I've tried EVERYTHING! version info: my isp is running my web page on a linux box with php ver. 4.4.1 according to...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
1
by: kevin bailey | last post by:
I have used the PEAR Auth package to successfully set up authentication. <code> // Details of where the authentication details are stored. $options = array( 'dsn'...
19
by: William Gill | last post by:
I seem to be having a mentally bad period lately . My code is beginning to be terrible convoluted mess, and I shudder to think what it will be like to go back in a couple months and try to follow...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.