Thread: Segmentation fault why, adn general functionality problems

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Segmentation fault why, adn general functionality problems

    So I have this code, and its giving me problems. In the first line of output, it displays how I want it to, after which it screws up. It's supposed to display 16 characters, then those 16 characters in hex. Oh by the way, I am using Linux.
    Code:
    #include <unistd.h> 
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(int argc, char** argv) {
    int fd;
    size_t linesize=16;
    int i=0;
    char buffer[4096];
    ssize_t bytesRead;
    ssize_t bytesPrinted=0;
    char str[linesize+1];
    int j;
    fd=open(fd, O_RDONLY, 0);
    	while ((bytesRead = read(fd, buffer, 4096)) > 0){
    	for (i=0; i<bytesRead; i++) {
    		if (buffer[i]=='\0'){
    			exit(0);
    		}
    		else{
    			printf("%c ", buffer[i]);
    			str[i]=buffer[i];
    			bytesPrinted++;
    			begin++;
    			if (bytesPrinted % linesize == 0){
    				str[linesize]='\0';
    				for (j=0; j<linesize; j++){
    					printf("%2x ", str[j]);
    				}
    			str[0]='\0';
    			printf("\n");
    			}
    		}		
    	}
    }
    return 0:
    };
    Oh by the way
    sorry about misspelling and
    my fingers got ahead of me.
    Last edited by cloudsword; 09-13-2009 at 01:21 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not a fan of your open statement.

    But anyway, once i goes past 16 all hell will break loose, since str[i] will no longer exist.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there any particular reason you're using open and read instead of fopen and one of the other reading functions?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    wouldn't the str[0]=''\0' take care of that problem?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cloudsword View Post
    wouldn't the str[0]=''\0' take care of that problem?
    No, and I'm not sure why you think it would.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Well, doesn't that function clear your string?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cloudsword View Post
    Well, doesn't that function clear your string?
    Yes. Since that's not related to your problem (your problem being i is way too big) so what?

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Quote Originally Posted by tabstop View Post
    No, and I'm not sure why you think it would.
    thus making your string ready for another set of 16 chars?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cloudsword View Post
    thus making your string ready for another set of 16 chars?
    Yes. So you would have to make even a slight attempt at putting 16 more chars in the string, as opposed to what you are doing in your code. (No seriously: read your code and don't just think it does what you want it to do.)

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Remember that a buffer only has so much storage. If you bought 16 apples, you wouldn't try to eat 17 apples.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Quote Originally Posted by tabstop View Post
    Yes. So you would have to make even a slight attempt at putting 16 more chars in the string, as opposed to what you are doing in your code. (No seriously: read your code and don't just think it does what you want it to do.)
    Thanks, After getting away from my computer, it came to me that I needed to replace i with something like bytesread%linesize;

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    If you bought 16 apples, you wouldn't try to eat 17 apples.
    There is nothing wrong with trying

    No matter what you use to replace i, just make sure it is never >16. bytesread%linesize looks good.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MK27 View Post
    There is nothing wrong with trying
    Sure, but you will fail miserably, just as you hopefully will by writing more data into a buffer than there is space.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. casting problems warnings segmentation fault.
    By BSmith4740 in forum C Programming
    Replies: 13
    Last Post: 07-03-2008, 12:13 PM
  3. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  4. Segmentation fault
    By BigAll in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 06:25 AM
  5. seg fault problems
    By nadamson6 in forum C++ Programming
    Replies: 11
    Last Post: 12-27-2005, 03:26 PM