?
Solved

can't read from txt files

Posted on 2005-03-31
11
Medium Priority
?
249 Views
Last Modified: 2010-04-15
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
Comment
Question by:naimead12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
11 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13677033
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
 

Author Comment

by:naimead12
ID: 13677241
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13677299
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!

 

Author Comment

by:naimead12
ID: 13677408
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13677451
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
 

Accepted Solution

by:
miket6000 earned 240 total points
ID: 13678071
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
 

Expert Comment

by:miket6000
ID: 13678353
Sorry there is an error with the maths in the above code, but you can still get the basic idea
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13680032
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question