⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileio.c

📁 SOM-sd 是现在国外非常流行的一个神经网络的结构自组织特征映射算法
💻 C
📖 第 1 页 / 共 5 页
字号:
    for (i = 0; i < nmemb; i++){      for (j = 0; j < size/2; j++){	cval = cptr[j];	cptr[j] = cptr[size-j-1];	cptr[size-j-1] = cval;      }      cptr += size;    }     }  else{  // Either target or source is PDP_ENDIAN. This means we need to swap         // byte pairs    if (endian == BIG_ENDIAN || fi->byteorder == BIG_ENDIAN){      for (i = 0; i < nmemb; i++){	for (j = 0; j < size; j+=2){	  cval = cptr[j];	  cptr[j] = cptr[j+1];	  cptr[j+1] = cval;	}	cptr += size;      }    }    else if (endian == LITTLE_ENDIAN || fi->byteorder == LITTLE_ENDIAN){      for (i = 0; i < nmemb; i++){	for (j = 0; j < size; j+=2){	  cptr[j] = cptr[size-j-2];	  cptr[j+1] = cptr[size-j-1];	}	cptr += size;      }       }  }  return res;}/******************************************************************************Description: Reads and ignores all characters in ifile starting from the              current file position until the end-of-line character. This             function is particularly useful to skip over comment lines.Return value: ******************************************************************************/void GotoEndOfLine(struct FileInfo *finfo){  int cval;  cval = zgetc(finfo);  while(cval >= 0 && cval != '\n')    cval = zgetc(finfo);  if (cval == '\n')    finfo->lineno++;}/******************************************************************************Description: Returns the next character in ifile which is not a white-space.             The character is returned to the stream so that the file pointer             is set to the position of that character.Return value: The next non-white-space character in ifile, or '\n' if the              end of a line was reached, or a negative value if the end-of-file              was reached.******************************************************************************/int ReadAhead(struct FileInfo *finfo){  int cval;  if (finfo == NULL || finfo->fptr == NULL)    return -1;  cval = zgetc(finfo);  while(cval >= 0 && isspace(cval) && cval != '\n')    cval = zgetc(finfo);  if (cval >= 0)    zungetc(cval, finfo); /* Push the char back into the stream */  return cval;}/******************************************************************************Description: Verify that more data is available in the current line in a             stream pointed to by ifile. If no more data is available, an              error is raised, and 0 returned.Return value: 1 if more data is available, 0 otherwise (end of line or end of              file reached).******************************************************************************/int SetErrorIfDataUnavailable(struct FileInfo *finfo){  int cval;  cval = ReadAhead(finfo);  if (cval == '\n'){    AddError("Unexpected end of line.");    return 0;            /* No data available */  }  else if (cval < 0){    AddError("Unexpected end of file.");    return 0;            /* No data available */  }  return 1;}/******************************************************************************Description: Verify that no more data is available in the current line in a             stream pointed to by ifile. If more data is available, an              error is raised, and 0 returned.Return value: 1 if no more data is available (end of line or end of file              reached), 0 otherwise (an error is raised).******************************************************************************/int SetErrorIfDataAvailable(struct FileInfo *finfo){  int cval;  cval = ReadAhead(finfo);  if (cval == '\n'){      /* If end of line reached then move  */    cval = zgetc(finfo);  /* filepointer to start of next line */    finfo->lineno++;      /* Keep track of the line number     */    return 1;  }  else if (cval < 0)  /* End of file reached */    return 1;  if (cval == '#'){       /* Start of a comment               */    GotoEndOfLine(finfo); /* Ignore everything until new line */    return 1;  }  AddError("Unexpected trailing data found in file.");  return 0;               /* Data available */}/******************************************************************************Description: Verify that no more data is available in the given stream pointed             to by ifile. If more data is available, an error is raised, and 0             returned.Return value: 1 if no more data is available (end of file reached),              0 otherwise (and an error is raised).******************************************************************************/int SetErrorIfAnyDataAvailable(struct FileInfo *finfo){  int cval;  cval = zgetc(finfo);       /* Try to get a data */  if (cval >= 0){            /* If successful reading a data, then */    zungetc(cval, finfo);    /* put it back into the stream and    */    AddError("Unexpected trailing data found in file."); /* raise an error.  */    return 0;  }  return 1;                            /* Data unavailable */}/******************************************************************************Description: Add information about the current file status to the list of             errors if errors were raised in the past.Return value: No value is returned.******************************************************************************/void AddFileInfoOnError(struct FileInfo *finfo){  if (CheckErrors() > 0){    char cptr[4096];    if (finfo->byteorder){      if (finfo->fname)	snprintf(cptr, 4096, "Error occured when reading binary file '%s'.", finfo->fname);      else	snprintf(cptr, 4096, "Error occured when reading from binary stream.");    }    else{      if (finfo->fname)	snprintf(cptr, 4096, "Error occured in line %d of file '%s'.", (int)finfo->lineno, finfo->fname);      else	snprintf(cptr, 4096, "Error occured when reading line %d.", (int)finfo->lineno);    }    AddError(cptr);  }}/******************************************************************************Description: Read nmemb of binary floats from file pointed to by finfo and             store in ptr.Return value: The number of floats read successfully.******************************************************************************/size_t ReadBinaryVector(FLOAT *ptr, size_t nmemb, struct FileInfo *finfo){  size_t nval;  nval = bo_fread(ptr, sizeof(FLOAT), nmemb, finfo); /* Read the vector */  if (nval != nmemb)                             /* Check if successful */    AddError("Unexpected end of file.");  return nval;}/******************************************************************************Description: Write nmemb of binary floats to file pointed to by finfo.Return value: The number of floats written successfully.******************************************************************************/size_t WriteBinaryVector(FLOAT *ptr, size_t nmemb, struct FileInfo *finfo){  size_t nval;  nval = fwrite(ptr, sizeof(FLOAT), nmemb, finfo->fptr); /* Write the vector */  if (nval != nmemb)                             /* Check if successful */    AddError("Unable to write data. File system full?");  return nval;}/******************************************************************************Description: Read nmemb of float values from file pointed to by finfo and             store in ptr.Return value: The number of floats read successfully.******************************************************************************/size_t ReadAscIIVector(FLOAT *ptr, size_t nmemb, struct FileInfo *finfo){  size_t i;  float fval;  for (i = 0; i < nmemb; i++){    if (!SetErrorIfDataUnavailable(finfo)) /* Check if data is available*/      return i;    if (zscanf(finfo, "%f", &fval) != 1){  /* read next value */      AddError("File seems corrupted or does not contain expected data.");      return i;            /* Return the number of values read */    }    ptr[i] = (FLOAT)fval;  /* Store value in vector */  }  return i;                /* Return the number of values read */}/******************************************************************************Description: Write nmemb of float values to file pointed to by finfo.Return value: The number of floats written successfully.******************************************************************************/size_t WriteAscIIVector(FLOAT *ptr, size_t nmemb, struct FileInfo *finfo){  size_t i;  for (i = 0; i < nmemb; i++){    if (ptr[i] == (int)ptr[i]){      if (!fprintf(finfo->fptr, "%d ", (int)ptr[i])){ /* write next value */	AddError("Unable to write data. File system full?");	break;               /* Don't try to write any further   */      }    }    else if (!fprintf(finfo->fptr, "%.9f ", ptr[i])){  /* write next value */      AddError("Unable to write data. File system full?");      break;               /* Don't try to write any further   */    }  }  return i;                /* Return the number of values read */}/******************************************************************************Description: Read a single integer from a given file stream in binary format.Return value: The integer value if successful, 0 otherwise.******************************************************************************/int ReadBinaryInt(struct FileInfo *finfo){  int ival;  if (bo_fread(&ival, sizeof(int), 1, finfo) != 1){ /* Read the integer */    AddError("Unexpected end of file.");            /* In case of error */    return 0;  }  return ival;}/******************************************************************************Description: Read a single integer from a given file stream in AscII format.Return value: The integer value, or 0 on error (an error is raised)******************************************************************************/int ReadAscIIInt(struct FileInfo *finfo){  int ival;  if (!SetErrorIfDataUnavailable(finfo)) /* Check that data is available*/    return 0;  if (zscanf(finfo, "%d", &ival) != 1){  /* read the integer */    AddError("File seems corrupted or does not contain expected data.");    return 0;            /* Return 0 on error */  }  return ival;           /* Return the the integer value read */}/******************************************************************************Description: Find if 'line' is in the form "key=value" and return a pointer to             value.Return value: Return a pointer to the value of key if the `line` was in the              specified form, or NULL otherwise.******************************************************************************/char *GetFileOption(char *line, char *key){  char *valptr;  if (line == NULL || key == NULL)    return NULL;  valptr = strnspc(line);           //Find first non-white space character  if (!strncasecmp(valptr, key, strlen(key))){    //Check for keyword    valptr += strlen(key);          //Jump over keyword    if (isalnum((int)*valptr))      //Verify that key is a not a substring      return NULL;    valptr = strpbrk(valptr, "=:"); //Look for '=' or ':'    if (valptr != NULL){            //If we found a '=' or ':', then      for (valptr++; *valptr != '\0' && (*valptr == ':' || *valptr == '='); valptr++);                             //skip all occurences of '=' or ':', and      valptr = strnspc(valptr);     //find start of value after '=', or ':'      if (*valptr == '\0')          //No value there?	valptr = NULL;    }    return valptr;  }  return NULL;}/******************************************************************************Description: Auxilary function used to read num outlink information from the              binary file finfo. The information is stored in an array pointed             to by links. The outlink section of the file is a sequence of             positive integers which give the ID number of the child node.Return value: The maximum value of an ID number found in the link section             which can be -1 for a missing child.******************************************************************************/int ReadLinksBinary(int *links, UNSIGNED num, struct FileInfo *finfo){  int max = -1;  UNSIGNED i;  for (i = 0; i < num; i++){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -