Exforsys

Online Training

strange result when read from different file

This is a discussion on strange result when read from different file within the C and C++ forums, part of the Programming Talk category; I am working on a program where I am reading data from multiple files in sequential directories. If one of ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-03-2006, 11:05 AM
Junior Member
 
Join Date: Jun 2006
Posts: 7
hat123 is on a distinguished road
strange result when read from different file

I am working on a program where I am reading data from multiple files in sequential directories. If one of the files is missing, the program will get the data from another file (in that directory). In both cases the data is written to arrays to be written to file at a later stage of the program. I am writing to screen the desired information as I read it--it is coming in correctly. However, if at any point I read from the secondary file, the first three values of the array1 are set to zero.

abbreviated version of the code:

Code:
while (i < max_num) {

if((dataFile = fopen(filename,"rb")) != NULL) {
    fread(&array1[i],sizeof(float),1,dataFile);
    fread(&array2[i],sizeof(float),1,dataFile);
    fread(&dumf,sizeof(float),1,dataFile);
    fread(&dumf,sizeof(float),1,dataFile);
    fread(&array3[i],sizeof(float),1,dataFile);
   close(dataFile);
}
else {
   if ((dataFile = fopen(filename2,"r")) != NULL){
       for (j=1;j<8; j++) {
          fgets(&dum,MAX_LINE,dataFile);   /* skip unneeded data lines */
      }
       fscanf(datafile,"%s%f",&dum,&array1[i]);
       fscanf(datafile,"%s%f",&dum,&array2[i]);
      for (j=1;j<8; j++) {
          fgets(&dum,MAX_LINE,dataFile);   /* skip unneeded data lines */
      }
      fscanf(datafile,"%s%f",&dum,&array3[i]);
   close(dataFile);
}
cnt++;
i++;

}   /* end while */

if (cnt > 1) {
   outFile = fopen("mydata.dat", "w");
    for (i=0; i<cnt; i++) {
        fprintf (outFile,"%f    %f     %f  \n",array1[i],array2[i],array3[i]);
   }
}
How do I fix this? I don't understand how it is happening in the first place, since I have verified that the correct values are written to the array initially.
Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-03-2006, 04:06 PM
Junior Member
 
Join Date: Jun 2006
Posts: 7
hat123 is on a distinguished road
I figured it out.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-05-2006, 04:59 PM
Member
 
Join Date: May 2006
Posts: 45
norman is on a distinguished road
Hi,
You have written as you have solved the problemt. What was the mistake. It would be nice if you could share your result with others

Norman
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-06-2006, 06:06 PM
Junior Member
 
Join Date: Jun 2006
Posts: 7
hat123 is on a distinguished road
my dummy variable "dum" didn't have a high enough dimension so the arrays were over writing. I set the size of dum to max_line (256) and the problem went away.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
how to open image file in c vijaykumarreddy C and C++ 4 07-30-2008 06:35 AM
C Programming - File management in C lokeshm C and C++ 1 06-17-2006 08:21 PM
TechMahindra Fresher Drive Result yash_mave Suggestions & Feedback 0 05-27-2006 06:48 AM
how to write into notepad file through winrunner? mangalsachin2002 Testing Tutorials 2 04-03-2005 02:39 PM
ClassNotFoundExceptions after Installing EAR file akpraveen Java Tutorials 3 07-20-2004 08:51 PM


All times are GMT -4. The time now is 09:42 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.