How should I concatenate the characters read from a file with fgetc to determine if it is a number, keyword or an identifier?

The problem is that, for example, the number 555 must be one and 5 and 5 and 5 as words must be jose and not character by character; besides I have problems with the blanks.

if (( f1 = fopen ( " ard.txt " , "r" ) ) == NULL) 
printf ( "Error opening file !");
while (( c = fgetc ( f1 ) )! = EOF )
if ( isdigit ( c ) )
    printf ( "% c is a number" , c ) ;

Recommended Answers

All 4 Replies

to determine if it is a number, keyword or an identifier

This is a key statement to me, because it sounds like you're writing a lexer. If that's the case, it's not really as simple as throwing characters into an array (or some variation of *scanf); you need to include some introspective analysis on the characters to properly tokenize elements of the language.

Would you elaborate on the contents of the file and the language definition? That's somewhat important in giving a proper answer to the question. :)

yes , that is a lexer in "C" ,I have for example the next code in C

           #include <stdio.h>

    int main()
    printf("Hola mundo");
    return 0;

I need to identify the number, words and keywords and the output of the file will be:

# hastag
< minus
stdio.h ---keyword
> mayor
int  keyword
void  keyword
       {  oper
printf   keyword
"   quot
hola    palabra
mundo palabra
"  quot
return  keyword
0 number

but I tried with the number but for example the number 1234 would be a only number but in the output is:

   1 number
 2 number
 3 number
 4 number

and the correct answer will be:

       1234-------number                 I think this homework is with the swithc case  right?

You could keep track of the LAST character type you extractedand compare it to the current type (i.e. digit). So when you grab a '1', you don't immediately assume you are done extracting the number. You grab the next digit '2' and notice that it's a digit, so you keep going and grab the next character and it's a '3', so you keep going and grab the '4' and you keep going, then the next characters a NON-digit, so you're done with the number. Concatenate as you go. You have 1234, then white space or a bracket or a letter or whatever, which means you're done with the token you are working on, so put the 1234 on the stack or print it out or whatever you do with it and move on. I imagine that's what you are doing with the words already, right? Find a letter, keep grabbing characters till there is a non-letter, that's a word. Same thing with digits.

Adjust the above strategy depending on whether they are throwing any numbers at you that do not consist solely of digits.

I found another way for do my proyect I need to read a txt and store it in a dynamic string array
the code I have now does but memory is wasted because it is static

I like to read a txt and every word stored in a string array of dynamic

  #include <stdio.h>
 #include <conio.h>
#define MAX_CHARS 20

typedef char string[MAX_CHARS+1];  // leave one space for '\0'

int i;
string array[4];

FILE *data;
data = fopen("ard.txt","r");
for(i = 0; i < 4; i++)
    { fscanf(data, "%s", array[i]); } // no need for & with %s
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.