Reading / Writing to a File

  • scaramanga
  • Born
  • Born
  • scaramanga
  • Posts: 4

Post 3+ Months Ago

Hi,

Found this forum very useful in my studies of C - language. Thank you for that.
Well now I have a problem of my own.
I am writing a program that can handle large numbers that can be 300 characters long.
The program is suppose to read the first number from file and then add the second number to it that user inputs and write it back to the file. Now, I have gotten to the point where I am supposed to write that number back to the file. I am kind of stuck with that.

Here is type definition for the "large number"
Code: [ Select ]
#define MAX 300

typedef int BigNumber[MAX];
BigNumber N;
  1. #define MAX 300
  2. typedef int BigNumber[MAX];
  3. BigNumber N;


and here is the function where i read the number from the the file.
Code: [ Select ]
void readbignumberFromfile(BigNumber N, FILE * file)
{
  char str[MAX];
  int i;
  fscanf(file,"%s", str);
            
  N[0] = strlen(str);  
    
  for (i = strlen(str)-1; i>=0; i--)
        N[N[0]-i] = chrtoint(str[i]);
}

int chrtoint(char a)
{
    int i;
    for (i = 48; i<=57; i++){
       if ( (char)(i)==a) return i-48;
  }
    return -1;    
}
  1. void readbignumberFromfile(BigNumber N, FILE * file)
  2. {
  3.   char str[MAX];
  4.   int i;
  5.   fscanf(file,"%s", str);
  6.             
  7.   N[0] = strlen(str);  
  8.     
  9.   for (i = strlen(str)-1; i>=0; i--)
  10.         N[N[0]-i] = chrtoint(str[i]);
  11. }
  12. int chrtoint(char a)
  13. {
  14.     int i;
  15.     for (i = 48; i<=57; i++){
  16.        if ( (char)(i)==a) return i-48;
  17.   }
  18.     return -1;    
  19. }


how could it be done easily to write the number back to the file. Should convert the number from int to char and then write it back or how should it be done?
I tried this kind of function but it only adds some kinf of squares to file.
Code: [ Select ]

void writebignumberTofile(BigNumber N, FILE * file)
{    
  int i;
  for (i = N[0]-1; i>=0; i--)
   putc(N[i], file);    
}
  1. void writebignumberTofile(BigNumber N, FILE * file)
  2. {    
  3.   int i;
  4.   for (i = N[0]-1; i>=0; i--)
  5.    putc(N[i], file);    
  6. }
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

putc prints a character to the file. Right now your print the ascii character that is represented by the numeric number in your array.

Image
http://digitalsushi.com/midashi/

Try adding 48 to each number to save to the file.

Code: [ Select ]
putc(N[i]+48, file);


You can also use the itoa function: http://www.cplusplus.com/reference/clib ... dlib/itoa/
  • scaramanga
  • Born
  • Born
  • scaramanga
  • Posts: 4

Post 3+ Months Ago

Oh yes, of course. Thank you :)
  • scaramanga
  • Born
  • Born
  • scaramanga
  • Posts: 4

Post 3+ Months Ago

Hello again,

Now I am stuck in a another part on this program.
When the program reads the "BigNumber" from file and subtracts a number that user inputed. It doesnt write it back to the file correctly. Let say I have a number 123456789 in the file. Then I subtract from that 123456787. The result is 2. It does that fine, but the number that is in the file is 223456789. It puts the 2 as a first number as it should, but how can I get rid of all the other numbers that were after the new number in the file?
Here is the part where I write it to the file.
Code: [ Select ]
void writebignumberTofile(BigNumber N, FILE * file)
{
    
  int i;
  for (i = N[0]; i>0; i--)
   putc(N[i]+48, file);    
}
  1. void writebignumberTofile(BigNumber N, FILE * file)
  2. {
  3.     
  4.   int i;
  5.   for (i = N[0]; i>0; i--)
  6.    putc(N[i]+48, file);    
  7. }


Or is there a problem in the subtraction function
Code: [ Select ]
 void Sub(BigNumber N, BigNumber X)
 {
  int i, j;
 
  for (i=1; i<=N[0]; i++)
   if (X[i]>N[i])    
   {
     j = i;
    
     while (N[++j] == 0);
     N[j]--;
    
     j--;
    
     while (j>i)
     {
      N[j] = 9;
      j--;
     }
    
     N[i] += 10;
     N[i] -= X[i];
   }
   else
   {
     N[i] -= X[i];
   }
  
  while ((N[N[0]] == 0) && (N[0]>0))
  N[0]--;
 }
  1.  void Sub(BigNumber N, BigNumber X)
  2.  {
  3.   int i, j;
  4.  
  5.   for (i=1; i<=N[0]; i++)
  6.    if (X[i]>N[i])    
  7.    {
  8.      j = i;
  9.     
  10.      while (N[++j] == 0);
  11.      N[j]--;
  12.     
  13.      j--;
  14.     
  15.      while (j>i)
  16.      {
  17.       N[j] = 9;
  18.       j--;
  19.      }
  20.     
  21.      N[i] += 10;
  22.      N[i] -= X[i];
  23.    }
  24.    else
  25.    {
  26.      N[i] -= X[i];
  27.    }
  28.   
  29.   while ((N[N[0]] == 0) && (N[0]>0))
  30.   N[0]--;
  31.  }
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

It probably has something to do with your subtract function. Also your print function seems strange to me. Your using the first element to figure out how many numbers to print. Does that first element always contain the length of the number?
  • scaramanga
  • Born
  • Born
  • scaramanga
  • Posts: 4

Post 3+ Months Ago

Yep, the first element always contains the length of the number. It is easier to handle when comparing the numbers which is bigger.

I tested that again yesterday and seemed fine if I added the \n character after I have written back to to file. Have to do some more testing to see if it works properly. Don't know if it is the right solution, is it?
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

When I wrote my big number/big int program I used C++ Classes and also overloaded the operators. It really depends on what the requirements are. I would probably have sub return a new BigNumber as to preserve both the initial numbers, but I don't know what the requirements are.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 48 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.