1

I am about to code a tokenizer, and I am actually surprised, I have managed to do a compilable code :D but I have a problem I don't know how to solve.

I start with making an array[WORD_MAX_SIZE]={0}. After I use fopen *'r'. Word max size is global defined. My program is running fine and I get the out I want, but I want to change a thing, but I don't know how. I print the result out to the screen in the end of my While loop. But I want to save the full result in one string. Instead of printing it, I would like it in a stringset, so I can play with it later. Can anyone help?

Thanks in advance.

while (feof(fp)==0)
{
    c=fgetc(fp);

    if(isalpha(c))
    {
        array[i]=c;
        i++;
    }
    else if (i!=0)
    {
        array[i]='\0';
        i=0;
        printf("%s\n", array);
    }
}

fclose(fp);

return 0;
Paulo
  • 1,458
  • 2
  • 12
  • 26
  • `while (feof(fp)==0)` won't work properly. See http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Andrew Henle Dec 10 '15 at 12:18

1 Answers1

1

You can always use sprintf() instead of printf(). printf() goes to the console. With sprintf(), you can choose where to print.

For example:

char returnedArray[100]; // or change the size
sprintf(returnedArray, "%s\n", array);

Now, you have your char array (remember you don't have strings in C) saved in returnedArray.

Paulo
  • 1,458
  • 2
  • 12
  • 26
  • Thanks for your respond. If i use that notation sprintf(returnedArray, "%s\n", array); Does it mean it is saved in one char array? – Pasionate coder Dec 10 '15 at 12:17
  • Yes, after that, the content of array will be saved in returnedArray (with a line break character in the end because of '\n'). If this answer helped you, could you please accept it? :) – Paulo Dec 10 '15 at 12:19