• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

can't read from txt files

Hi guys,

I wrote the following code,trying to read from two files some data.


[code]
stream7  = fopen( "outfile1.txt", "w+" );
stream5  = fopen( "outfile1.txt", "r+" );
stream6  = fopen( "outfile2.txt", "r+" );
for(a5=0;a5<=300;a5++)
{
fscanf(stream5,"%e", &c1[a5][0]);
Sleep(1000);
printf("%e\n",c1[a5][0]);
fscanf(stream6,"%e", &c2[a5][0]);
*temp1 = (*temp1 + c1[a5][0] * c2[a5][0]);
fprintf(stream7,"%e\n",*temp1);
}
[code\]



The form of the files is something like this:

outfile1.txt

5.890487e-311
1.635034e-311
6.084601e-310
2.870287e-309
1.638044e-311
4.074601e-314
7.890457e-312
.
.
.

outfile2.txt

5.978627e-310
3.256761e-310
3.889652e-310
6.762316e-310
2.405802e-310
6.976636e-310
.
.
.



And the printf output is something like this:

0.000000e-0
0.000000e-0
0.000000e-0
0.000000e-0
0.000000e-0
0.000000e-0
0.000000e-0
0.000000e-0
0.000000e-0
0.000000e-0
.
.
.
0.000000e-0

The same goes with the output file(stream7).What seems to be the problem?Why the result is zero all the time?

Thanks in advance
0
naimead12
Asked:
naimead12
  • 2
  • 2
  • 2
  • +2
1 Solution
 
AxterCommented:
Hi naimead12,
Please post more of your code show the variable types.
What type is temp1?
What type is c1 and c2?
Show how these types are declared and initialized

David Maisonave :-)
Cheers!
0
 
naimead12Author Commented:
Unbielievable!!Sorry I forgot that...

double c1[4000][4000],c2[4000][4000];
double temp1[4000];    

I just initialize temp1 like :  
 *temp1=0;
just before the loop starts.
0
 
sunnycoderCommented:
You are reopening the same file twice
stream7  = fopen( "outfile1.txt", "w+" );
stream5  = fopen( "outfile1.txt", "r+" );

Also you are not checking return values from any call ... You should add error checking for fopen fscanf and fprintf

This does not look like actual code
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
naimead12Author Commented:
Well I just copy-paste the "stream5  = fopen( "outfile1.txt", "r+" );" and i forgot to change outfile1.txt to outfile3.txt...

How can I add error checking to fscanf and fprintf?

Just an idea though?I am noticing that the outfile1.txt and outfile2.txt values are too small.Is there any possibility that after multiply them, the result number is so small that cannot be interpreted by the machine?
0
 
sunnycoderCommented:
by checking the return values ... if (stream5 == NULL ) ...

Yes it is possible that very small numbers cannot be represented by the machine. try some other bigger numbers and observe the results
0
 
miket6000Commented:
It is true that those numbers are far too small to be represented by double or any other native c data type for that matter. I would suggest reading the numbers in as an array of char then writing a function to parse the resulting string and multiply by 10e308 or so, the below function should do that, though I appologise I'm currently unable to test the below code, if anyone spots any errors please let me know. Just remeber that the returned result is 10e308 times as large as the input.

float make_usable(char *line_from_file)
{
     char base_str[9], exponent_str[4];
     float base;
     int exponent, multiplier;

     for(int i = 0; i++; i <=7 ) /* read the first 8 characters into an array */
     {
          base_str[i] = *(line_from_file + i);
     }
     base_str[9] = '\0'; /*terminate string array */
     base = atof(base_str);

     for (int j = 0; j++; j<=2) /* read the last 3 characters into an array */
     {
          exponent_str[j] = *(line_from_file + 11 - j); /* this should read from the 11th character through to the 13th character */
     }
     exponent_str[4] = '\0'; /* terminate string array */
     exponent = atoi(exponent_str) - 308; /* multiply by 10e308 */
     
     multiplier = 1;
     for (int k = 0; k++; k <= exponent)
          multiplyer = multiplyer * 10;

     return base * multiplier;
}

0
 
miket6000Commented:
Sorry there is an error with the maths in the above code, but you can still get the basic idea
0
 
PaulCaswellCommented:
My documentation states:

"r+"

Opens for both reading and writing. (The file must exist.)

"w+"

Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

Therefore, your code:

stream7  = fopen( "outfile1.txt", "w+" );
stream5  = fopen( "outfile1.txt", "r+" );

Will first open file "outfile1.txt" and destroy its contents, then it will open this empty file for reading. This completely explains your zeros.

Paul
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now