int main(void)
{
   int operations, i, operands;
   char *string_dig1, *string_dig2;

   // OPEN THE FILE
   FILE *ifp;
   ifp = fopen("bigint.txt", "r");
   fscanf (ifp, "%d", &operations);
   printf("%d\n", operations);


   // MAKE MEMORY TO READ THE STRING DIGITS INTO THE ARRAY
   string_dig1 = (char *)malloc(sizeof(char));
   string_dig2 = (char *)malloc(sizeof(char));

   for(i = 0; i <= operations; i++)
   {
      fscanf(ifp, "%d %s %s", &operands, string_dig1, string_dig2);
      printf("%d\n %s %s", operands, string_dig1, string_dig2);
   }

   fclose(ifp);
   system ("pause");
   return 0;
}

ive been trying to print the contents of the file. but its giving me wrong output.. this is the original
------------------------------------------------
3
1 8888888888 2222222222
2 9999999999 10000000000
2 10000000000 9999999999

--------------------------------------

i want to replicate it in the output screen but this is what it is giving me
----------------------------------------------------------
3
8888888888 2222222222
9999999999 100000000002
10000000000 99999999992

--------------------------------------------------------
1st line contains the number that represents number of operations,after that each line contains 3 digits. 1 and 2 represents addition and subtraction operands. next two are integers that should be read in as array of digits

Several observations about your code: string_dig1 = (char *)malloc(sizeof(char)); Allocates enough room for a single character - not an entire array of them. You need to allow for the largest input data you expect to get and take precautions against values greater than that. The fact that things have not failed for you yet is not an indication that you are doing something correct there.

You are printing all the values you read but you are placing the newline character in a particularly strange spot. Perhaps your print statement should look like: printf("%d %s %s\n", operands, string_dig1, string_dig2); Also, and this is just a good habit to get into, you should check the return values of your functions. This is particularly important when dealing with user (or file) input. scanf and family all return meaningful values to you; ignoring them is ill advised.

Several observations about your code: string_dig1 = (char *)malloc(sizeof(char)); Allocates enough room for a single character - not an entire array of them. You need to allow for the largest input data you expect to get and take precautions against values greater than that. The fact that things have not failed for you yet is not an indication that you are doing something correct there.

You are printing all the values you read but you are placing the newline character in a particularly strange spot. Perhaps your print statement should look like: printf("%d %s %s\n", operands, string_dig1, string_dig2); Also, and this is just a good habit to get into, you should check the return values of your functions. This is particularly important when dealing with user (or file) input. scanf and family all return meaningful values to you; ignoring them is ill advised.

so how do i dynamically allocate an array. since i dont know the number of elements it will hold

since i dont know the number of elements it will hold

You can either create a buffer of a set size and ignore input longer than that or read a character at a time and increase the buffer size as necessary to accommodate.

In either case you will need to move away from scanf ( fgets may work for you).

You can either create a buffer of a set size and ignore input longer than that or read a character at a time and increase the buffer size as necessary to accommodate.

In either case you will need to move away from scanf ( fgets may work for you).

ahh thank you very much.. solved the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.