Skip to main content
Known Participant
October 22, 2010
Question

Where the error occurred?

  • October 22, 2010
  • 1 reply
  • 481 views

When I read txt file during the second development using fdk, I can read all information from txt file,But when finish to read the txt file,FramMaker ocurres serious error.

code:

VoidT  GetEffectiveInfo()
{
    FILE *fp;
   
 
CharT a[10];

StringT page,startTime,endTime;

    fp=fopen("C:\\effTime.txt","r");
    if(fp!=NULL)
    {
        while(!feof(fp))
        {
            //fgets(str,25,fp);
   fscanf(fp,"%[^,]%*c",a);
            F_ApiAlert((StringT)a, FF_ALERT_CONTINUE_WARN);
        }
        fclose(fp);
    }
}

error information:


This topic has been closed for replies.

1 reply

Legend
October 22, 2010

zhaopeng,

I don't know enough about this to say what is wrong with your code. However, here are two functions which I've used reliably for some time. One uses FDK functions and the other uses C stdio.h stuff. Both return the text file as an F_StringsT array, with each string representing a line from the text file. Maybe this will help you troubleshoot your code.

Russ

//currently a maximum of 2048 characters per line

IntT ws_ReadTextFile(StringT path,

                     F_StringsT *returnStrings)

{

  ChannelT chan;

  FilePathT *filepath;

  UCharT ptr[1],

    uchar[2048];

  IntT numRead;

  UIntT i = 0;

  ws_StrsInit(returnStrings, 0, False);

  //convert the path and open the channel (file)

  filepath = F_PathNameToFilePath(path, NULL, FDosPath);

  if((chan = F_ChannelOpen(filepath,"r")) == NULL)

      return False;

  //read the first character

  numRead = F_ChannelRead(ptr, sizeof(UCharT),

                            1, chan);

  //run this loop, reading character by character until we get to the end

  while(!F_ChannelEof(chan))

  {

    //if we get to a carriage return, let's substitute a line termination escape

    if(ptr[0] == 13)

    {

      uchar = '\0';

      //and append the UCharT array to the end of the strings array. A line


      //feed means we have a complete line
      ws_StrsAppend(returnStrings, (StringT)uchar);

      //and reset this guy,
      i = 0;
    }

    //otherwise, append the character read to the UCharT array. We are building
    //up our string for the next full line. We ignore the newline character. Sometimes
    //it accompanies a carriage return (13) and we've already handled the line feed.
    //I hope that the newline (10) never stands by itself
    else if(ptr[0] != 10) uchar[i++] = ptr[0];

    //and get the next character and continue with the loop.
    numRead = F_ChannelRead(ptr, sizeof(UCharT), 1, chan);
  }

  //if there was a lingering line that wasn't terminated with a carriage return,
  //let's handle that.
  if(i > 0)
  {
    uchar = '\0';
    ws_StrsAppend(returnStrings, (StringT)uchar);
  }


  //all done
  F_ChannelClose(chan);


  return True;
}

//currently a maximum of 2048 characters per line
IntT ws_ReadTextFile_2(UCharT *fileName,
                       F_StringsT *returnStrings)
{

  FILE *stream;
  errno_t err;
  UCharT uchar[2048];
  IntT i;

  ws_StrsInit(returnStrings, 0, False);

   // Open for read (will fail if file "crt_fopen_s.c" does not exist)
   err = fopen_s(&stream, fileName, "r" );

   if(stream && !err)
   {
     while ( fgets ( uchar, sizeof uchar, stream ) != NULL )
     {
       for(i = 0; i < sizeof uchar; i++)
       {
         if(uchar == 10 ||
            uchar == 13 ||
            uchar == 0)
         {
           uchar = 0;
           i = sizeof uchar;
         }
       }

        ws_StrsAppend(returnStrings, (StringT)uchar);
     }
   }

   if(stream != NULL)
     fclose(stream);

   if(err) return False;
   else return True;

  
}

VoidT ws_StrsInit(F_StringsT *strings,
                  UIntT len,
                  UIntT deallocateFirst)
{
  if(deallocateFirst == True)
    F_ApiDeallocateStrings(strings);

  strings->len = len;
  strings->val = (StringT *) F_Alloc(len*sizeof(StringT), NO_DSE);
}

VoidT ws_StrsAppend(F_StringsT *strings,
                    StringT newString)
{
  //allocate/reallocate the new array
  if(strings->len == 0)
  {
    if(strings->val)
      F_ApiDeallocateStrings(strings);
    strings->len = 1;
    strings->val = (StringT *) F_Alloc(1*sizeof(StringT), NO_DSE);
  }
  else
  {
    strings->len++;
    strings->val =
      (StringT *) F_Realloc(strings->val,
        strings->len*sizeof(StringT), NO_DSE);
  }

  //put the new string on the end
  strings->val[strings->len - 1] = F_StrCopyString(newString);

  //we're done
  return;
}

zhaopengAuthor
Known Participant
October 22, 2010

Thank you very much!