从文件c中读取多位数int

从文件c中读取多位数int,c,getchar,getc,C,Getchar,Getc,所以我有一个名为num.txt的文本文件,它有一个由空格分隔的整数字符串 假设num.txt包含:5321642586523 我想以读取格式打开文件并获取数字。所以我可以说 int iochar; FILE *fp; fp = fopen("num.txt", "r"); while ((iochar=getc(fp)) !=EOF){ if(iochar!=' '){ printf("iochar= %d\n", iochar); //this prints out

所以我有一个名为num.txt的文本文件,它有一个由空格分隔的整数字符串

假设num.txt包含:5321642586523

我想以读取格式打开文件并获取数字。所以我可以说

int iochar;
FILE *fp;

fp = fopen("num.txt", "r");
while ((iochar=getc(fp)) !=EOF){
    if(iochar!=' '){
        printf("iochar= %d\n", iochar); //this prints out the ascii of the character``
    }
^这适用于单个数字。但是我应该如何处理两位、三位或更多位的数字呢?

使用
strtol()
解析整数列表:

char buf[BUFSIZ];

while (fgets(buf, sizeof buf, stdin)) {
    char *p = buf;

    while (1) {
        char *end;

        errno = 0;
        int number = strtol(p, &end, 10);

        if (end == p || errno) {
            break;
        }

        p = end;

        printf("The number is: %d\n", number);
    }
}
如果要解析浮点数,请使用
strtod()

使用
strtol()
解析整数列表:

char buf[BUFSIZ];

while (fgets(buf, sizeof buf, stdin)) {
    char *p = buf;

    while (1) {
        char *end;

        errno = 0;
        int number = strtol(p, &end, 10);

        if (end == p || errno) {
            break;
        }

        p = end;

        printf("The number is: %d\n", number);
    }
}

如果要解析浮点数,请使用
strtod()

将数据读入缓冲区,然后使用
sscanf
读取整数

char nums[900];
if (fgets(nums, sizeof nums, fp)) {
    // Parse the nums into integer. Get the first integer.
    int n1, n2;
    sscanf(nums, "%d%d", &n1, &n2);
    // Now read multiple integers
}

为什么不将数据读入缓冲区并使用
sscanf
读取整数呢

char nums[900];
if (fgets(nums, sizeof nums, fp)) {
    // Parse the nums into integer. Get the first integer.
    int n1, n2;
    sscanf(nums, "%d%d", &n1, &n2);
    // Now read multiple integers
}

使用缓冲区存储读取的字节,直到找到分隔符,然后使用atoi解析字符串:

char simpleBuffer[12];    //max 10 int digits + 1 negative sign + 1 null char string....if you read more, then you probably don't    have an int there....
int  digitCount = 0;
int iochar;

int readNumber; //the number read from the file on each iteration
do {

    iochar=getc(fp);

    if(iochar!=' ' && iochar != EOF) {
        if(digitCount >= 11)
            return 0;   //handle this exception in some way

        simpleBuffer[digitCount++] = (char) iochar;
    }
    else if(digitCount > 0)
        simpleBuffer[digitCount] = 0; //append null char to end string format

        readNumber = atoi(simpleBuffer);    //convert from string to int
       //do whatever you want with the readNumber here...

       digitCount = 0;  //reset buffer to read new number
    }

} while(iochar != EOF);

使用缓冲区存储读取的字节,直到找到分隔符,然后使用atoi解析字符串:

char simpleBuffer[12];    //max 10 int digits + 1 negative sign + 1 null char string....if you read more, then you probably don't    have an int there....
int  digitCount = 0;
int iochar;

int readNumber; //the number read from the file on each iteration
do {

    iochar=getc(fp);

    if(iochar!=' ' && iochar != EOF) {
        if(digitCount >= 11)
            return 0;   //handle this exception in some way

        simpleBuffer[digitCount++] = (char) iochar;
    }
    else if(digitCount > 0)
        simpleBuffer[digitCount] = 0; //append null char to end string format

        readNumber = atoi(simpleBuffer);    //convert from string to int
       //do whatever you want with the readNumber here...

       digitCount = 0;  //reset buffer to read new number
    }

} while(iochar != EOF);

与OPs风格保持一致:
检测数字组并在运行时累加整数

由于OP没有指定整数的类型,并且所有示例都是正数,因此假设type
unsigned

#include <ctype.h>

void foo(void) {
  int iochar;
  FILE *fp;

  fp = fopen("num.txt", "r");
  iochar = getc(fp);
  while (1) {
    while (iochar == ' ')
      iochar = getc(fp);
    if (iochar == EOF)
      break;
    if (!isdigit(iochar))
      break;  // something other than digit or space
    unsigned sum = 0;
    do {

      /* Could add overflow protection here */

      sum *= 10;
      sum += iochar - '0';
      iochar = getc(fp);
    } while (isdigit(iochar));
    printf("iochar = %u\n", sum);
  }
  fclose(fp);
}
#包括
无效foo(无效){
int-iochar;
文件*fp;
fp=fopen(“num.txt”,“r”);
iochar=getc(fp);
而(1){
while(iochar=='')
iochar=getc(fp);
if(iochar==EOF)
打破
如果(!isdigit(iochar))
break;//数字或空格以外的内容
无符号和=0;
做{
/*可以在这里添加溢出保护*/
总和*=10;
sum+=iochar-'0';
iochar=getc(fp);
}while(isdigit(iochar));
printf(“iochar=%u\n”,总和);
}
fclose(fp);
}

符合OPs风格:
检测数字组并在运行时累加整数

由于OP没有指定整数的类型,并且所有示例都是正数,因此假设type
unsigned

#include <ctype.h>

void foo(void) {
  int iochar;
  FILE *fp;

  fp = fopen("num.txt", "r");
  iochar = getc(fp);
  while (1) {
    while (iochar == ' ')
      iochar = getc(fp);
    if (iochar == EOF)
      break;
    if (!isdigit(iochar))
      break;  // something other than digit or space
    unsigned sum = 0;
    do {

      /* Could add overflow protection here */

      sum *= 10;
      sum += iochar - '0';
      iochar = getc(fp);
    } while (isdigit(iochar));
    printf("iochar = %u\n", sum);
  }
  fclose(fp);
}
#包括
无效foo(无效){
int-iochar;
文件*fp;
fp=fopen(“num.txt”,“r”);
iochar=getc(fp);
而(1){
while(iochar=='')
iochar=getc(fp);
if(iochar==EOF)
打破
如果(!isdigit(iochar))
break;//数字或空格以外的内容
无符号和=0;
做{
/*可以在这里添加溢出保护*/
总和*=10;
sum+=iochar-'0';
iochar=getc(fp);
}while(isdigit(iochar));
printf(“iochar=%u\n”,总和);
}
fclose(fp);
}


请提供当前代码和预期输出的输出请提供当前代码和预期输出的输出。不要第一次调用
fscanf()
,这是不安全的,最好在正确的轨道上调用
fgets()
。检查
sscanf()
结果很好。与其第一次调用
fscanf()
,这是不安全的,不如在正确的轨道上调用
fgets()
。检查
sscanf()
结果很好。如果我正在读取一个文件,因此我不知道在这种情况下设置char bu[BUFSIZ]的输入有多大,该怎么办?@Taz然后查询文件的大小(例如,使用
fseek()
ftell()
stat
在POSIX上),并使用
malloc()
分配足够大的缓冲区。当心零!1) 不需要
number==0
,只要
if(end==p).
。2) 也可以执行
errno=0;整数=strtol(p和end,10);如果(errno).
检测溢出。顺便说一句:
fgets()
将只读取一行(最多一行)“\n””,而不一定如注释所示读取整个文件。可能会起作用,因为OP说文件是“由空格分隔的整数字符串”。@chux你说得对,还添加了溢出检测和多行文件的处理。如果我从一个文件中读取,那么我不知道在这种情况下设置char bu[BUFSIZ]的输入有多大?@Taz然后你查询文件的大小(例如,在POSIX上使用
fseek()
ftell()
stat
),并使用
malloc()
分配足够大的缓冲区。注意终止零!1)不需要
number==0
,只要
if(end==p)
。2) 也可以执行
errno=0;整数=strtol(p和end,10);如果(errno).
检测溢出。顺便说一句:
fgets()
将只读取一行(最多一行)“\n””,而不一定如注释所示读取整个文件。可能会起作用,因为OP说文件是“由空格分隔的整数字符串”。@chux你说得对,还添加了溢出检测和多行文件的处理。+1改为使用
fgetc
fscanf
fgets
编写相同的答案。这与迄今为止尝试的OP相关的粘贴解决方案不同。
iochar
从未设置
iochar
应该是
intiochar
+1而不是使用
fgetc
fscanf
fgets
编写相同的答案。这一个与粘贴解决方案不同,与迄今为止尝试的OP相关。
iochar
从未设置
iochar
应该是
intiochar
charch-->
intch
@chux为什么
int
?如果我们使用
int
,那么我们也会得到与此相同的ascii值。1)什么值?2) 在
char
unsigned char
的平台上,
而((ch=fgetc(fp))!=EOF)
始终为真。3) 在
char
signed char
EOF==-1
的平台上,如果代码读取值为-1的
ch
,则它与
EOF
@chux无法区分,假设
1
写入文件并使用
fgetc(fp)
我们可以得到
1
即char,但如果我们将其分配给整数变量,它将是
49
而不是
1
。正如下面的答案一样,你也可以看到,他正在比较
io