0

I'm trying to read a c source file for symbols that: are not in a string, are not in a comment.

int main() {
        clearInput();
        ReadFileToScreen();

return 0; }

Here are my functions:

void ReadFileToScreen()
{
    short i = 0;
    int ret;
    int numChars = 0;
    char inFile[20];
    char *pointerToData;

    printf("Enter input file name: ");
    fgets(inFile, sizeof(inFile), stdin);
    inFile[strcspn(inFile, "\r\n")] = 0;

    while (inFile[i] != '\0')
    {
        i++;
        numChars++;
    }

    pointerToData = ReadFromFile(inFile, numChars);


    ret = readIdentificators(pointerToData);
    printf("%d\nPress Enter to continue", ret);
}

and

char* ReadFromFile(char *FileName, int numChars)
{
    int i = 0;
    int reached = 0;
    char fileData[100];
    char fileBuffer[10000];
    FILE *fileIn;

    if (FileName[numChars - 1] == 'c' && FileName[numChars - 2] == '.')
    {
        fileIn = fopen(FileName, "r");

        while (fgets(fileData, sizeof(fileData), fileIn) != NULL)
        {
            i = 0;
            while (fileData[i] != '\0')
            {
                fileBuffer[reached] = fileData[i];
                reached++;
                i++;
            }
        }
        fclose(fileIn);
    }
    else
    {
        printf("Wrong input file!\n");
    }

    return fileBuffer;
}

and

    int readIdentificators(char *InBuffer)
{
    int result = 0;
    int i = 0;
    short flag_singleLine = 0;
    short flag_multiLine = 0;
    short flag_Quote = 0;

    while (InBuffer[i] > 0 && InBuffer[i] <= '~')
        printf("%c", InBuffer[i++]); 

    i = 0;
    while (InBuffer[i] > 0 && InBuffer[i] <= '~')
    {
            if ((InBuffer[i] == '/') && (flag_multiLine == 0))
                if (InBuffer[i + 1] == '*')
                    flag_multiLine = 1;

            if ((InBuffer[i] == '*') && (flag_multiLine == 1))
                if (InBuffer[i + 1] == '/')
                    flag_multiLine = 0;

            if ((InBuffer[i] == '/') && (InBuffer[i + 1] == '/'))
                flag_singleLine = 1;

            if (InBuffer[i] == '\n')
                flag_singleLine = 0;

            if ((InBuffer[i] == '"') && (flag_Quote = 1))
                flag_Quote = 0;

            if ((InBuffer[i] == '"') && (flag_Quote == 0))
                flag_Quote = 1;

            //if ((InBuffer[i] == '"') && (InBuffer[i - 1] == '\\') && (flag_Quote == 1))



            if ((InBuffer[i] >= 'a' && InBuffer[i] <= 'z' ||
                InBuffer[i] >= 'A' && InBuffer[i] <= 'Z' ||
                InBuffer[i] >= '0' && InBuffer[i] <= '9' ||
                InBuffer[i] == '_'
                ) && (flag_multiLine == 0) && (flag_singleLine == 0) && (flag_Quote == 0))
            {
                result++;
            }
        i++;
    }

    return result;
}

It reads files correctly but I can't seem to make the checks fro the comments and strings...

Xaxoxuxu
  • 1
  • 2
  • Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Dec 18 '16 at 17:18
  • 1
    Use re2c or flex. The 1970s are behind us. – Petr Skocik Dec 18 '16 at 17:19
  • 1
    `fileBuffer` is an automatic variable. It doesn't exist anymore after the return. You cannot return it [as a pointer]. Use `malloc` or use a global buffer. – Paul Ogilvie Dec 18 '16 at 17:20
  • 1
    Be aware that a string literal may also *contain* a `"` by an escape sequence, for example `"Hallo \"dear\" world\n"` and your code should test for that possibility so as not to terminate the literal prematurely. Moreover, a`"` may also be a value such as `int ch = '"';` – Weather Vane Dec 18 '16 at 17:22
  • I edited, and fileBuffer returns successfully – Xaxoxuxu Dec 18 '16 at 18:17
  • Your use of `fileBuffer` is still incorrect and results in Undefined Behaviour. It may *appear* to work, for now, but when you access the memory that was previously allocated to `fileBuffer` from outside `ReadFromFile` then you are skating on very thin ice. – Paul R Dec 19 '16 at 10:53

0 Answers0