View unanswered posts | View active topics It is currently Mon Sep 15, 2014 12:05 am






Reply to topic  [ 4 posts ] 
Outdated Wiki Example about file access 
Author Message
Rookie

Joined: Tue Nov 20, 2012 8:09 pm
Posts: 35
Post Outdated Wiki Example about file access
I found what I thought to be a great example of more complicated file access in RobotC on the official wiki: http://www.robotc.net/wiki/NXT_Functions_File_Access#Example
But then code I'm developed for a project with the help of that page was producing compile errors I couldn't figure out. So I tried compiling the example. Same errors. That code was written in Dec 2011, and is not compatible with the latest RobotC.

Here is the code:
Code:
/*****************************\
|* Example File Input/Output *|
|* Bence Feher 22/Dec/2011   *|
\*****************************/
 
task main()
{
  TFileHandle   hFileHandle;              // will keep track of our file
  TFileIOResult nIOResult;                // will store our IO results
  string        sFileName = "test.txt";   // the name of our file
  int           nFileSize = 100;          // will store our file size
 
  byte CR = 0x13;   // define CR (carriage return)
  byte LF = 0x10;   // define LF (line feed)
 
  string        sMessageToWrite = "ROBOTC IO test!";    // we will write this to the file
  string        sMesageToWrite_2 = "A new line!";       // we will also write this to the file on the next line
  char          incommingChar;                          // this will store each char as we read back in from the file
  string        incommingString[5];                     // this will store the final, fully-read strings (with new strings getting put into new indexes
  int           nLineCounter = 0;                       // this will let us know which line we are on when reading and writing (used as the index to 'incommingString[]')
 
  Delete("test.txt",nIOResult);
  OpenWrite(hFileHandle, nIOResult, sFileName, nFileSize);    // open the file for writing (creates the file if it does not exist)
  WriteText(hFileHandle, nIOResult, sMessageToWrite);         // write 'sMessageToWrite' to the file
  WriteByte(hFileHandle, nIOResult, CR);                      // write 'CR' to the file (carriage return)
  WriteByte(hFileHandle, nIOResult, LF);                      // write 'LF' to the file (line feed)
  WriteText(hFileHandle, nIOResult, sMesageToWrite_2);        // write 'sMesageToWrite_2' to the file
  Close(hFileHandle, nIOResult);                              // close the file (DON'T FORGET THIS STEP!)
 
  wait1Msec(1000);  // just a wait so we can see the variables updating in the ROBOTC debugger in order (this is not necessary)
 
  OpenRead(hFileHandle, nIOResult, sFileName, nFileSize);     // open our file 'sFileName' for reading
 
  for(int i = 0; i < nFileSize; i++)                          // iterate through the file until we've hit the end:
  {
    ReadByte(hFileHandle, nIOResult, incommingChar);            // read in a single byte
 
    if(incommingChar == CR || incommingChar == LF)              // if the incomming byte is a carriage return or a line feed:
    {
      if(incommingChar == LF)                                     // if it's a line feed:
        nLineCounter++;                                             // increment our index (will now store next char on a 'new line' (a new index into our 'incommingString[]')
    }
    else
    {
      incommingString[nLineCounter] += incommingChar;             // append that byte (char) to the end of our final string, at the right index
    }
  }
  Close(hFileHandle, nIOResult);                              // close our file (DON'T FORGET THIS STEP!)
 
  for(int i = 1; i <= 5; i++)
  {
    nxtDisplayCenteredTextLine(i, incommingString[i-1]);
  }
 
 
  while(true);  // infinite loop to keep our program running so that we can view the ROBOTC debugger variables
}


And here are the errors:
Code:
*Warning*:Comparison between 'signed' and 'unsigned' operands.
*Warning*:Comparison between 'signed' and 'unsigned' operands.
**Error**:Internal Compiler: Code generation for an invalid string assignment.


I realize that only one of these is actually an error, but I'd like to know about the warnings as well.

So, what needs to be done to make this program work correctly?


Sun Jan 19, 2014 9:44 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Outdated Wiki Example about file access
+1 !

I also would appreciate support of real C commands (Robot"C" is finally "C" ):

Code:

Functions
Operations on files:

remove
    Remove file (function )

rename
    Rename file (function )

tmpfile
    Open a temporary file (function )

tmpnam
    Generate temporary filename (function )


File access:

fclose
    Close file (function )

fflush
    Flush stream (function )

fopen
    Open file (function )

freopen
    Reopen stream with different file or mode (function )

setbuf
    Set stream buffer (function )

setvbuf
    Change stream buffering (function )


Formatted input/output:

fprintf
    Write formatted data to stream (function )

fscanf
    Read formatted data from stream (function )

vfprintf
    Write formatted data from variable argument list to stream (function )

vfscanf
    Read formatted data from stream into variable argument list (function )

vscanf
    Read formatted data into variable argument list (function )

vsnprintf
    Write formatted data from variable argument list to sized buffer (function )

vsprintf
    Write formatted data from variable argument list to string (function )

vsscanf
    Read formatted data from string into variable argument list (function )


Character input/output:

fgetc
    Get character from stream (function )

fgets
    Get string from stream (function )

fputc
    Write character to stream (function )

fputs
    Write string to stream (function )

getc
    Get character from stream (function )

putc
    Write character to stream (function )

ungetc
    Unget character from stream (function )


Direct input/output:

fread
    Read block of data from stream (function )

fwrite
    Write block of data to stream (function )


File positioning:

fgetpos
    Get current position in stream (function )

fseek
    Reposition stream position indicator (function )

fsetpos
    Set position indicator of stream (function )

ftell
    Get current position in stream (function )

rewind
    Set position of stream to the beginning (function )


Error-handling:

clearerr
    Clear error indicators (function )

feof
    Check end-of-file indicator (function )

ferror
    Check error indicator (function )

perror
    Print error message (function )


Macros

BUFSIZ
    Buffer size (constant )

EOF
    End-of-File (constant )

FILENAME_MAX
    Maximum length of file names (constant )

FOPEN_MAX
    Potential limit of simultaneous open streams (constant )

L_tmpnam
    Minimum length for temporary file name (constant )

NULL
    Null pointer (macro )

TMP_MAX
    Number of temporary files (constant )

Additionally: _IOFBF, _IOLBF, _IONBF (used with setvbuf)
and SEEK_CUR, SEEK_END and SEEK_SET (used with fseek).


In these cases we know how to handle these commands correctly or - in case - we could simply refer to standard C references like
http://www.cplusplus.com/reference/cstdio/
and IMO there is no need to "invent" deviant RobotC commands if ANSI C commands already exist.

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Mon Jan 20, 2014 5:14 am
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 602
Post Re: Outdated Wiki Example about file access
We're actively trying to update the Wiki again - we lost over a year of Wiki updates/changes with our hosting provider tanking our database unexpectedly.

We'll update the page as soon as we can. Thanks.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Mon Jan 20, 2014 11:03 am
Profile
Rookie

Joined: Tue Nov 20, 2012 8:09 pm
Posts: 35
Post Re: Outdated Wiki Example about file access
Guess who has a more recent version of the wiki article? With updated code? That's right, it's our friends at archive.org!!!!
https://web.archive.org/web/20130705024430/http://www.robotc.net/wiki/NXT_Functions_File_Access
I'd still love to hear a technical explanation of why the old version doesn't work though :)

You guys have probably thought of it already, but I bet you can recover a lot of lost wiki content that way.


Mon Jan 20, 2014 4:05 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.