Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
涉及文件和结构的C编程_C - Fatal编程技术网

涉及文件和结构的C编程

涉及文件和结构的C编程,c,C,如何使用fgetc从文件中读取负数?数字是如何编码的 如果是ASCII码,请记住它需要多个字符。您可以为它编写一个循环,但您可能会找到fscanf更多帮助 如果是二进制数据,请记住,fgetc无论如何只读取8位——同样,您需要考虑其他函数才能有效地执行此操作 这里的要点是,除非你只是为了证明你能做到,否则,fgetc可能是错误的答案。可能fgets?fgetc()将字符解释为“无符号字符”,但将其强制转换为int(但在文件末尾返回EOF,即-1) 如果源文件包含有符号值的某些表示形式,则需要对其

如何使用fgetc从文件中读取负数?

数字是如何编码的

如果是ASCII码,请记住它需要多个字符。您可以为它编写一个循环,但您可能会找到
fscanf
更多帮助

如果是二进制数据,请记住,
fgetc
无论如何只读取8位——同样,您需要考虑其他函数才能有效地执行此操作

这里的要点是,除非你只是为了证明你能做到,否则,
fgetc
可能是错误的答案。可能
fgets

fgetc()将字符解释为“无符号字符”,但将其强制转换为int(但在文件末尾返回EOF,即-1)


如果源文件包含有符号值的某些表示形式,则需要对其进行解码。

fgetc
一次只读取一个字符。如果您试图从文件中读取负数或任何数字,请使用
fscanf

#include <stdio.h>

main()
{
  int v;
  fscanf (stdin, "%d", &v);
  printf ("v = %d\n", v);
}
#包括
main()
{
INTV;
fscanf(标准输入、%d、&v);
printf(“v=%d\n”,v);
}

'0'的ascii值为0x30,'1'的ascii值为0x31。。。到目前为止。。。 假设您的文件中只有一个编号:

   FILE * pFile;
   int c, n = 0;
   bool negative;
   pFile=fopen ("myfile.txt","r");
   if (pFile==NULL){
      perror("Error opening file");
   }else{
        c = fgetc (pFile);
        negative = (c == '-');
        do {
          c = fgetc (pFile);
          if (c>=0x30 && c<=0x39) {
         n = n*10 + (c-0x30);
       }
        } while (c != EOF);
        fclose (pFile);
   }
   if(negative==true) n=n*-1;
   printf ("Your number: %d\n",n);
文件*pFile;
int c,n=0;
布尔阴性;
pFile=fopen(“myfile.txt”、“r”);
if(pFile==NULL){
perror(“打开文件时出错”);
}否则{
c=fgetc(pFile);
负=(c='-');
做{
c=fgetc(pFile);

如果(c>=0x30&&c我同意Charlie的观点,即有比多次调用fgetc更好的方法,但如果坚持使用该函数,则需要运行一个循环来计算每个字符。这也取决于数据的编码方式。如果是ascii(正如使用返回字符的func所暗示的那样)您可以检查第一个字符是否为“-”,然后使用atoi(const char*)将每个后续字符转换为int,每次转换一个,并将结果值乘以10,然后再将新值添加到其中。更好的方法是读取多个字符(使用FGET或其他方法)然后用atoi(const char*)转换字符*。如果您更清楚地描述了您试图做的事情,可能会提供更好的答案。请注意,如果您的数据格式不完全符合您指定的格式,则使用fscanf将失败。但fscanf似乎确实是您解决此问题的答案。

如果“结构”在标题中暗示二进制,那么您可能想使用fread(),但如果您真的想了解二进制文件中存储的整数的布局是什么,那么可以使用fgetc()

这段代码显示了如何使用并集将一系列读取字节映射回整数

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

union Integer
{
    int intPart_;
    char charPart_[4];
};


int main(int argc, char* argv[])
{
    FILE* pFile = fopen("integerFile.dat", "w");

    int intWritten = -257;
    size_t bytesWritten = fwrite(&intWritten, 1, sizeof(int), pFile);
    assert(bytesWritten == sizeof(int));
    fclose(pFile);

    pFile = fopen("integerFile.dat", "r");
    int intRead = 0;
    size_t bytesRead = fread(&intRead, 1, sizeof(int), pFile);
    assert(bytesRead == sizeof(int));
    printf("%d\n", intRead);
    fclose(pFile);

    pFile = fopen("integerFile.dat", "r");
    Integer intToRead;
    for(int i = 0;
        i != sizeof(int);
        ++i)
    {
        int byteRead = fgetc(pFile);
        intToRead.charPart_[i] = byteRead;
        printf("%d\n", byteRead );
    }
    printf("%d\n", intToRead.intPart_);


    fclose(pFile);


    return 0;
}
#包括
#包括
#包括
并集整数
{
int intPart_2;;
char charPart_u4];
};
int main(int argc,char*argv[])
{
FILE*pFile=fopen(“integerFile.dat”,“w”);
intwrited=-257;
size_t byteswrited=fwrite(&intwrited,1,sizeof(int),pFile);
断言(bytesWrited==sizeof(int));
fclose(pFile);
pFile=fopen(“integerFile.dat”,“r”);
intRead=0;
size_t bytesRead=fread(&intRead,1,sizeof(int),pFile);
断言(bytesRead==sizeof(int));
printf(“%d\n”,intRead);
fclose(pFile);
pFile=fopen(“integerFile.dat”,“r”);
整数整数读入;
对于(int i=0;
i!=sizeof(int);
++(一)
{
int byteRead=fgetc(pFile);
intToRead.charPart_i]=byteRead;
printf(“%d\n”,byteRead);
}
printf(“%d\n”,intToRead.intPart);
fclose(pFile);
返回0;
}

如果不进行溢流/下溢检查,您可以使用fscanf进行返回值检查:

#include <stdio.h>

int main()
{
  int v;
  if( 1==fscanf(yourfilepointer, "%d", &v) )
    printf ("v = %d\n", v);
  else
    fputs("error on reading v",stderr);
  return 0;
}
#包括
int main()
{
INTV;
如果(1==fscanf(您的文件指针、%d、&v))
printf(“v=%d\n”,v);
其他的
fputs(“读取v时出错”,标准);
返回0;
}