3

I have opened a file using handle = fopen("rw.txt"). The file consist of 3 values which is separated with spaces ex: 12 23 34 using isspace(fgetc(handle)) Problem here is that fgetc() increments the internal pointer, making store incorrect strings including string.

Can these three values be stored in some different way, making me store these values, and ignore the whitespace using NXC syntax?

Here is my current code:

#import "value.txt" val
task main()
{   
    byte handle;
    handle = fopen("value.txt", "r");
    if (handle != NULL)
        {
            string a;
            string b;
            string c;
            int count = 0;

           while(count <= 2)
           {    
               if(!isspace(fgetc(handle)))
               {    
                   if(count == 0)
                   {
                         fgets(a, 2, handle);
                         TextOut(0, LCD_LINE3, "done a" + a);   
                         TextOut(0,LCD_LINE4," lenght: " + NumToStr(strlen(a)));
                         Wait(3000);    
                         count = count + 1;
                         ClearScreen();             
                   }
                   else if(count == 1)
                   {
                        fgets(b, 2, handle);
                        TextOut(0, LCD_LINE3, "done b" + b);
                        Wait(3000); 
                        count = count + 1;
                        ClearScreen();          
                   }
                   else if(count == 2)
                   {
                        fgets(c, 2, handle);
                        TextOut(0, LCD_LINE3, "done c" + c);
                        Wait(3000);
                        count = count + 1;
                        ClearScreen();
                   }   
               }

                TextOut(0, LCD_LINE3, "done" + NumToStr(count));
               Wait(3000);
               ClearScreen();

            }

        }
    }

}   
4

The most basic solution would be to store the character read by fgetc, for example, instead of:

if(!isspace(fgetc(handle)))

you'd write:

char val = fgetc(handle);
if(!isspace(val))

This way if the isspacecall returns false you will still have the latest character in the val variable.

I'd also suggest you look into the switch statement instead of using multiple if-else blocks: http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/switch.html

  • I don't see how this changes my case. the problem is that the pointer increment when fgetc is called. I have decided to just hardcode the solution, as it doesn't seem like that the structure of how the values are outputted doesn't change. – test Sep 22 '15 at 8:53
  • Then you can try to rewind the pointer one character using fseek: bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/… – zovits Sep 22 '15 at 9:05
  • Firmware version =< 1.28 .. I don't have the rights to update them. – test Sep 22 '15 at 9:49
  • 1
    What are you trying to achieve then? If for example you know you won't have too many values in the text file you could read the whole file into a string and perform the separation in your code with array access to all characters. If this is not possible then you have to resort to character level manipulation: read a char, if it is not a separator then append it to the current buffer, if it is a separator then take the content of the current buffer, do soemthing with it and empty the buffer for the next item. – zovits Sep 22 '15 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.