Title: read characters from a source file C
Tags: c;string;file
Question: 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);
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' &amp;&amp; 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;
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] &gt; 0 &amp;&amp; InBuffer[i] <= '~')
printf("%c", InBuffer[i++]);
i = 0;
while (InBuffer[i] &gt; 0 &amp;&amp; InBuffer[i] <= '~')
if ((InBuffer[i] == '/') &amp;&amp; (flag_multiLine == 0))
if (InBuffer[i + 1] == '*')
flag_multiLine = 1;
if ((InBuffer[i] == '*') &amp;&amp; (flag_multiLine == 1))
if (InBuffer[i + 1] == '/')
flag_multiLine = 0;
if ((InBuffer[i] == '/') &amp;&amp; (InBuffer[i + 1] == '/'))
flag_singleLine = 1;
if (InBuffer[i] == '\n')
flag_singleLine = 0;
if ((InBuffer[i] == '"') &amp;&amp; (flag_Quote = 1))
flag_Quote = 0;
if ((InBuffer[i] == '"') &amp;&amp; (flag_Quote == 0))
flag_Quote = 1;
//if ((InBuffer[i] == '"') &amp;&amp; (InBuffer[i - 1] == '\\') &amp;&amp; (flag_Quote == 1))
if ((InBuffer[i] &gt;= 'a' &amp;&amp; InBuffer[i] <= 'z' ||
InBuffer[i] &gt;= 'A' &amp;&amp; InBuffer[i] <= 'Z' ||
InBuffer[i] &gt;= '0' &amp;&amp; InBuffer[i] <= '9' ||
InBuffer[i] == '_'
) &amp;&amp; (flag_multiLine == 0) &amp;&amp; (flag_singleLine == 0) &amp;&amp; (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...
Comment: 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 = '"';`

Comment: Use re2c or flex. The 1970s are behind us.

Comment: 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/).

Comment: 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.

Comment: I edited, and fileBuffer returns successfully

Comment: `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.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:12/18/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><string><file>', 'Title': 'read characters from a source file C', 'ViewCount': 38, 'id': 41210865, 'user_AboutMe': '', 'user_AccountId': 9560920.0, 'user_DisplayName': 'Xaxoxuxu', 'user_Reputation': 1.0, 'user_Views': 9.0, 'user_id': 7101803.0}}