Thread: Segmentation error

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    Segmentation error

    Hello, I keep getting a segmentation error in this code

    I'm trying to get two values (from two lines) in a seperate out file, set them equal to a value, and then use them for calculations. But I cant really get that far. Any help?

    Code:
    -bash-3.00$ ./a.out
    
    Name: My Name
    
    
    str1 = (null)
    Segmentation fault
    -bash-3.00$
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    
    void displayName(void);
    bool openFile(FILE *fIn, FILE *fOut);
    
    
    int main(void) {
    
       FILE *fIn, *fOut;
    
       displayName();
       openFile(fIn, fOut);
       
       return 0;
    }
    
    void displayName(void) {
    
       printf("\nName: My Name\n\n");
    }
    
    bool openFile(FILE *fIn, FILE *fOut) {
    
       int str1, str2;
    
       if ((fIn = fopen("test.txt", "r")) != NULL) {
      
           if ((fOut = fopen("out.txt", "w")) != NULL) {
    
              fscanf(fIn, "%s\n%s", str1, str2);
           
    
           
           printf("\nstr1 = %s", str1);
           printf("\nstr2 = %s", str2);
    
          
            }
           else
              printf("\nUnable to open \"out\" for writing.");
           fclose(fIn);
       }
        else
           printf("\nUnable to open \"test\" for reading.");
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    add a return statement to your bool function see if that helps

    edit: ahh there you go, kennedy helped!

    when i see these questions i usually try to compile myself and stop at the first error, which is what i did in this case, leaving me to study the rest of program and the function he caught! nice
    Last edited by nadroj; 11-07-2006 at 08:45 PM.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    >fscanf(fIn, "%s\n%s", str1, str2);

    This is wrong. %s is the modifier for char *, not int. str1 and str2 are uninitialized, therefore, you are randomly trashing memory.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    Edit, I fixed my second question!

    Thanks much!

    But the fscanf function is correct right? I'm trying to figure out why I'm getting a segmentation error after the value of str1 is displayed =/
    Last edited by Kitty Litter; 11-07-2006 at 09:12 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    As Kennedy said, fscanf() is expecting two char*s, not two ints. If you want to read ints, pass the address of the two ints:
    Code:
    fscanf(fIn, "%d%d", &str1, &str2);  /* maybe change the names of the variables, too */
    If you want to read in two strings, make str1 and str2 arrays of characters.
    Code:
    char str1[something], str2[something];
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    There we go thanks!

    I had it halfway right and kept getting a seg error. I changed it back to ints completely and it works.

    I <3 y'all

    *edit:

    Kennedy, you're advice got me there, it was my own stupid error that messed me up afterwards :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM