Thread: File I/O: Offsetting file pointer (fp) using fseek

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    File I/O: Offsetting file pointer (fp) using fseek

    Hello forum.

    I have an issue with a program supposed to take in a user input for a file name, open it, read its contents and finally output the function name(s) and how many source lines of code (for this program, only legitimate semi-colons).

    This is an example of a .txt file with a snippet of C code.

    Code:
    void fillAnArray(int a[]) 
    BEGIN_FCN  
     int i, j; 
     fill = -3;  /* comment; <- uncounted semi-colon */ 
     for( i = 0, j=2; i < sizeof(a); i++, j--) 
       a[i] =  i + j * fill;    /* of course ; a 
      comment may be a number of 
      lines; pages, etc etc */ 
     printf (“stuff;”);
     return;
    END_FCN
    successfully, it should output 7 SLOCs, due to 7 counted semi-colons excluding comments.

    This is my code so far. It isn't very far, but I have an issue right from the get go.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define BEGIN_FCN {
    #define END_FCN }
    #define MAX 1000
    
    #define INPUT_FILE "example.txt"
    
    int main(void)
    {
        char fileName[MAX];
        char str [MAX];
        int begin, end;
        int filePrev, fileCur;
        int *c, temp1, temp2, length1, length2;
    
    
    
        FILE *fp;
        printf ("Enter the file name to open: ");
        gets(fileName); /* get the file name from stdin */
        fp = fopen(fileName, "r");
        if (fp == NULL)
        {
            do
            {
                printf("\nFile %s could not be opened\n", fileName);
                printf ("Enter the file name to open: ");
                gets(fileName); /* get the file name from user */
                fp = fopen(fileName, "r");
            }
            while (fp == NULL);
        }
        printf("\bFile %s is successfully opened\n\n", fileName);
        /* perform desired read operations from the file in here */
    
        while (!feof(fp))
        {
            fgets(str, MAX, fp);
            printf("%s", str);
        }
    
        fseek(fp, 0, SEEK_SET);
        filePrev = fseek(fp, 0, SEEK_SET);
    
    while (!feof(fp))
        {
            fgets(str, MAX, fp); //Get current string
            printf("%s\n", str);
            temp1 = strstr(str, "BEGIN_FCN"); //Check if BEGIN_FCN is in string
            temp2 = strstr(str, "END_FCN"); //Check if END_FCN is in string
          if (temp1 != 0)
            {
                fseek(fp, 0, SEEK_CUR); //Function name is in the line before
                fgets(str, MAX, fp); //String containing function name
                printf("%s\n", str);
                //fseek(fp, line2, SEEK_SET);
                temp1 = strchr(str,'(');
            }
        }
        printf("\n\nComplete couting SLOC of %s", fileName);
        fclose(fp);
        getchar();
        return 0;
    }
    My issue is after finding the BEGIN_FCN which replaces the { and the END_FCN which replaces the } of functions, finding the proper offset for fseek(fp, offset, SEEK_CUR); so it aims at the beginning of the string right above BEGIN_FCN, which is the string that contains the name of the function.

    I want to get the name of the function, and find how many SLOCs there are inside.

    It works for this time because if the offset is 0, it is at the start of the file which is already the beginning of the proper string.

    Would I use strlen to find the lengths of the line with the function, then the line I read afterwards ("BEGIN_FCN" or "{") then subtract how much from the current fp to end up at the start of the proper string?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't generally fseek on text files due to the way different OSs treat newlines. You would be better off reading each line into memory, and figuring out what you need to do from there.
    Code:
    while( fgets() != NULL )
        if strstr buf, "BEGIN_FNC"
            while( fgets() ... same as above )
                if strstr buf, "END_FNC"
                    break from the loop you are done with this function
                else
                    add to your line counter or whatever you are doing
            do whatever it is with your function length you just got

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

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Quote Originally Posted by quzah View Post
    You don't generally fseek on text files due to the way different OSs treat newlines. You would be better off reading each line into memory, and figuring out what you need to do from there.
    Code:
    while( fgets() != NULL )
        if strstr buf, "BEGIN_FNC"
            while( fgets() ... same as above )
                if strstr buf, "END_FNC"
                    break from the loop you are done with this function
                else
                    add to your line counter or whatever you are doing
            do whatever it is with your function length you just got

    Quzah.
    Wait, so I wouldn't need to use fseek?

    How would I be able to retrieve the file name which is the string abouve BEGIN_FCN?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That depends. You could store the whole file in a linked list, one line per node, or you could just use two buffers, one for current line and one for last line. When you read in a new line if it matches what you need, then last line should be your function name. If it isn't what you need, then current line becomes last line, and you read a new current line.


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

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Quote Originally Posted by quzah View Post
    That depends. You could store the whole file in a linked list, one line per node, or you could just use two buffers, one for current line and one for last line. When you read in a new line if it matches what you need, then last line should be your function name. If it isn't what you need, then current line becomes last line, and you read a new current line.


    Quzah.
    Ok. I used two buffers and if BEGIN_FCN is found in the 2nd buffer, the function name is in the 1st buffer.

    How would I approach getting the name?

    it could be in the form of...

    Code:
    void main()
    void /*comment*/ main                    ()
    void /**/ main /**/
    
    and etc
    Would using strtok be useable? I can output by using strtok and splitting the string, but how would you be able to store the actual string section that contains the name?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure that would work. Look at the standard string functions, and see what you can come up with to remove comments from lines.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and their Application to image processing
    By quintenmater in forum C Programming
    Replies: 7
    Last Post: 12-10-2010, 02:10 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  5. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM

Tags for this Thread