0

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;
 }
Andrea Torres
  • 15
  • 1
  • 1
  • 6
  • 1
    Use `fgets()` to get the entire line and `strlen()` to count its length. If you want to be extra careful, you can iterate through the string using `isdigit()` to make sure you're only counting digits (e.g. you don't accidentally count the trailing newline). – torstenvl Dec 03 '18 at 07:13
  • Thank you would you be able to give me an example of fgets() I'm not sure that I am using it right. I thought that I was not working because I only have digits and not chars. Since I do not know the size how would I save the entire line. using malloc() ? – Andrea Torres Dec 03 '18 at 07:16
  • 1
    You're not using it ***at all***. Usage here would be to declare a `char buffer[1024]` and then `if (NULL == fgets(buffer, 1024, fp)) { /* Handle error... */ } ...` – torstenvl Dec 03 '18 at 07:21
  • Digits are characters, they're just always between `'0'` and `'9'` – Barmar Dec 03 '18 at 07:24
  • In POSIX you can also use `getline()`. It uses `malloc()` internally to allocate the space for you, so it can handle any size line. – Barmar Dec 03 '18 at 07:25
  • 1
    The problem is `return 0;` before the `do` loop. You're skipping over the code that counts the characters. – Barmar Dec 03 '18 at 07:27
  • I guess you meant to put that in the `if (fp == NULL)`, but you forgot to put `{}` around the two lines. – Barmar Dec 03 '18 at 07:27
  • See https://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1 – Barmar Dec 03 '18 at 07:28
  • Thank you all for helping it really helps to have a fresh set of eyes on code. Here is the working code. – Andrea Torres Dec 03 '18 at 07:49
  • you removed the check for missing file... if a the file does not exist you will have an issue with your code. you should re-add the `return 0;` to your code just make sure it is bracketed with the `printf("The file is not found!!");` statment – Omer Dagan Dec 03 '18 at 09:57

1 Answers1

0

Problems with the loop:

                                 // problem code
int numDigits=0;
char c;      
do {                           
  c = fgetc(fp);   
  // Checking for end of file 
  if (feof(fp)){ 
    numDigits++;
    break ; 
  }
  numDigits++;
}  while(1); 
  • Off by 1

    When it is time to exit, no need to add + 1 to the count.

  • Counts all characters as digits

    There is no test to determine if the character read is a digit.

  • Inadequate exit condition

    On a rare input error, OP's code loop forever with fgetc(fp) returning EOF and feof(fp) not true.

  • Count may readily exceed INT_MAX.


Some fixes:

// 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); 
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256