Thread: PS: I gotta bad feeling about the file pointers!

  1. #1
    lonelyplanetwanderer
    Guest

    PS: I gotta bad feeling about the file pointers!

    wazzup?...i got 4 functions including main (see below)...main gives the option of calling engine() or find()...these 2 call funk() for the computation and printinf of results...find() works fine when it call funk()...so funk() is working alright...engine() with funk() gives a Segmentation Fault core dumped!...engine() is this tinee tiny lil' function with 10 lines of code...can't seem to figure out whats' wrong...tried all sorts of juxtapositioning of code...doesn't seem to solve the problem...would greatly appreciate some inputs...need to sumbit tomm!~


    PS: I gotta bad feeling about the file pointers!

    #include<stdio.h>
    #include<string.h>

    int a,b,c,d,y=0,z=0,i=0,j,k,l=0,m,n,ctr=0,len,len1,len 2,len3,len4,len5,len6,len7,len8,len9,len10,f,x,chk =0;
    char message[38]="MAIL From:<[email protected]>", cell[5]="9198", cell_1[5]="+919", cell_2[5]="9810", cell_3[5]="9811", cell_4[5]="9821", cell_5[5]="9820", cell_6[5]="9841", cell_7[5]="9823", cell_8[5]="9830";
    char line[300];
    void funk();

    main()
    {
    char choice;

    void engine();
    void find();

    printf("ENTER 'D' to display the following details from the SMS server log file:\n1. Message Sent Time\n2. Message Sent Date\n3. Customer's Mobile Number\n");
    printf("\nOR\n\nEnter 'S' to search records by DATE\n");

    scanf("%c",&choice);

    if(choice=='D')
    engine();

    if(choice=='S')
    find();
    }



    void engine()
    {
    FILE *fp;
    fp=fopen("ummailer.txt","r"); //ENTER SMS server log file name here


    while(!feof(fp))
    {
    while(fgets(line,300,fp)!=NULL)
    {
    funk(line, fp);
    label: x=ftell(fp);
    fseek(fp,x,0);
    }
    }
    fclose(fp);
    }



    void find()
    {
    char input_mth[4];
    char input_dt[3];
    FILE *fp;

    printf("ENTER the search month (e.g. 'Apr'):\n");
    scanf("%s",input_mth);
    printf("ENTER the search date (e.g. '18'):\n");
    scanf("%s",input_dt);

    if(strlen(input_dt) == 1)
    {d=5;
    c=1;}
    else
    {d=4;
    c=2;}

    fp=fopen("ummailer.txt","r"); //ENTER SMS server log file name here


    while(!feof(fp))
    {
    while(fgets(line,300,fp)!=NULL)
    {
    len=strlen(line);

    for(i=0;i<3
    {
    if(line[i]==input_mth[i])
    {
    i++;
    if(i==3)
    {
    for(n=0;n<c
    {
    if(line[n+d]==input_dt[n])
    {
    n++;
    if(n==1 && line[4]==' ' && (strlen(input_dt)==1))
    {
    funk(line, fp);
    }


    if(n==2 && line[4]!=' ' && (strlen(input_dt)==2))
    {
    funk(line, fp);
    }

    }
    else goto label;
    }

    }
    }
    else break;
    }

    label: z=ftell(fp);
    fseek(fp,z,0);
    }
    }
    fclose(fp);
    }



    void funk(char * line, FILE *fp)
    {
    FILE *fp1;
    char _line[300];

    len=strlen(_line);
    fp1=fopen("ummailer.txt","r");

    for(j=41;j<77;j++)
    {
    for(k=0;k<37
    {
    if(line[j]==message[k])
    {
    j++;
    k++;

    if(k==36)
    {
    ctr++;
    printf("RECORD NUMBER: %d\n",ctr);
    printf("Message Sent DATE: ");
    for(m=0;m<6;m++)
    {
    printf("%c",line[m]);
    }
    printf("\n");
    printf("Message Sent TIME: ");
    for(m=7;m<15;m++)
    {
    printf("%c",line[m]);
    }
    printf("\n");

    f=ftell(fp);
    fseek(fp1,f,0);
    while(fgets(_line,300,fp1)!=NULL)
    {
    len=strlen(_line);
    for(a=0;a<len;a++)
    {
    for(b=0;b<4
    {
    if(_line[a]==cell_2[b] || _line[a]==cell_1[b] || _line[a]==cell[b] || _line[a]==cell_3[b] || _line[a]==cell_4[b] || _line[a]==cell_5[b] || _line[a]==cell_6[b] || _line[a]==cell_7[b] || _line[a]==cell_8[b])
    {
    a++;
    b++;
    len1=strlen(cell);
    len3=strlen(cell_1);
    len4=strlen(cell_2);
    len5=strlen(cell_3);
    len6=strlen(cell_4);
    len7=strlen(cell_5);
    len8=strlen(cell_6);
    len9=strlen(cell_7);
    len10=strlen(cell_8);
    if((b+1)==len1 && _line[a-3]=='9')
    {
    printf("Mobile Number: ");
    for(c=54;c<66;c++)
    {
    printf("%c",_line[c]);
    }
    printf("\n");
    printf("\n");
    goto label;
    }
    if((b+1)==len3 && _line[a-3]=='+')
    {
    printf("Mobile Number: ");
    for(c=a;c<(a+12);c++)
    {
    printf("%c",_line[c]);
    }
    printf("\n");
    printf("\n");
    goto label;
    }
    }
    else break;
    }
    }
    y=ftell(fp1);
    fseek(fp1,y,0);
    }
    label: fclose(fp1);

    }
    } //END OF FIRST EVER 'IF'
    else
    break;
    }
    }
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Please use code tags!

    You don't check the values of the file pointers. The function fopen returns NULL if it could not open the file.

    The function funk() expects a pointer to the file unmailer.txt. In the function funk(), you declare another pointer and are trying to open unmailer.txt. Did you test if it is possible to open a file which is already opened?

    Try to avoid variable-names starting with underscore.

    Try to avoid using goto.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Shiro
    Try to avoid using goto.
    There is no try, just do ! (Didn't Yoda say something like similar?!)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    lonelyplanetwanderer
    Guest

    ...i forgot

    The use of a file pointer opening a file already open works fine...if i don't use the funk() function and write the same code within engine() it works well...so no issues as far as that goes...

    ...i forgot to mention the engine() calling funk() actually runs for the first few records in the ummailer.txt and then suddenly gives a segmentation fault!...there is no difference in the remainder of the txt file!

    ...any suggestions will be greatly appreciated!~

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Suggestion 1: Post you code (or edit the first post), this time with smilies off, and using code tags.

    Now, in your code:
    Code:
    for(j=41;j<77;j++) 
    { 
    for(k=0;k<37 )   /* Problem here, missing parameter */
    { 
    if(line[j]==message[k]) 
    {
    There are more examples of the above problem.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It's actually

    Code:
    for(k=0;k<37 ; )
    Just missing the normal increment step, which is inside the loop instead

    Also its got the usual feof bug

    while(!feof(fp))
    {
    while(fgets(line,300,fp)!=NULL)

    Should be
    while ( fgets(line,sizeof(line),fp) != NULL )

    And the final killer
    char _line[300];
    len=strlen(_line); /* strlen of garbage - nice */

    Not to mention the repeated re-opening of the same file
    void engine()
    {
    FILE *fp;
    fp=fopen("ummailer.txt","r"); //ENTER SMS server log file name here
    ....
    funk(line, fp);


    And in funk
    void funk(char * line, FILE *fp)
    {
    FILE *fp1;
    char _line[300];

    len=strlen(_line);
    fp1=fopen("ummailer.txt","r");


    And this seems a waste of time
    x=ftell(fp);
    fseek(fp,x,0);

    Two tips
    1. Learn about passing parameters - lots of void functions and global vars isn't good
    2. use local vars for loop indexes etc

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Salem
    It's actually
    Code:
    for(k=0;k<37 ; )
    Just missing the normal increment step, which is inside the loop instead
    Yes, that's true, but there's also another problem. The increment statement is buried inside an if condition. If that condition isn't met, we don't increment. The way it's coded, it might work, but it's pretty hard to read/understand.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Code:
    if((b+1)==len1 && _line[a-3]=='9')
    if((b+1)==len3 && _line[a-3]=='+')
    What if the value of 'a' is 0??? Check for that too apart from the suggestions you got above.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by shaik786
    Code:
    if((b+1)==len1 && _line[a-3]=='9')
    if((b+1)==len3 && _line[a-3]=='+')
    What if the value of 'a' is 0??? Check for that too apart from the suggestions you got above.
    Good spot... to expand a bit more (in case lonelyplanetwanderer doesn't realise), what if a is 1 or 2 as well?

    Actually, looking at the code, a will never be 0. This is because it starts life set to 0, then before it gets to this if statement, it gets incremented.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    lonelyplanetwanderer
    Guest

    thanx guys!

    appreciate all the inputs!...got it to work...cheers=)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM