![](/img/trans.png)
[英]How can I read a known number of strings of unknown size from a .txt file and store each line in a line of a matrix (in C)?
[英]How do I read a number from a file to get the size in 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;
}
這是我在文件中添加隨機數的地方,我不添加空格,因為它應該都是一個非常長的數字。 盡管我擁有此函數的大小,但這並不是該函數的用途,並且下一個函數正常工作后,print語句將被注釋掉。
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)
不是真的。
計數可能會輕易超過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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.