For an assignment I must create 2 files each with a number that has more than 100 digits. I can do this well, but when it comes time to finding out exactly how many digits are int the file I am stumped I have tried fgets(), but I do not think that I am handling this well. Any help is greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include <stdio.h>
int getc(FILE *stream);
int getchar(void);
This is not really necissary to my problem but it is part of my code eventually I will need to get the size of each number and pass them as parameters here:
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;
}
This is where I am adding the random numbers to the file and works I do not add spaces because It is supposed to all be one very long number. Although I have the size in this function that is not the purpose of this function and the print statement will be commented out once the next function is working properly.
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);
}
This is the function that needs help I do not know what I am doing wrong.
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;
}
Thank you all for your help this is the fixed code. I really appreciate the help.
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;
}