Thread: segmentation fault

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231

    segmentation fault

    hey guys what is wrong with this while loop

    Code:
    int buff1;
    FILE *pfile;
    
      pfile = fopen("textfile.txt","r+");
    while( (buff1 = fgetc(pfile)) != EOF && buff1 != '\n')
    {
             
           
                    printf("%s",buff1);
                    
                    }
    yes I wrote this, this isn't homework where I have to find the problem or something

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should not be trying to print an int as if it were a string. You could, however, print it as a char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    fgetc() means you are requesting a character from the file and then to print it you will need to print one character at a time by
    printf("%c",buff1);

  4. #4
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    Thanks, cant believe I missed that, thanks O_o

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    I use putchar for printing single characters. You could just do putchar(buff1);.

  6. #6
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    this is going to go inside of a windows application so I just did printf() for simplicity

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. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM