Thread: a few questions about this file i/o program

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    79

    a few questions about this file i/o program

    i wrote this and it compiles with no errors or warnings. Works too, but having some small problems.

    1) It segfaults at the very end after executing the last printf and calling fclose.
    2) It lists the number of bites as 9 - even thought the filesize is greater than that.
    3) why isnt it displayed in binary as that is how im reading the file


    here is the code
    Code:
    #include <stdio.h>
    
    int main (int argc, char *argv[]){
    
    	FILE *p = fopen(argv[1],"rb");
    	char buff[256];
    	int  i = 0;
    	int bytes = 0;
    	while(1){
    	
    		bytes = fread(buff,sizeof(buff),256,p);
    		printf("Packet %d :\nDump: %s\n\n",i,buff);
    		i++;
    		if(feof(p))
    		break;
    
    
    		
    	
    	}
    
    	printf("Finished Reading From file.\n Total Bytes : %d\n",bytes);
            //prints out printf then segfaults.
    	fclose(p);
    	return 0;
    
    
    }

    OUTPUT:

    Code:
    brian@brian-unix-box:~/c$ ./read writers.php
    Packet 0 :
    Dump: <?PHP
    SESSION_START();
    //Writers.php
    //
    
    include 'includes/top.php';
    include 'includes/header.php';
    include 'functions.php';
    include 'conf.php';
    include 'opendb.php';
    
    echo "<div class = 'writersmain'>";
    //should display like following
    //name of writer as link to profile
    //threads that they have started
    
    
    //div to css
    //adb
    //adbtName
    //adbhdr
    
    if(isset($_GET['id'])){
    $r = protect($_GET['id']);
    
    
        
         
        $val2 = "SELECT * FROM tbl_article WHERE pID = " .$r;
        $res2 = mysql_query($val2) or die (mysql_error());
        if(mysql_num_rows($res2) < 1){
        
        echo "<div class = 'adb'><div class = 'adbtName'>This user has no Articles</div></div>";
        
        }
        
        else{
          
           $nq = "SELECT * FROM tbl_users WHERE USERID = " .$r;
           $resnq = mysql_query($nq) or die (mysql_error());
           $arrnq = mysql_fetch_array($resnq);
           
           echo "<div class = 'adbhdr'>All Articles Posted By: ".$arrnq['uname']."</div>";
          while($row2 = mysql_fetch_array($res2,MYSQL_ASSOC)){
          echo "<div class = 'adb'>
                <div class = 'adbtName'>
                <a href = 'articles.php?id=" .$row2['ArticleID']."'>
                ".$row2['ArticleTitle']."</a></div>
                </div>";}
          
        
        }
    
    
    
    
    
    }
    
    else{
    echo "<h3>Search For Short Stories, Rants and Reviews By Author</h3>";
    $val = "SELECT * FROM tbl_users";
    $res = mysql_query($val) or die (mysql_error());
    echo "<form action = 'writers.php' method = 'GET'><SELECT NAME = 'id'>";
    while($row = mysql_fetch_array($res,MYSQL_ASSOC)){
    
         echo "<option value = '".$row['USERID']."'>".$row['uname'];
         /*echo "<div class = 'adbhdr'>".$row['uname']."</div>";
         
        $val2 = "SELECT * FROM tbl_article WHERE pID = " .$row['USERID'];
        $res2 = mysql_query($val2) or die (mysql_error());
        if(mysql_num_rows($res2) < 1){
        
        echo "<div class = 'adb'><div class = 'adbtName'>This user has no Articles</div></div>";
        
        }
        
        else{
          
          while($row2 = mysql_fetch_array($res2,MYSQL_ASSOC)){
          echo "<div class = 'adb'>
                <div class = 'adbtName'>
                <a href = 'articles.php?id=" .$row2['ArticleID']."'>
                ".$row2['ArticleTitle']."</a></div>
                </div>";}
          
        
        }*/
    
    
    }
    
    echo "</SELECT>";
    echo "<input type = 'submit'value = 'Get Articles'></form>";
    
    
    }
    
    
    
    echo '</div>'; //ending cc di
    include 'includes/footer.php';
    
    
    
    
    ?>
    
    
    Finished Reading From file.
     Total Bytes : 9
    Segmentation fault
    brian@brian-unix-box:~/c$ ./read writers.php
    Last edited by Salem; 04-19-2010 at 11:31 AM. Reason: Bored with fixing code tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fread(buff,sizeof(buff),256,p);
    Because fread() MULTIPLIES the two middle parameters.

    You just stuffed 64K into your 256 byte buffer.


    Try
    Code:
    while ( (bytes = fread(buff,sizeof(char),sizeof(buff)-1,p)) > 0 ) {
        buff[bytes] = '\0';
        // now you have something to safely pass to priintf %s
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    ah ok thanks i understand now.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    is it still coming across in binary if i wanted to transfer something meaningfull like a picture or a compiled file would this method of transfer work?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Sure - but you should probably send the length first, then the data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM