Answers.Body: <p>The most basic solution would be to store the character read by <code>fgetc</code>, for example, instead of:</p>
  
  <pre><code>if(!isspace(fgetc(handle)))
  </code></pre>
  
  <p>you'd write:</p>
  
  <pre><code>char val = fgetc(handle);
  if(!isspace(val))
  </code></pre>
  
  <p>This way if the <code>isspace</code>call returns false you will still have the latest character in the <code>val</code> variable.</p>
  
  <p>I'd also suggest you look into the <code>switch</code> statement instead of using multiple <code>if</code>-<code>else</code> blocks: <a href="http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/switch.html" rel="nofollow">http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/switch.html</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-22T08:53:55.480Z
Answers.Text: 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.
Answers.UserDisplayName: test
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-22T09:05:33.243Z
Answers.Text: Then you can try to rewind the pointer one character using fseek: http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group__cstdio_a_p_i_gabea819317fe740c7376ca24eaf1225c6.html
Answers.UserDisplayName: zovits
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-22T09:49:38.463Z
Answers.Text: Firmware version =< 1.28 .. I don't have the rights to update them.
Answers.UserDisplayName: test
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-22T10:20:20.897Z
Answers.Text: 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.
Answers.UserDisplayName: zovits
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-22T08:42:45.430Z
Answers.LastActivityDate: 2015-09-22T08:42:45.430Z
Answers.UserDisplayName: zovits
Body: <p>I have opened a file using <code>handle = fopen("rw.txt").</code> 
  The file consist of 3 values which is separated with spaces 
  ex: <code>12 23 34</code> 
  using <code>isspace(fgetc(handle))</code> 
  Problem here is that <code>fgetc()</code> increments the internal pointer,  making store incorrect strings including string. </p>
  
  <p>Can these three values be stored in some different way, making me store these values, and ignore the whitespace using NXC syntax?</p>
  
  <p>Here is my current code:</p>
  
  <pre><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 &lt;= 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();
  
              }
  
          }
      }
  
  }   
  </code></pre>
  
Tags: <nxt><nxc>
Title: fgetc() moves pointer one step forward
UserDisplayName: test
ThreadText: # fgetc() moves pointer one step forward
  
  (Asked by: username_0 on 2015-09-22)
  
  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();
  
              }
  
          }
      }
  
  }
  
  **Tags:** nxt, nxc
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-09-22. Score: 4 | Accepted answer
  
  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
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_1**: Then you can try to rewind the pointer one character using fseek: http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group__cstdio_a_p_i_gabea819317fe740c7376ca24eaf1225c6.html  
  > **username_0**: Firmware version =< 1.28 .. I don't have the rights to update them.  
  > **username_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.  
  
  ---
  
  