1

I tried to upload this program to my NXT, but for some reason I am not able to run it properly as it terminated with the message file error!. No idea why.

I am trying to read a .txt file into a char road[][]. I think it is where I insert the value into my 2d array where things go wrong.

#import "road.txt" 

task main()
{   
    FileOpenType road_txt;
    file.Filename = "road.txt";
    SysFileOpenRead(road_txt); 
    char road[][]; 
    while(road_txt.Result == NO_ERR)
    {
       for( int i = 0; i < 10; i++ )
       {

         for(int j = 0 ; j < 09 ; j++)
         {  
            char temps = fgetc(road_txt.FileHandle);
            if(temps != '\n')
            {
                road[i][j] = temps;

            }
         }

       }    

    }

}   
  • What steps did you take to diagnose the error? E.g. are you sure the file is there? Are you sure it has the correct content? Have you tried adding debug messages to find the exact line where failure occurs? – zovits Sep 22 '15 at 11:15
  • All debug messages chrashed after road[i][j] = temps The .txt file is on the nxt – test Sep 22 '15 at 11:28
  • Don't forget that #import "road.txt" creates an implicit byte array named after the filename (in this case road) when it loads the file if you don't specify a name for the array - you're then redefining that 1d array into a 2d char array - that might be the issue - try removing the #import directive. – Zhaph - Ben Duguid Sep 23 '15 at 10:22
1

Most probably the problem is that the array is declared, but not initialized. The program does not know how much memory to allocate for this specific array and so there is no allocated storage in RAM for this array.

To solve this, try initializing your array. You can either provide the starting dimensions during declaration, like in the last example here: http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/arrays.html Which in your case would look like this:

char road[10][9];

And it will result in an array the given size, filled with zeroes.

Or use a function call to do the same if you have specific (not 0) initial values: http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group___array_functions_ga49d48de55d817e96ed132eadc21260eb.html

  • it does look like to work.. Problem is, when i try to run TextOut(0,LCD_LINE1,road[1]) it doesn't output anything... – test Sep 22 '15 at 12:08
  • So i am bit unsure if it actually do what it is supposed to do? – test Sep 22 '15 at 12:16
  • I guess that is because road is a 2D array, so road[1] is still an array, not a string what TextOut expects. – zovits Sep 22 '15 at 12:16
  • how would you convert a char array into a string using the nxc syntax. Doesn't seem to be any function capable of doing so.. – test Sep 22 '15 at 12:32
  • You could use the fgetsfunction to read string values from your file: bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/… Keep in mind though that fgets reads until it hits an end of line or an end of file character, so you will need to break your input file to lines or read the whole file as a string and then perform slicing. – zovits Sep 22 '15 at 13:33

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.