c - 如何从文件中读取数字以获取 c 中的大小
问题描述
对于作业,我必须创建 2 个文件,每个文件的数字都超过 100 位。我可以很好地做到这一点,但是当需要找出文件中有多少位数时,我很难过我已经尝试过 fgets(),但我认为我处理得不好。任何帮助是极大的赞赏。
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include <stdio.h>
int getc(FILE *stream);
int getchar(void);
这对我的问题来说并不是真正需要的,但它是我代码的一部分,最终我需要获取每个数字的大小并将它们作为参数传递:
int * add(int * a,int s_a,int * b,int s_b){
if(s_b<s_a){
int tmp_s;
int * tmp_a;
tmp_s=s_a;
s_a=s_b;
s_b=tmp_s;
tmp_a=a;
a=b;
b=tmp_a;
}
int * out= (int *)malloc (sizeof(int)*(s_b+1));
int i, carry,sum;
carry=0;
for(i=0;i<s_a;i++){
sum=((a[i])+b[i]%10)+carry;
carry=(a[i]+b[i])/10;
out[i]=sum;
}
for(i;i<s_b;i++){
sum=(b[i]+carry)%10;
carry=(b[i]+carry)/10;
out[i]=sum;
}
out[i]=carry;
return out;
}
这是我将随机数添加到文件的地方,我不添加空格,因为它应该都是一个非常长的数字。虽然我在这个函数中有大小不是这个函数的目的,一旦下一个函数正常工作,打印语句将被注释掉。
void addrandomNums(char * fileName){
srand ( time(NULL) );
FILE *ffp;
ffp=fopen(fileName,"w");//if the file exsisists then we erase and write
new stuff
int sizeOfNum=rand()%10000+100;//the size of the number will be from 100-10099(I think)the instructions said a number greater than 100 digits so I tried to make this as wide of a range as possible
printf("Size of the number for this file is: %d digits \n",sizeOfNum);
for(int i=1;i<sizeOfNum;i++){
fprintf(ffp,"%d", rand() % 10);//adds a number from 0-9
}
fclose(ffp);
}
这是需要帮助的功能我不知道我做错了什么。
int getNumDigits(char * fileName){
int numDigits=0;
FILE *fp=fopen(fileName,"w");
printf( "Reading the file: %s \n" ,fileName) ;
if (fp == NULL)
printf("The file is not found!!");
return 0;
do
{
// Taking input single character at a time
char c = fgetc(fp);
// Checking for end of file
if (feof(fp))
break ;
printf("%c", c);
numDigits++;
} while(1);
fclose(fp);
printf("***The number of characters present in file is: %d \n",numDigits);
//getch();
return numDigits;
}
int main(void) {
addrandomNums("num1.txt");
addrandomNums("num2.txt");
int size1=getNumDigits("num2.txt");
return 0;
}
谢谢大家的帮助,这是固定代码。我真的很感激帮助。
int getNumDigits(char * fileName){
int numDigits=0;
FILE *fp=fopen(fileName,"r");
printf( "Reading the file: %s \n" ,fileName) ;
//char buffer[MAX]=fgets()
// int length=strlen(fgets(buffer, MAX, fp));
if (fp == NULL)
printf("The file is not found!!");
char c;
do{
// Taking input single character at a time
c = fgetc(fp);
// Checking for end of file
if (feof(fp)){
numDigits++;
break ;
}
numDigits++;
} while(1);
fclose(fp);
printf("***The number of characters present in file is: %d \n",numDigits);
printf("All Done.");
return numDigits;
}
解决方案
循环问题:
// problem code
int numDigits=0;
char c;
do {
c = fgetc(fp);
// Checking for end of file
if (feof(fp)){
numDigits++;
break ;
}
numDigits++;
} while(1);
关闭 1
当该退出时,无需在计数中加+1。
将所有字符计算为数字
没有测试来确定读取的字符是否为数字。
退出条件不足
在罕见的输入错误中,OP 的代码将永远循环
fgetc(fp)
返回EOF
而feof(fp)
不是true。计数可能很容易超过
INT_MAX
。
一些修复:
// int numDigits=0;
unsigned long long numDigits = 0;
unsigned long long numCharacters = 0;
//char c;
int c; // fgetc() typically returns 257 different values
do {
c = fgetc(fp);
// if (feof(fp)){
// numDigits++;
// break ;
// }
if (c == EOF) {
break;
}
// numDigits++;
if (isdigit(c)) numDigits++;
numCharacters++;
} while(1);
推荐阅读
- python - 将一个字符串连接到另一个字符串中
- java - Stax Provider 子类型问题 - Tomcat
- matlab - 如何对包含子分组数据的矩阵进行行排序
- sql - 如何在 WHERE 子句中的变量中使用 AND
- ios - 如何使用 json 和 swift 4.1 在谷歌地图上添加多个标记?
- javascript - 使用 Node.js MySQL 库的正确方法是什么?
- arduino - Arduino 使用 LCD i2c 适配器(错误接收数据)
- html - 文本的 HTML CSS 中的加载效果
- excel - VBA 2016 上的 SumIfs
- ios - 嵌入 NavigationController swift4 时从 UIPageViewController 更新 ChildViewController