Click here to Skip to main content
15,868,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm trying get "tokens" from a file under C, into a preallocated buffer. So far it looks like this:
C
char* token;
....
int main(int argc, const char* argv[])
{
     ....
     token = (char*)malloc(256);
     grab_token();
     ....
}
void grab_token()
{
	char a;
	int i = 0;
	while(a == ' ' || a == '\t' || a == '\r' || a == '\n' || a != EOF) { a = fgetc(source_FILE); }
	while(a != ' ' || a != '\t' || a != '\r' || a != '\n' || a != EOF)
	{
		a = fgetc(source_FILE);
		token[i] = a;
		i++;
	}
        // NULL terminate token
	token[i+1] = '\0'; 
}

It's quite simple for now, just checks if the character received is a whitespace, tab, newline.... or exits if it's an EOF or it takes "words" or "tokens" from the file and puts it in an allocated buffer. However the "grab_token()" specifically fails with the infamous "Segmentation Fault (Core Dumped)", with SIGEGV which means invalid memory access, however for some reason I don't see why I should be accessing memory I haven't allocated. And yes, fgetc() works perfectly.
Posted

You are using OR (||) where you should use instead AND (&&).
You are not checking for buffer overrun on token (actually you are not checking even if malloc succeeded).
 
Share this answer
 
CPallini is right. But to be more clear, see the following piece of code. It includes some comments and advices. you can change it according to your needs. I haven't tested the code, but I think there won't be any core dump (if it compiles successfully :) ).


C#
// define size as constant
const int BUFF_SIZE = 256;

// 1- I would return some value to check if successful or not
// 2- pass 'token' pointer as parameter
// 3- dont use global variables if you dont have to do so.
void grab_token()
{
    char a;
    int i = 0;

    while((a = fgetc(source_FILE))!=EOF) {
        if(a == ' ' || a == '\t' || a == '\r' || a == '\n') // this part can be converted to macro
            continue;
        break;
    }
    if(a==EOF) {
        token[0]=0;
        // maybe some error handling
        return;
    }

    while((a = fgetc(source_FILE))!=EOF) {
        if(a == ' ' || a == '\t' || a == '\r' || a == '\n')
            break;
        if(i<BUFF_SIZE-2) // BUFF_SIZE-1 is reserved for termination
            token[i++]=a;
        else {
            // some error handling
            // break;
            // or
            // return;
        }
    }
    token[i]=0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900