从文件c中读取多位数int
所以我有一个名为num.txt的文本文件,它有一个由空格分隔的整数字符串 假设num.txt包含:5321642586523 我想以读取格式打开文件并获取数字。所以我可以说从文件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
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