structural fault any ideas

This is a discussion on structural fault any ideas within the C Programming forums, part of the General Programming Boards category; im currently working on two file reading and writing programs for my final assesment on putty using vi both of ...

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

    Question structural fault any ideas

    im currently working on two file reading and writing programs for my final assesment on putty using vi

    both of theses programs now are error free although contain multipul warning messages but when i run the first program i recive a structural fault when tring to read the problem.

    heres the code:
    Code:
    #include <stdio.h>
    
    typedef int weeks[13];
    
    struct sale
     {
       int week;
       char name;
       int units;
       int price;
     }sale;
    
    
    int main (void)
    {
     int week;
     int unit;
     int cost;
     sale.week = week;
     sale.units = unit;
     sale.price = cost;
    
    
     char filename1;
     char filename2;
     char output;
     int maxsale= 0;
     int maxweek= 0;
     int tc1; 
     char cbuff[5];
     FILE *pointer1;
     FILE *pointer2;
    
     printf("please enter the filename to open\n");
     scanf("%s",&filename1);
    
     printf("please enter the filename to write to\n");
     scanf("%s",&filename2);
     
     pointer1 = fopen(filename1,"r");
    
     if (pointer1 == NULL)
      {
       printf("file failed to open");
      }
    
    
    
     pointer2 = fopen(filename2,"w"); 
    
     if (pointer2 == NULL)
      {
       printf("file failed too open");
      }
     
    
    
     while(fgets(cbuff,1000,pointer1)!=NULL)
        {
      sscanf(pointer1,"%d\t%s\t%d", &sale.week, &sale.units, &sale.price); 
     
          if (cost > maxsale)
              { 
                maxsale = cost;
                maxweek = week;
              }
    
    
            
           fprintf(pointer2,"week no\t\t Total sales\n");
            
    
         if (week == "1")
             {
              (tc1= (cost * unit) + tc1);
               fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
         
          if (week == "2")
             {
              (tc1= (cost * unit) + tc1);
             fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
    
          if (week == "3")
             {
              (tc1= (cost * unit) + tc1);
             fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
    
          if (week == "4")
             {
              (tc1= (cost * unit) + tc1);
              fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
    
          if (week == "5")
             {
              (tc1= (cost * unit) + tc1);
              fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
         
           if (week == "6")
             {
              (tc1= (cost * unit) + tc1);
              fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
    
          if (week == "7")
             {
              (tc1= (cost * unit) + tc1);
              fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
    
          if (week == "8")
             {
              (tc1= (cost * unit) + tc1);
              fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
    
          if (week == "9")
             {
              (tc1= (cost * unit) + tc1);
              fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
        
          if (week == "10")
             {
              (tc1= (cost * unit) + tc1);
              fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
    
         if (week == "11")
             {
              (tc1= (cost * unit) + tc1);
              fprintf("pointer2,%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0;
    
         if (week == "12")
             {
              (tc1= (cost * unit) + tc1);
              fprintf(pointer2,"%d \t\t %d\n",sale.week,tc1);
            }
           tc1=0; 
            
           
         }
    
      fprintf(pointer2,"the max sale was %d and the max week was %d\n",&maxsale,&maxweek);
      fclose(pointer1);
      fclose(pointer2);
    }
    can any one give me some ideas before midday (thats when it has to be emailed in)

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    scanf("%s",&filename1);
    scanf("%s",&filename2);

    filename1, filename2 are single characters; a string can't fit in a character. you need a character array that has at least (length of input)+1 chars

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

    Talking cheers

    cheers thats a big help i think thats solved the problem on the second one aswell cheers

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    You check filepointer - but continue to work with it even if it is null

    fgets(cbuff,1000,pointer1)
    your cbuff array has size 5
    use sizeof(cbuff) as the second parameter to avoid buffer overrun (and increase size of the buffer - use BUFSIZ predefine constant for example)

    sscanf(pointer1,"%d\t%s\t%d", &sale.week, &sale.units, &sale.price);
    the first parameter should be buffer, containing string, not file handle
    3 variables are of the type int, %s format should not be used there


    sale.week = week;
    sale.units = unit;
    sale.price = cost;
    you just copy the uninitialized values into your struct
    Code:
    if (cost > maxsale)
              { 
                maxsale = cost;
                maxweek = week;
              }
    you filled struct members with the input - variables week, cost etc are still not initialized


    if (week == "1")

    week is int don't compare it to string - use if (week == 1)


    Don't compare C-strings with == - use strcmp if you nedd it somethere
    Last edited by vart; 12-22-2006 at 04:34 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    just wondering why didnt you do a Case statement, and avoid all those "ifs"?
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Because a case statement would be the same length
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Maybe I am still to new to C but wont it still try and match through all those 'if' statements even after the first match? At least with a switch you can break to terminate the case statement.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    I don't see any differences inside ifs...
    So there is no point to use them here at all
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by gettyUP
    Maybe I am still to new to C but wont it still try and match through all those 'if' statements even after the first match? At least with a switch you can break to terminate the case statement.
    Yes, if you use just if() statements. If you use else if()s where appropriate, then no. And IIRC you can't use strcmp() in switch statements.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You can use strcmp() whenever you want, just isn't the smartest thing to do. Something like:

    Code:
    switch(strcmp(two, strings)){
            case 0:
                        SomeCoolFunction();
                        break;
            case 1:
                        SomecoolFunction();
                        break;
            case -1:
                        SomeCoolfunction();
                        break;
    }
    strcmp() is supposed to return -1,0,1 ONLY, however, can one really trust that?

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Return Value
    The return value for strcmp indicates the lexicographic relation of string1 to string2.

    Value Relationship of string1 to string2
    < 0
    string1 less than string2

    0
    string1 identical to string2

    > 0
    string1 greater than string2
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Just to be clear: strcmp() doesn't nessesarily return -1 or 1, just a value less than or more than zero (as vart has indicated).
    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.

  13. #13
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Ooops, yep, you're right. I was thinking about a wrapper I made for it (for a structure). . . sorry about that.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    hi me agein just if any one is still reading this i handed my program in at one and took in all the advice i was given up untilkl that point and i scored ok after checking this post id like to thank everyone who tried to help even though i didnt understand it all at the time.

    p.s to kog metalgod at the time i didnt know what a case statment was so thats why i used all the if statments thanks though

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    That chain of if-statements has got to be one of the most hilarious pieces of code I've ever seen. I'd tell you all the errors in this code, but I can't seem to stop laughing long enough to do so.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 02:53 AM
  2. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. idea's idea's idea's
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-29-2002, 12:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21